2015-04-27 141 views
0

我想測試一下application_controller的私有方法redirect_to的正確路徑。下面是我試圖做的:如何在不提出請求的情況下測試redirect_to

describe 'application is complete' do 
    it 'redirects to the mortgage application show page' do 
     controller.send(:load_mortgage_application) 
     expects(controller).to receive(:redirect_to).with(mortgage_application_path(controller.locale, mortgage_application)) 
    end 
    end 

,但我得到的錯誤:

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

如何測試,而無需迴應?

//這工作:

it 'redirects to the mortgage application show page' do 
     controller.expects(:redirect_to).with(mortgage_application_path(controller.locale, mortgage_application)) 
     controller.send(:load_mortgage_application) 
    end 

回答

0

首先,我會假設你需要一個控制器規格範圍內,這樣你就可以,例如,使用get:load_mortgage_application。在這一點上,你可以這樣做:

response.should redirect_to '/scorecard' 

或較新的語法:

expect(response).to redirect_to '/scorecard' 
+0

的事情是,這是一個私有方法。我知道這不是測試實施的最佳實踐,但出於某種原因我想這樣做。在我發出請求之前,'response'是'nil',並且我沒有提出請求,因爲應用程序控制器沒有任何動作,只有'before_filter's。 – ciembor

+0

啊,以前沒聽說過。 – kddeisz

相關問題