ruby-on-rails – 用于rails 3.1.4应用程序控制器的rspec 2.10.1中的RunTimeError:ActionController :: RackDelegation

在我们的rails 3.1.4 app中,rspec用于测试应用程序控制器中的公共方法require_signin.这是方法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生成巨大的多页错误,如下所示:

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摆脱了错误.
it "redirects if some condition" do
  subject.send(:helper_method)
  get :action # <= Need this before setting expectation below
  response.should redirect_to("/somewhere")
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...