2009-11-26 19 views
10

RSpecでビューをテストしようとしています。私はトラブルの原因となっている特定のビューには、URLパラメータに応じて、その外観を変更します。RSpecビューテスト:パラメータを変更するには?

<% if params[:sort_by] == 'name' %> 
<div>Sorted by Name</div> 
<% end %> 

をRSpecのは、次のようになります。私の見解は、そのように、このパラメータを使用していますhttp://mydomain/model?sort_by=name

になり

link_to "sort>name", model_path(:sort_by => 'name')をこれは:

it "should tell the user the attribute for sorting order" do 
    #Problem: assign params[:sort_for] = 'name' 
    render "/groups/index.html.erb" 
    response.should have_tag("div", "Sorted by Name") 
end 

RSpecで私のビュー(コントローラなし)をテストしたいと思いますが、このパラメータを取得できません私のparams変数に入れてください。これまで

  • assign[:params][:sort_by] = 'name'
    • assign[:params] = {:sort_by => 'name'}
    • ...

    いいえ成功:私はすべての異なる味でassignを試してみました。すべてのアイデアは高く評価されます。

  • 答えて

    10

    あなたの意見にparamsを使用しないでください。
    ヘルパーを使用するのに最適な方法です。

    <div>Sorted by <%= sorted_by %></div> 
    

    そしてヘルパーのテストで、あなたはparams要求を定義することができますので、あなたのヘルパーファイルの1

    def sorted_by 
        params[:sorted_by].capitalize 
    end 
    

    に続いて、あなたは(非常に簡単にあなたのヘルパーをテストすることができます。

    +0

    私が期待したものではありませんが、問題を解決してアプリケーションを改善しました。ありがとう! – sebastiangeiger

    33

    をそれのコントローラーの場合テストすればそれは

    controller.stub!(:params).and_return {} 
    

    です。そのヘルパーテストなら、それはb E:

    helper.stub!(:params).and_return {} 
    

    そして、そのビューのテストでは、それは次のようになります。

    view.stub!(:params).and_return {} 
    

    あなたは以下のような警告を受ける場合。

    Deprecation Warnings: 
    
    Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/akbarbin/Documents/Office/projects/portfolio/spec/views/admin/waste_places/new.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'. 
    
    
    If you need more of the backtrace for any of these deprecations to 
    identify where to make the necessary changes, you can configure 
    `config.raise_errors_for_deprecations!`, and it will turn the 
    deprecation warnings into errors, giving you the full backtrace. 
    
    1 deprecation warning total 
    
    Finished in 4.86 seconds (files took 4.72 seconds to load) 
    

    あなたは簡単な方法はこれだけやっている

    allow(view).to receive(:params).and_return({sort_by: 'name'}) 
    
    +0

    これは役に立ちましたが、私はRspec 3の構文を使用しました: 'allow(view).to receive(:params).and_return({action: 'index'})' –

    5

    にそれを変更することができます。

    helper.params = {:foo => '1', :bar => '2'} 
    

    をしかし、一般的に、それはより多くの統合-yのではなく、「スタブする方が良いでしょう"実現可能な時の値。だから私はintegrate_viewsでコントローラテストを使用することを好む。次に、にパラメータを指定してを取得し、フロー全体がコントローラーに送信されてからコントローラーによって処理され、最後にレンダリングされるまでテストされることをテストできます。

    また、私は一般にビューロジックをヘルパーに引き出す方が好きです。これはテストが簡単です。空の値がPARAMのために指定されている場合は42から、およびデフォルト値:[selected_preset】例えば

    は、私が「selected_preset」キーのparamsに依存しているハッシュを返すselection_listと呼ばれるヘルパーを、持っていると言います。

    コントローラテストではintegrate_viewsと呼んでいます(実際には、実際の表示テストで同じことを行うことができます)。

    describe '#show' do 
        describe 'selected_preset' do 
        it 'should default to 42 if no value was entered' do 
         get :show, :params => {:selected_preset => ''} 
         response.template.selection_list[:selected_preset].should == 42 
    

    この統合テストでは、この機能の一部が壊れているかどうかがわかります。しかし、私も理想的にはユニットテストを持ってほしいと思っていますピンポイントその破損。

    私は、ヘルパーがparamsに直接アクセスする代わりにインスタンス変数を使用するようにすることから始めます。次のように私は、直接取得する以下の一行を追加することによって、上記のコードを変更します:

    describe '#show' do 
        describe 'selected_preset' do 
        it 'should default to 42 if no value was entered' do 
         get :show, :params => {:selected_preset => ''} 
         assigns[:selected_preset].should == 42 # check instance variable is set 
         response.template.selection_list[:selected_preset].should == 42 
    

    を今、私はまた、簡単にヘルパーユニットテストを実行することができます。

    describe MyHelper do 
        describe '#selection_list' do 
        it 'should include the selected preset' do 
         assigns[:selected_preset] = 3 
         helper.selection_list[:selected_preset].should == 3 
    
    +0

    "integrate_views"は廃止されました"render_views"のhttps://relishapp.com/rspec/rspec-rails/v/2-1/docs/controller-specs/render-views –

    1

    ビューを設定する別の方法params:

    controller.request.path_parameters[:some_param] = 'a value' 
    
    関連する問題