What's the purpose of assigns method in Rails tests (MiniTest)?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 15:36:03

问题


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?


回答1:


It means if a controller defined an instance variable @item="something".

You can fetch an instance variable in your test with e.g.:

# It will check if the instance variable is a string.
assert_kind_of String, assigns(:item)



回答2:


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



来源:https://stackoverflow.com/questions/27214443/whats-the-purpose-of-assigns-method-in-rails-tests-minitest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!