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

ruby-on-rails – 如何使用rspec测试content_for,第1张

概述我试图测试我的application_helper的yield_for方法,但我不知道最好的方法.我尝试了下面的代码,但得到以下错误: Failure/Error: self.stub(:content_for).with(:foo).and_return('bar') Stub :content_for received unexpected message :with with (:fo 我试图测试我的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) : defaultend

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'  endend
解决方法 尝试在助手上制作存根

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) : defaultend
总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何使用rspec测试content_for全部内容,希望文章能够帮你解决ruby-on-rails – 如何使用rspec测试content_for所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1269137.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存