如何与 Factory Girl 一起使用“分配”
嗨,
我有以下测试:
test "should annoy Chuck Norris" do
son = Factory.build(:son)
assert_difference('Son.count') do
post :create, son: { asset: son.asset, chuck_id: son.chuck }
end
assert_redirected_to chuck_path(assigns(son.chuck))
assert_equal 'The world has been destroyed...', flash[:notice]
end
assigns(son.chuck)
不起作用,当我尝试 son.chuck
时,它起作用了,但是在运行了所有测试之后创建一些 Chucks
,Factory.build(:son)
创建了一个 Chuck
,其 ID 为 12
和 断言_重定向_至
正在发送 ID 1
。导致出现以下错误:
Expected response to be a redirect to <http://test.host/chucks/12> but was a redirect to <http://test.host/chucks/1>
/Users/edison/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.1.0.rc4/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'
那么我怎样才能得到正确的Chuck.id
呢?
Hiho,
I have the following test:
test "should annoy Chuck Norris" do
son = Factory.build(:son)
assert_difference('Son.count') do
post :create, son: { asset: son.asset, chuck_id: son.chuck }
end
assert_redirected_to chuck_path(assigns(son.chuck))
assert_equal 'The world has been destroyed...', flash[:notice]
end
assigns(son.chuck)
doens't work, when I try son.chuck
, it works, but after running all my tests and creating some Chucks
, Factory.build(:son)
created a Chuck
with id 12
and the assert_redirected_to
are sending id 1
. Resulting the following error:
Expected response to be a redirect to <http://test.host/chucks/12> but was a redirect to <http://test.host/chucks/1>
/Users/edison/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.1.0.rc4/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'
So how can I get the correct Chuck.id
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想要使用的是
assigns(:chuck).son
。分配的参数必须与控制器中的变量名称匹配。换句话说,假设控制器中有
@chuck.son
,则应该使用assigns(:chuck).son
。请注意参数如何与@
符号后面的内容相对应。I think what you're looking to use is
assigns(:chuck).son
.The argument to assigns must match the variable name in the controller. In other words, assuming you have
@chuck.son
in the controller, you should useassigns(:chuck).son
. Note how the argument corresponds to what come right after the@
symbol.