如何重构 JavaScript 和 HTML 请求\响应的测试代码?

发布于 2024-12-07 22:13:19 字数 779 浏览 0 评论 0原文

我正在使用 Ruby on Rails 3.1.0 和 rspec-rails 2 gem。由于我必须测试同一控制器操作的 HTML 和 JavaScript 请求,并且有时这些请求会通过渲染不同视图文件或表现不同来响应,因此我想重构一些代码。

通常,在我的控制器文件中,我有:

def create
  ...
  respond_to
    format.html
    format.js
  end
end

此时,为了测试 JS 和 HTML 请求\响应,在我的规范文件中,我有两个不同的示例(一个例如,对于每种情况):

context "POST create" do
  let(:user) { User.new }

  it "should correctly respond to a JS request" do
    xhr :post, :create
    ...
    session[:user].should be_nil
    flash[:notice].should be_nil
  end

  it "should correctly respond to a HTML request" do
    post :create
    ...
    session[:user].should be_nil
    flash[:notice].should be_nil
  end
end

我应该如何重构上面的代码?

I am using Ruby on Rails 3.1.0 and the rspec-rails 2gem. Since I have to test both HTML and JavaScript requests for the same controller action and since sometime those responds by rendering different view files or behaving differently, I would like to refactor some code.

Generally, in my controller file I have:

def create
  ...
  respond_to
    format.html
    format.js
  end
end

At this time, in order to test both JS and HTML requests\responses, in my spec file I have two different examples (one example, for each case):

context "POST create" do
  let(:user) { User.new }

  it "should correctly respond to a JS request" do
    xhr :post, :create
    ...
    session[:user].should be_nil
    flash[:notice].should be_nil
  end

  it "should correctly respond to a HTML request" do
    post :create
    ...
    session[:user].should be_nil
    flash[:notice].should be_nil
  end
end

How could\should I refactor the above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-12-14 22:13:19

您可以使用shared_examples_for

context "POST create" do
  let(:user) { User.new }

  shared_examples_for "a succesfull request" do
    it("does not set the user")  { session[:user].should be_nil }
    it("does not set the flash") { flash[:notice].should be_nil }
  end

  context "with a js request" do
    before(:each) do
      xhr :post, :create
    end

    it_should_behave_like "a succesfull request"
  end

  context "with a HTML request" do
    before(:each) do
      post :create 
    end

    it_should_behave_like "a succesfull request"
  end
end

希望这有帮助。

You could use shared_examples_for.

context "POST create" do
  let(:user) { User.new }

  shared_examples_for "a succesfull request" do
    it("does not set the user")  { session[:user].should be_nil }
    it("does not set the flash") { flash[:notice].should be_nil }
  end

  context "with a js request" do
    before(:each) do
      xhr :post, :create
    end

    it_should_behave_like "a succesfull request"
  end

  context "with a HTML request" do
    before(:each) do
      post :create 
    end

    it_should_behave_like "a succesfull request"
  end
end

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文