Reputation: 1670
I want to try to test if an object use an optimisation in a particular case. So I have this sort of code
class Bar
attr_reader :n
def initialize(n)
@n=n
end
def a
if @n <= 3
b1
else
b2
end
end
def b1
@n+=1
end
def b2
@n+=1 ##super fast addition
end
end
I try to write a rspec like that
bar=Bar.new(5)
allow(bar).to receive(:b2).and_call_original
bar.a
expect(bar).to receive(:b2).once
expect(bar.n).to eq 6
But it doesn't work... Do you kown is it possible ? and if yes how ?
Upvotes: 1
Views: 1201
Reputation: 2061
You should place receive
expectation before the method invocation. Also you can merge allow
and expect
into a single line:
bar = Bar.new(5)
expect(bar).to receive(:b2).once.and_call_original
bar.a
expect(bar.n).to eq 6
Upvotes: 4