2012-06-12 33 views
8

在我們的rails 3.1.4應用程序中,rspec用於測試應用程序控制器中的公共方法require_signin。下面是該方法require_signin:RunTimeError:ActionController :: RackDelegation in rspec 2.10.1 for rails 3.1.4應用程序控制器

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的代碼?非常感謝。

+0

添加一個賞金請。我也需要這個答案 – tekknolagi

回答

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."} 

我剛剛在Devise中添加了:confirmable選項,並意識到我創建的所有用戶都是未經證實的,因此無法成功登錄。我需要將confirmed_at Time.now添加到爲用戶創建的工廠/燈具中。在你的例子中,似乎你試圖在沒有登錄的時候測試,所以我不確定這是否適用。

3

我碰到這個errror並意識到我是通過調用一個輔助方法我想測試觸發控制器上的重定向,但我還沒有實際實例測試請求呢。在調用expectation之前調用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 
+0

感謝未來!這只是解決了我的問題。 – taylorthurlow

0

如果你想檢查作用機理,你應該在send調用之前使用should_receive這樣

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