[英]how to test a common method in application_controller
I have an auth method and want to put it in my application_controller. 我有一个auth方法,想把它放在我的application_controller中。
class ApplicationController < ActionController::Base
helper_method :check_cred
def check_cred
"within check cred"
end
but if I do this 但是如果我这样做
require 'spec_helper'
describe ApplicationController do
it 'should check_cred', task050: true do
check_cred.should == 'within check cred'
end
end
I get: 我得到:
undefined local variable or method `check_cred' for #<RSpec::Core::ExampleGroup::Nested_9:0x007ff5e3e40558>
How would I call a method like this to test? 我将如何调用这样的方法进行测试?
thx 谢谢
RSpec controller specs wrap ActionController::TestCase::Behavior , which provides some instance variables to be used during tests: RSpec控制器规范包装了ActionController :: TestCase :: Behavior ,它提供了一些在测试期间使用的实例变量:
Special instance variables
特殊实例变量
ActionController::TestCase will also automatically provide the following instance variables for use in the tests:
ActionController :: TestCase还将自动提供以下实例变量供测试使用:
@controller
: The controller instance that will be tested.@controller
:将要测试的控制器实例。
So you may be able to do the following: 因此,您可以执行以下操作:
it 'should check_cred', task050: true do
@controller.check_cred.should == 'within check cred'
end
Alternatively, you could move this helper method out into a separate helper module, and use an RSpec helper spec to perform the test, which may prove to be a better way to structure this test. 另外,您可以将此帮助程序方法移到单独的帮助程序模块中,并使用RSpec 帮助程序规范执行测试,这可能是构造此测试的更好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.