rspec2: поддельные параметры и request.headers для помощников - PullRequest
4 голосов
/ 13 февраля 2011

У меня есть помощник Rails 3, который возвращает код в зависимости от params и request.headers.Я пытался издеваться над ними, но безуспешно.Моя последняя попытка:

require 'spec_helper'

describe ApplicationHelper, "#banner" do
  before do
    @b728 = Factory :banner, :code => '<div style="width: 728px">:(clickref)</div>', :width => 728, :height => 90
    @b160 = Factory :banner, :code => '<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('<div style="width: 728px">11+2+21+31+41</div>')
  end
end

Я все еще выдвигаю исключение:

NameError:
       undefined local variable or method `params' for #
     # ./app/helpers/application_helper.rb:7:in `banner'

Решение: Благодаря zetetic моя спецификация теперь выглядит так:


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')

1 Ответ

4 голосов
/ 13 февраля 2011

Попробуйте изменить banner на helper.banner

Вы заметите, что при использовании имени метода само по себе контекст:

self.class # => RSpec::Core::ExampleGroup::Nested_1

Но при звонке с helper это:

self.class # => ActionView::Base

helper определен в RSpec :: Rails :: HelperExampleGroup и делает это:

# Returns an instance of ActionView::Base with the helper being specified
# mixed in, along with any of the built-in rails helpers.
def helper
  _view.tap do |v|
    v.extend(ApplicationHelper) if defined?(ApplicationHelper)
    v.assign(view_assigns)
  end
end
...