2012-02-03 3 views
2

コントローラーやページの動作を別々のテストとしてテストしたいのですが、テストをスピードアップしますが、コントローラーやcapybaraページを複数回実行するだけで1回ロードするだけです。一例として、コントローラ試験で:RSpecで1つのコントローラ負荷またはCapyparaページの負荷に対して複数のテストを実行するにはどうすればよいですか?

 it "should include all videos in the list of all videos" do 
     get :show, id: event.id 
     response.should be_somehow 
     end 

     it "should set the main video to be the paid video" do 
     get :show, id: event.id 
     response.should be_somehow_else 
     end 

私はこのなりたい:

 before :all do 
     get :show, id: event.id 
     end 

     it "should include all videos in the list of all videos" do 
     response.should be_somehow 
     end 

     it "should set the main video to be the paid video" do 
     response.should be_somehow_else 
     end 

問題はRSpecの応答オブジェクト(または、カピバラのために、ページオブジェクトをきれいにすることです)。だから私は、コントローラのテスト、responseオブジェクト、またはカピバラ中page結果でassignsをチェックしてるかどうか、この作品のようなもの:

before :all do 
    get :show, id: event.id 
    @response = response 
end 
example "it should do something" do 
    @response.should be_somehow # test works 
end 
example "it should do something else" do 
    @response.should be_somehow_else # test fails; @response has been flushed by Rails testing facilities 
end 

だから、テストのスピードアップのためのソリューションは、単一のテストで複数のチェックを持っているではありません。

example "it should be totally correct in every way" do 
    get :show, id: event.id 
    response.should be_somehow # test works 
    response.should be_somehow_else # test works 
end 

しかし、これはテストの命名に関する私の感性を悪化させます。

これは、私が多段階セットアップ(ログイン、許可)プロセスを持つかもしれないCapybaraではるかに悪化しており、1ページの負荷をチェックする15のことがあります。他の醜いものは見せなかったのですか? javascriptのアクションは正しいものに縛られましたか?右のバックボーンテンプレートはレンダリングされましたか?これらのテストはすぐにRubyのコメントインラインでshould文の20の連続した行になるので、私がテストしていることを思い出すことができます。

テスト状態をテストからテストまで維持しないように指示する前に、それは私がやっていることではありません。単一の状態に関連付けられた独立変数をチェックしたいと思います。

おかげ

答えて

0

これは、画像を入力するVCRのための優秀な時間のように思えます。例えば:

before :all do 
    event = Event.create 
    VCR.use_cassette('show_event') do 
    make_http_request(:get, "http://localhost:3000/event/#{event.id}") 
    make_http_request(:get, "http://localhost:3000/#{event.id}") 
    end 
end 

it "should include all videos in the list of all videos" do 
    use_vcr_cassette "show_event" 
    response.should be_somehow 
end 

it "should set the main video to be the paid video" do 
    use_vcr_cassette "show_event" 
    response.should be_somehow_else 
end 

これは、テストの(ない各)全ての前にリクエストを作りますがYAMLに保存し、実行され、その後、各テストのためのYAMLで選びます。

これにより、オーバーヘッドが削減され、[ラック/環境]の代わりにIOバウンドに変更されます。

+0

これは私のサイトの出力を一度記録してから変更しないでしょうか? – bhuga

+0

VCRは実際に多くの方法を記録できます。はい、あなたが望むなら、それを行うことができます、または毎回それを行うことができます。または一度青い月ごとに! – krainboltgreene

+0

これはlocalhostの場合とは違います。 VCRはlocalhost上ではまったく動作しないか、完全にオフになります。私はこれが動作するとは思わない:/ – bhuga

関連する問題