![](/img/trans.png)
[英]Rails / Active Record has_many through association - fetching a record
[英]Active record test has_many association
我正在尝试测试某些实体是否与所有者实体正确关联。
class Recipe < ActiveRecord::Base
has_many :ingredients
end
我插入了成分,后来想测试它们是否属于给定的配方。 在黄瓜中,我想测试以下内容:
recipe.ingredients.should_contain(ingredient1, ingredient2)
我该如何测试?
这似乎更像是一个单元测试或集成测试,而不是行为测试。 您可以使用Shoulda来测试该关系是否存在。 https://github.com/thoughtbot/shoulda-matchers 。 然后,您可以对单元测试执行类似的操作。
recipe.should have_many(:ingredients)
另一方面,您可以通过集成测试插入成分,然后像您在问题中所问的那样,检查另一侧的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.