I'm trying to stub out a global function called from a constructor. I'm using Rspec and Rspec-mocks. Here's the code to be tested:
def foo
'foo'
end
class Bar
def initialize
@bar = foo
end
def bar
@bar
end
end
And here's the test:
describe 'bar'
it 'calls foo' do
expect(self).to receive(:foo) { 'bar' }
expect(Bar.new.bar).to eq('bar')
end
end
The test fails with the following message:
Failure/Error: expect(Bar.new.bar).to eq('bar')
expected: "bar"
got: "foo"
(compared using ==)
How can I stub out the global functionfoo
?
PS: expect(self).to receive(:foo) { 'bar' }
works as expected if foo
is being called from another global function.