我使用了<代码>has_many>的测试装置:通过,通过<代码>。
# posts.yml
one:
title: "Railscasts"
url: "http://railscasts.com/"
description: "Ruby on Rails screencasts"
# categories.yml
one:
name: "magazine"
two:
name: "tutorial"
three:
name: "news"
four:
name: "Ruby"
# posts_controller_test.rb
def test_post_create
assert_difference Post.count do
post :create, post: posts(:one).attributes
.merge(categories: [categories(:two), categories(:four)])
end
end
在添加了另一处固定装置后,经过审判后,它就没有工作。
# post_categories.yml
one:
post: one
category: two
two:
post: one
category: four
def test_post_create
assert_difference Post.count do
post :create, post: posts(:one)
end
end
puts posts(:one).attributes
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00}
puts posts(:one).attributes
.merge(categories: [categories(:two), categories(:four)])
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "category_ids"=>[980190962, 1018350795]}