Eth*_*han 27 testing tdd unit-testing ruby-on-rails fixtures
我正在阅读本指南中关于Rails装置的信息(谢谢,trevorturk).看来你在Yaml文件中定义了类,它们会自动加载到test
DB中 - 很酷.
但是如果你想指定这个食谱属于那本食谱(或其他什么)你怎么做?
您是否应该在Yaml代码中指定cookbook.id
和recipe.cookbook_id
手动赋值?(只是一个猜测 - 指南没有显示那样的东西.)或者是否有更合适的方式?
And*_*Vit 58
您应该使用命名的fixture,它会为您自动生成一个id号,而不提供.这些id号基本上是你使用的任何字符串的整数哈希值.如果您引用了指定版本,请不要添加"_id":
# recipes.yml
chicken_soup:
cookbook: my_recipes
# cookbooks.yml
my_recipes:
title: My Test Cookbook
Run Code Online (Sandbox Code Playgroud)
此外,如果您希望有多对多关联( HABTM ),您只需在夹具中为关联提供一个数组:
# recipes.yml
chicken_soup:
cookbooks: [my_recipes, another_recipe]
Run Code Online (Sandbox Code Playgroud)