[英]How do I test the Application Controller in rspec (get request)?
I want to test a get request in my application controller, my spec looks like: 我想在我的应用程序控制器中测试get请求,我的规格如下所示:
describe "GET some_get_method" do
it "should work" do
get :some_get_method
end
end
But I recieve the following error when I do this: 但是当我这样做时,我收到以下错误:
Failure/Error: get :some_get_method
AbstractController::ActionNotFound:
The action 'some_get_method' could not be found for ApplicationController
My application controller looks like: 我的应用程序控制器如下所示:
def some_get_method
vari = params[:vari]
if !vari
render_something
return false
end
true
end
In my routes I have: 在我的路线中,我有:
namespace :application do
get 'some_get_method/', :action => :some_get_method
end
It probably isn't appropriate to spec ApplicationController
directly for functional testing, as your real interest is in the behavior of the controllers derived from it. 直接指定ApplicationController
进行功能测试可能不适合,因为您的真正兴趣在于从其派生的控制器的行为。
For unit testing the methods within ApplicationController
, however, you could create a stub controller within the spec file (or, more appropriately, a test helper) that does nothing but expose the methods you want to test in ApplicationController
: 但是,要对ApplicationController
的方法进行单元测试,可以在spec文件(或更合适的是一个测试助手)中创建一个存根控制器,该控件除了在ApplicationController
公开要测试的方法外什么都不做:
class StubController < ApplicationController
end
You can now test Stub
's methods (really Application
's methods) directly without having to try and instantiate an abstract class. 现在,您可以直接测试Stub
的方法(实际上是Application
的方法),而不必尝试实例化抽象类。
Set up routing 设置路由
If you need to test rendering, one approach might be to add a test-only route accessible to RSpec's request handlers: 如果需要测试渲染,一种方法可能是添加RSpec的请求处理程序可访问的仅测试路由:
# in config/routes.rb
unless Rails.env.production?
namespace :stub do
get 'some_get_method', :action => :some_get_method
end
end
# in spec
describe "GET some_get_method" do
it "should work" do
get :some_get_method
# test
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.