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

ruby-on-rails – 如何使用rspec测试content_for

发布时间:2020-12-17 02:04:46 所属栏目:百科 来源:网络整理
导读:我试图测试我的application_helper的yield_for方法,但我不知道最好的方法.我尝试了下面的代码,但得到以下错误: Failure/Error: self.stub(:content_for).with(:foo).and_return('bar') Stub :content_for received unexpected message :with with (:foo) ap
我试图测试我的application_helper的yield_for方法,但我不知道最好的方法.我尝试了下面的代码,但得到以下错误:

Failure/Error: self.stub(:content_for).with(:foo).and_return('bar')
   Stub :content_for received unexpected message :with with (:foo)

application_helper.rb

def yield_for(content_sym,default = '')
  content_for?(:content_sym) ? content_for(content_sym) : default
end

application_helper_spec.rb

describe '#yield_for' do
  it 'should fetch the yield' do
    self.stub(:content_for).with(:foo).and_return('bar')
    helper.yield_for(:foo).should == 'bar'
  end
end

解决方法

尝试在助手上制作存根

it 'should fetch the yield' do
  helper.stub(:content_for).with(:foo).and_return('bar')
  helper.yield_for(:foo).should == 'bar'
end

你的测试通过这个

def yield_for(content_sym,default = '')
  content_for(content_sym) ? content_for(content_sym) : default
end

(编辑:李大同)

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

    推荐文章
      热点阅读