我如何摆脱@controller 在我的测试中为零错误 [英] How do i get rid of @controller is nil error in my tests

查看:33
本文介绍了我如何摆脱@controller 在我的测试中为零错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在接受

 @controller is nil: make sure you set it in your test's setup method.

当我运行测试时.知道这是什么意思吗?

When I run my tests. Any idea what this means?

推荐答案

当您从 ActionController::TestCase 继承时,它会从测试名称推断控制器名称,如果它们不匹配,您必须使用 setup 设置它的测试的一部分.

when you inherit from ActionController::TestCase it infers the controller name from the test name if they do not match you have to use the setup part of test to set it.

所以如果你有

class PostsControllerTest < ActionController::TestCase
  def test_index
    #assert something
  end
end

然后 @controller 被自动实例化为 PostsController,但是,如果不是这种情况并且你有一个不同的名字,你需要一个 setup代码>如此

Then @controller is auto instantiated to PostsController, however, if this were not the case and you had a different name you would need a setup as such

class SomeTest < ActionController::TestCase
  def setup
    @controller = PostController.new
  end
end

这篇关于我如何摆脱@controller 在我的测试中为零错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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