Note that there are some explanatory texts on larger screens.

plurals
  1. POrspec2: Fake params and request.headers for helpers
    text
    copied!<p>I've got an Rails 3 helper, which returns code depending on the params and request.headers. I've tried to mock them, but didn't succeed. My latest try:</p> <pre><code>require 'spec_helper' describe ApplicationHelper, "#banner" do before do @b728 = Factory :banner, :code => '&lt;div style="width: 728px">:(clickref)&lt;/div>', :width => 728, :height => 90 @b160 = Factory :banner, :code => '&lt;div style="width: 160px">:(clickref)&lt/div>', :width => 160, :height => 600 Factory :ad_sense_channel, :key => "country", :value => nil, :ad_sense_id => 1 Factory :ad_sense_channel, :key => "country", :value => "de", :ad_sense_id => 2 # More ad_sense_channel factories here... end it "should return b728 for 728x90, left position and DictionariesController, German language and a guest and a German IP" do helper.stub(:params) { {:controller => "dictionaries", :action => "index"} } helper.stub(:request) do request = double('request') request.stub(:headers) { {"Accept-Language" => "de-DE, kr"} } request end @detected_location = DetectedLocation.new("193.99.144.80") banner(728, 90, "left").should eq('&lt;div style="width: 728px">11+2+21+31+41&lt;/div>') end end </code></pre> <p>I still raises an exception:</p> <pre><code>NameError: undefined local variable or method `params' for # # ./app/helpers/application_helper.rb:7:in `banner'</code></pre> <p>Solution: Thanks to zetetic, my spec now reads like this:</p> <pre><code> controller.params = {:controller => "dictionaries", :action => "index"} controller.request.stub(:headers) { {"Accept-Language" => "de-DE, kr"} } @detected_location = DetectedLocation.new("193.99.144.80") helper.banner(728, 90, "left").should eq('11+2+21+31+41') </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