RunTimeError:ActionController :: RackDelegation在rspec 2.10.1中用于rails 3.1.4应用程序控制器 [英] RunTimeError: ActionController::RackDelegation in rspec 2.10.1 for rails 3.1.4 application controller

查看:105
本文介绍了RunTimeError:ActionController :: RackDelegation在rspec 2.10.1中用于rails 3.1.4应用程序控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的rails 3.1.4 app中, rspec 用于测试应用程序控制器中的公共方法 require_signin 。以下是方法require_signin:

In our rails 3.1.4 app, rspec is used to test the public method require_signin in application controller. Here is the method require_signin:

  def require_signin
    if !signed_in?  
      flash.now.alert = "Log in first!"
      redirect_to signin_path
    end
  end  

这是 rspec 代码:

it "should invoke require_signin for those without login" do
  controller.send(:require_signin)
  controller {should redirect_to signin_path}  
end

以上 rspec 会产生巨大的多页错误,如下所示:

The above rspec generates gigantic multi pages error starting like the below:

RuntimeError:←[0m
       ←[31mActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: #<ApplicationController:0x3
a67f10 @_routes=nil, @_action_has_layout=true, @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_reques
t=#<ActionController::TestRequest:0x3a68720 @env={"rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x34fad60>, ........

rspec可能出现什么问题代码?非常感谢。

推荐答案

我遇到了这个错误并意识到我通过调用辅助方法触发控制器上的重定向想要测试,但我还没有真正实例化测试请求。在调用期望之前调用 get:index 消除了错误。

I came across this errror and realized I was triggering a redirect on the controller by calling a helper method I wanted to test, but I hadn't actually instantiated a test request yet. Calling get :index before calling the expectation got rid of the error.

it "redirects if some condition" do
  subject.send(:helper_method)
  get :action # <= Need this before setting expectation below
  response.should redirect_to("/somewhere")
end

这篇关于RunTimeError:ActionController :: RackDelegation在rspec 2.10.1中用于rails 3.1.4应用程序控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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