简体   繁体   English

如何在application_controller中测试通用方法

[英]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.

相关问题 在application_controller中测试&#39;skip_after_action&#39;方法 - Test 'skip_after_action' method in application_controller 如何测试创建实例变量的Application_Controller - How to test Application_Controller that creates instance variables 如何在application_controller中调用方法? - How do I call a method in application_controller? 如何修复application_controller中的未定义方法错误? - How to Fix undefined method error in the application_controller? API控制器的过期application_controller缓存方法 - expire application_controller cached method from API controller Application_controller定义仅显示未来日期的方法 - Application_controller define method to only show future dates 无法从视图访问application_controller中的Helper方法 - Helper method from application_controller not accessible from view Rails 5和Rspec 3.5:局部视图中的application_controller方法 - Rails 5 and Rspec 3.5: application_controller method in a view partial 向 application_controller 添加一个方法,使其对所有控制器和视图可见 - adding a method to application_controller so it is visible to all controllers and views Rails 3.1 Engine:如何让引擎application_controller与客户端应用程序的application_controller对话? - Rails 3.1 Engine: How do I get the engine application_controller to talk to the client app's application_controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM