rspec2:助手的假 params 和 request.headers
我有一个 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)</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')
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:
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
I still raises an exception:
NameError:
undefined local variable or method `params' for #
# ./app/helpers/application_helper.rb:7:in `banner'
Solution: Thanks to zetetic, my spec now reads like this:
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
banner
更改为helper.banner
您会注意到,当单独使用方法名称时,上下文是:
但是当从
helper
调用时,它是:helper
在 RSpec::Rails::HelperExampleGroup 中定义,并执行以下操作:Try changing
banner
tohelper.banner
You'll note that when using the method name by itself the context is:
But when calling from
helper
it's:helper
is defined in RSpec::Rails::HelperExampleGroup and does this: