2016-08-04 2 views
2

私はこのコントローラーの仕様を超えています。それは私には何もテストしていないことは明らかです。しかし、response.status == 200の期待は失敗しません。だから、Rspecがコントローラテストをどのように構築するのか、そしてデフォルトのresponse.status == 200があるかどうかを理解したいと思います。 RSpec controller spec documents point outとしてアクションが渡されない場合のRspecコントローラテストのデフォルトの応答はありますか?

describe 'SomeController', type: :controller do 
    let!(:user){ FactoryGirl.create :admin_user } 
    before { sign_in user } 

    describe 'GET#action' do 
    it 'response is success' do 
     expect(response.status).to eq 200 
    end 
end 
end 




Finished in 0.93954 seconds (files took 1.63 seconds to load) 
1 example, 0 failures 

答えて

2

コントローラ仕様レール機能テスト (ActionController::TestCase::Behavior)用のRSpecのラッパーです。

ので、Special Instance Variables section下、ActionController::TestCase::Behavior用で、コードのドキュメントを見て、私たちは最後のHTTPの応答を表す、「ActionDispatch::TestResponse目的である、ActionController::TestCaseが自動的a @response instance variablereadable as just response in the test)を提供することを見ることができます応答"。だから、コントローラー仕様で明示的な要求をせずにアクセスできるresponseがある理由を説明しているようですが、なぜそれがステータス200ですか?

まあ、ActionDispatch::TestResponseActionDispatch::Responsewhen initialized provides 200 as the default status)を継承しています。あなたもあなたのレールコンソールでこれを試すことができます:それは確かに地雷をしたとして

> ActionDispatch::TestResponse.new 
=> #<ActionDispatch::TestResponse:0x007fc449789b68 
@blank=false, 
@cache_control={}, 
@charset=nil, 
@committed=false, 
@content_type=nil, 
@cv= 
    #<MonitorMixin::ConditionVariable:0x007fc449789848 
    @cond=#<Thread::ConditionVariable:0x007fc449789820>, 
    @monitor=#<ActionDispatch::TestResponse:0x007fc449789b68 ...>>, 
@etag=nil, 
@header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}, 
@mon_count=0, 
@mon_mutex=#<Thread::Mutex:0x007fc449789a50>, 
@mon_owner=nil, 
@sending=false, 
@sending_file=false, 
@sent=false, 
@status=200, # <<< Here's your default status. 
@stream=#<ActionDispatch::Response::Buffer:0x007fc449789938 @buf=[], @closed=false, @response=#<ActionDispatch::TestResponse:0x007fc449789b68 ...>>> 

だから、私は、RSpecのコントローラの仕様でresponseオブジェクトのご理解を支援し、この深いダイビングを願っています。

+0

偉大な答えポール。それはまさに私が探していたものです!ありがとう – vinibol12

関連する問題