What's the purpose of assigns method in Rails

2019-03-24 04:35发布

Used in automatically generated tests:

test "should create item" do
  login_user
  assert_difference('Item.count') do
    post :create, item: { creator: @item.creator, title: @item.title, user_id: @item.user_id, text: 'Hello, world!' }
  end

  assert_redirected_to(assigns(:item))
end

Rails documentation doesn't have any description. What's the purpose of this method and how to use it?

2条回答
你好瞎i
2楼-- · 2019-03-24 05:09

It means if a controller defined an instance variable @item="something". You can fetch instance variable in your test with e.g.

assert_kind_of String, assigns(:item) # will check if the instance variable is a string
查看更多
Luminary・发光体
3楼-- · 2019-03-24 05:19

Be aware assigns deprecated in Rails 5. And extracted to separate gem. To use it you must include 'rails-controller-testing' to your gemfile.

查看更多
登录 后发表回答