RunTimeError:ActionController :: RackDelegation在rspec 2.10.1中用于rails 3.1.4应用程序控制器

在我们的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: #"text/html"}, @_status=200, @_reques t=#[1, 1], "rack.input"=>#, ........ 

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 

如果要检查动作机制,则应在send此类调用之前使用should_receive

 it "should redirect to signin_path" do controller.should_receive(:redirect_to).with(signin_path) controller.send(:require_signin) end 

可能不是完全有用,但我在得到同样的错误后来到这里。 我开始使用传递测试套件,进行了一些更改,然后开始出现以下错误:

 RuntimeError: ActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: ...many lines... 

仔细看看错误之后,我注意到它在某处说:

 @flashes={:alert=>"You have to confirm your account before continuing."} 

我刚刚在Devise中添加了:confirmable选项,并意识到我创建的所有用户都尝试登录时未经证实,因此无法成功登录。我需要将confirmed_at Time.now添加到我的工厂/为用户创建夹具。 在你的例子中,似乎你试图测试什么时候没有登录,所以我不确定这是否一定适用。