How to test content_for using rspec

I am trying to test my application_helper yield_for method, but I do not know how to do this. I tried the code below, but getting the following error:

  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 
+4
source share
2 answers

Try making a stub on an assistant

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

Got your test with this

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

I am using rspec (3.1.0) and it should work with:

 describe '#yield_for' do it 'should fetch the yield' do helper.content_for(:foo, 'bar') expect(helper.yield_for(:foo)).to eq('bar') end end 
0
source

Source: https://habr.com/ru/post/1486088/


All Articles