Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The easy way is to just do this:</p> <pre><code>helper.params = {:foo =&gt; '1', :bar =&gt; '2'} </code></pre> <p>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 <strong>integrate_views</strong>. Then you can specify your params to the <strong>get</strong>, and test that the entire flow works, from sending params to the controller, to having them processed by the controller, and finally to rendering.</p> <p>I also generally prefer to pull out view logic into helpers, which can be easier to test.</p> <p>For instance, say I have a helper called <strong>selection_list</strong>, which returns a Hash whose "selected_preset" key relies on <strong>params[:selected_preset]</strong>, and defaults to 42 if an empty value is specified for the param.</p> <p>Here's a controller test where we've called <strong>integrate_views</strong> (you could of course do the same thing with an actual view test, if you're into that).</p> <pre><code>describe '#show' do describe 'selected_preset' do it 'should default to 42 if no value was entered' do get :show, :params =&gt; {:selected_preset =&gt; ''} response.template.selection_list[:selected_preset].should == 42 </code></pre> <p>This integration test will alert me if some part of this functionality breaks. But I also would ideally like to have some <em>unit</em> tests to help me <em>pinpoint</em> that breakage.</p> <p>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:</p> <pre><code>describe '#show' do describe 'selected_preset' do it 'should default to 42 if no value was entered' do get :show, :params =&gt; {:selected_preset =&gt; ''} assigns[:selected_preset].should == 42 # check instance variable is set response.template.selection_list[:selected_preset].should == 42 </code></pre> <p>Now I also can easily perform a helper unit test:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload