4

我正在尝试测试我的 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
4

2 回答 2

1

Try making the stub on the helper

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 passing with this

def yield_for(content_sym, default = '')
  content_for(content_sym) ? content_for(content_sym) : default
end
于 2013-06-13T19:33:21.410 回答
1

我正在使用 rspec (3.1.0),它应该适用于:

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
于 2016-06-14T21:11:00.807 回答