在Rails测试(MiniTest)中,assign方法的目的是什么? [英] What's the purpose of assigns method in Rails tests (MiniTest)?

查看:87
本文介绍了在Rails测试(MiniTest)中,assign方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自动生成的测试中使用:

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 文档没有任何描述.这种方法的目的是什么以及如何使用它?

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

推荐答案

这意味着控制器是否定义了实例变量@item="something".

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)

这篇关于在Rails测试(MiniTest)中,assign方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆