简体   繁体   English

活动记录测试has_many关联

[英]Active record test has_many association

I'm trying to test if some entities are properly associated with the owner entity. 我正在尝试测试某些实体是否与所有者实体正确关联。

class Recipe < ActiveRecord::Base
    has_many :ingredients
end

I've inserted the ingredients and later on want to test if they belong to a given recipe. 我插入了成分,后来想测试它们是否属于给定的配方。 In cucumber I would like to test something like: 在黄瓜中,我想测试以下内容:

recipe.ingredients.should_contain(ingredient1, ingredient2)

How can I test this? 我该如何测试?

This seems more like a unit or integration test and less like a test of behavior. 这似乎更像是一个单元测试或集成测试,而不是行为测试。 You can use shoulda to test if the relation exists. 您可以使用Shoulda来测试该关系是否存在。 https://github.com/thoughtbot/shoulda-matchers . https://github.com/thoughtbot/shoulda-matchers Then you can do something like this for a unit test. 然后,您可以对单元测试执行类似的操作。

    recipe.should have_many(:ingredients)

On the other hand you can insert the ingredients through an integration test and check the relation on the other side just like you asked in your question. 另一方面,您可以通过集成测试插入成分,然后像您在问题中所问的那样,检查另一侧的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM