检查 RSpec 中的 ActiveRecord 关联
我正在学习如何使用 Rspec 编写测试用例。我有一个简单的帖子评论支架,其中一个帖子可以有很多评论。我正在使用 Rspec 对此进行测试。我应该如何检查 Post :has_many :comments
。我应该存根 Post.comments
方法,然后通过返回评论对象数组的模拟对象来检查它吗?真的需要对 AR 关联进行测试吗?
I am learning how to write test cases using Rspec. I have a simple Post Comments Scaffold where a Post can have many Comments. I am testing this using Rspec. How should i go about checking for Post :has_many :comments
. Should I stub Post.comments
method and then check this with by returning a mock object of array of comment objects? Is testing for AR associations really required ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 ActiveRecord 关联应该经过 Rails 测试套件的充分测试(而且确实如此),因此大多数人并不觉得有必要确保它们能够正常工作——只是假设它们会正常工作。
如果您想确保您的模型使用这些关联,那就是不同的事情,并且您想要测试这一点并没有错。我喜欢使用 shoulda gem 来完成此操作。它可以让你做这样的巧妙的事情:
Since ActiveRecord associations should be well-tested by the Rails test suite (and they are), most people don't feel the need to make sure they work -- it's just assumed that they will.
If you want to make sure that your model is using those associations, that's something different, and you're not wrong for wanting to test that. I like to do this using the shoulda gem. It lets you do neat things like this:
一般来说,测试关联是一种很好的做法,尤其是在 TDD 受到高度重视的环境中——其他开发人员通常会在查看相应的代码之前先查看您的规范。测试关联可确保您的规范文件最准确地反映您的代码。
测试关联的两种方法:
使用 FactoryGirl:
这是一个相对肤浅的测试,使用这样的工厂:
如果您的模型缺少与注释的
has_many
关联,则返回 NoMethodError。您可以使用 ActiveRecord #reflect_on_association 更深入地了解您的协会的方法。例如,具有更复杂的关联:
您可以更深入地了解您的协会:
并测试相关的任何选项或条件。
Testing associations is good practice generally, especially in an environment where TDD is highly regarded- other developers will often look to your specs before looking at the corresponding code. Testing associations makes sure that your spec file most accurately reflects your code.
Two ways you can test associations:
With FactoryGirl:
This is a relatively superficial test that will, with a factory like:
return you a NoMethodError if your model lacks a
has_many
association with comments.You can use the ActiveRecord #reflect_on_association method to take a more in-depth look at your association. For instance, with a more complex association:
You can take a deeper look into your association:
and test on whatever options or conditions are relevant.
如果您不想使用 shoulda 之类的外部 gem 来测试您的关联(请参阅罗伯特·斯派克的答案 有关详细信息),另一种选择是使用
reflect_on_association
获取相关关联的AssociationReflection
对象,然后对其进行断言:If you'd rather not use an external gem like shoulda to test your associations (see Robert Speicher's Answer for details on that), another option is to use
reflect_on_association
to get theAssociationReflection
object for the relevant association, and then assert on that:大多数人不会测试关联,因为 Rails 已经有单元测试来确保这些方法正常工作。如果您正在做一些复杂的事情,例如涉及过程或其他东西,您可能需要显式地测试它。通常你可以通过做
或类似的事情来做到这一点。
Most people don't test the associations, as Rails already has unit tests to make sure those methods work correctly. If you are doing something complex, like involving a proc or something, you might want to explicitly test it. Usually you can do this by just doing
or something akin to that.