1

我在这个优秀的 Rails 教程的最后一章,我遇到了这段代码:

describe "creating a relationship with Ajax" do
  it "should increment the Relationship count" do
    expect do
      xhr :post, :create, relationship: { followed_id: other_user.id }
    end.to change(Relationship, :count).by(1)
  end

  it "should respond with success" do
    xhr :post, :create, relationship: { followed_id: other_user.id }
    response.should be_success
  end
end

我想知道是否有任何方法可以停止重复,类似于下面的代码(它不会工作,因为它只创建 2 个测试)?

it "should increment relationships count" do
  expect do
    xhr :post, :create, relationship: { followed_id: other_user.id }
    response.should be_success        
  end.to change(Relationship, :count).by(1)
end
4

1 回答 1

1

这不是 DRY 问题。您应该将递增测试和成功响应分开

于 2013-01-13T14:47:53.423 回答