加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rspec2:假助手和请求助手

发布时间:2020-12-17 02:28:26 所属栏目:百科 来源:网络整理
导读:我有一个Rails 3帮助器,它根据params和request.headers返回代码.我试图嘲笑他们,但没有成功.我的最新尝试: require 'spec_helper'describe ApplicationHelper,"#banner" do before do @b728 = Factory :banner,:code = 'div style="width: 728px":(clickref
我有一个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,: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,"left").should eq('11+2+21+31+41')

解决方法

尝试将横幅更改为helper.banner

您会注意到,当单独使用方法名时,上下文是:

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

但是当从帮助者那里打电话时:

self.class # => ActionView::Base

帮助程序在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读