开发者

How to use `assigns` with Factory Girl

开发者 https://www.devze.com 2023-03-13 01:52 出处:网络
Hiho, I have the following test: test \"should annoy Chuck Norris\" do son = Factory.build(:son) assert_difference(\'Son.count\') do

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?


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 use assigns(:chuck).son. Note how the argument corresponds to what come right after the @ symbol.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号