필자는 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')
당신이 전체를 보여줄 수를 투기? – zetetic
@ zetetic : 코드를 업데이트했습니다. – iGEL