9

在我们的 rails 3.1.4 应用程序中,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?非常感谢。

4

3 回答 3

4

我遇到了这个错误,并意识到我通过调用我想要测试的帮助方法来触发控制器上的重定向,但我实际上还没有实例化测试请求。在调用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
于 2013-06-17T19:22:10.227 回答
1

如果你想检查动作机制,你应该在这样的调用should_receive之前使用send

it "should redirect to signin_path" do
  controller.should_receive(:redirect_to).with(signin_path)
  controller.send(:require_signin) 
end
于 2013-10-07T11:02:35.790 回答
0

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

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

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

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

我刚刚在设计中添加了该:confirmable选项,并意识到我创建的所有尝试登录的用户都未确认,因此无法成功登录。我需要为confirmed_at Time.now用户添加到我的工厂/夹具创建中。不过,在您的示例中,您似乎正在尝试在未登录时进行测试,所以我不确定这是否一定适用。

于 2012-09-10T22:11:13.217 回答