如何在 rspec 中的 lambda 之后测试不止一件事?
所以我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您试图在单个测试中断言太多,但它与测试的名称不匹配。请考虑这一点:
此外,您可能没有意识到有一种更好的方法来编写这些断言,它可以做同样的事情,但读起来更漂亮:
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:
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:
作为一名开发人员成长了几年之后,我找到了一个非常干净的解决方案,可以在需要时测试多个值:
但是当我们确实需要测试多个记录的创建时:
After growing as a developer for a couple of years, I have found quite a clean solution for testing multiple values when needed:
But when we do need to test creation of multiple records:
@idlefingers-回复:
解决这个问题,您可以使用以下技巧:
@idlefingers - re:
To get around this, you can use this trick: