RSpec View testing: How to modify params?

后端 未结 4 560
清歌不尽
清歌不尽 2020-12-31 07:31

I am trying to test my views with RSpec. The particular view that is causing me troubles changes its appearance depending on a url parameter:

link_to \"sort>

4条回答
  •  离开以前
    2020-12-31 08:01

    The easy way is to just do this:

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

    But in general it's better to be more integration-y and not "stub" values when it's feasible. So I prefer to use controller tests with integrate_views. Then you can specify your params to the get, and test that the entire flow works, from sending params to the controller, to having them processed by the controller, and finally to rendering.

    I also generally prefer to pull out view logic into helpers, which can be easier to test.

    For instance, say I have a helper called selection_list, which returns a Hash whose "selected_preset" key relies on params[:selected_preset], and defaults to 42 if an empty value is specified for the param.

    Here's a controller test where we've called integrate_views (you could of course do the same thing with an actual view test, if you're into that).

    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
    

    This integration test will alert me if some part of this functionality breaks. But I also would ideally like to have some unit tests to help me pinpoint that breakage.

    I'll start by having the helper use an instance variable instead of directly accessing params. I'll change the above code by adding a single line directly below the get, as follows:

    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
    

    Now I also can easily perform a helper unit test:

    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
    

提交回复
热议问题