2011-02-12 6 views
4

필자는 Rails 3 도우미가 있는데, params와 request.headers에 따라 코드를 반환합니다. 나는 그들을 조롱하려했지만 성공하지 못했습니다. 내 최신 시도 :rspec2 : 도우미 용 가짜 매개 변수와 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)</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') 
+0

당신이 전체를 보여줄 수를 투기? – zetetic

+0

@ zetetic : 코드를 업데이트했습니다. – iGEL

답변

4

helper.banner

banner을 변경해보십시오

메소드 이름을 단독으로 사용할 때

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

그러나 helper에서 전화 할 때입니다 : 상황이다

self.class # => ActionView::Base 

helper이 RSpec에 :: 레일 :: 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 
+1

감사합니다. 이제 controller.params = {: controller => "사전", : action => "index"}'및'controller.request.stub (: 헤더) {{ "Accept-Language"= " DE, kr "}}'. 매개 변수를 모방하고 요청할 필요가 없습니다. :) – iGEL

관련 문제