如何在 rspec 中的 lambda 之后测试不止一件事?

发布于 2024-10-25 02:19:39 字数 316 浏览 1 评论 0原文

所以我在Rails中使用rspec有类似的东西:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

但是post :create, @attr创建了一个用户和一个公司,那么我如何“链接”change调用以便我可以测试两者? 我正在寻找类似 end.should change(User,:count) &&更改(公司,:计数)

So I have something like this in Rails with rspec:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

But the post :create, @attr creates both a User and a Company, so how do I "chain" the change calls so that i can test both?
What Im looking for is something like end.should change(User,:count) && change(Company,:count)

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

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

发布评论

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

评论(3

后来的我们 2024-11-01 02:19:39

我认为您试图在单个测试中断言太多,但它与测试的名称不匹配。请考虑这一点:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

it "should create a new company" do
  lambda do
    post :create, @attr
  end.should change(Company,:count)
end

此外,您可能没有意识到有一种更好的方法来编写这些断言,它可以做同样的事情,但读起来更漂亮:

expect {
  post :create, @attr
}.to change(Company, :count)

I'd argue you're trying to assert to much in a single test and it doesn't match the name of the test. Consider this instead:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

it "should create a new company" do
  lambda do
    post :create, @attr
  end.should change(Company,:count)
end

Also, you may not be aware that there's a nicer way of writing those assertions which does the same thing, but reads much more nicely:

expect {
  post :create, @attr
}.to change(Company, :count)
红颜悴 2024-11-01 02:19:39

作为一名开发人员成长了几年之后,我找到了一个非常干净的解决方案,可以在需要时测试多个值:

expect{ execute }.to change{ [spe1.reload.trashed?, spe2.reload.trashed?] }.from([true, true]).to([false, false])

但是当我们确实需要测试多个记录的创建时:

[User, Company].each do |klass|
  it "creates one #{klass}" do
    expect{ post :create, valid_args }.to change(klass, :count).by(1)
  end
end

After growing as a developer for a couple of years, I have found quite a clean solution for testing multiple values when needed:

expect{ execute }.to change{ [spe1.reload.trashed?, spe2.reload.trashed?] }.from([true, true]).to([false, false])

But when we do need to test creation of multiple records:

[User, Company].each do |klass|
  it "creates one #{klass}" do
    expect{ post :create, valid_args }.to change(klass, :count).by(1)
  end
end
倾城花音 2024-11-01 02:19:39

@idlefingers-回复:

“我认为你试图在一次测试中断言太多,而且它
与测试名称不匹配”

  • 但有时,出于性能原因并减少测试套件时间,我不想使用另一个单独的示例来执行另一个断言。

解决这个问题,您可以使用以下技巧:

def user_and_company_count
  User.count + Company.count
end

it "can assert both counts" do
  expect { post :create, @attr }.to change(self, :article_an_activity_count).by(2)
end

@idlefingers - re:

"I'd argue you're trying to assert to much in a single test and it
doesn't match the name of the test"

  • Sometimes, though, I don't want another separate example just to perform another assertion, for performance reasons and to keep test suite time down.

To get around this, you can use this trick:

def user_and_company_count
  User.count + Company.count
end

it "can assert both counts" do
  expect { post :create, @attr }.to change(self, :article_an_activity_count).by(2)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文