Skip to content

Instantly share code, notes, and snippets.

@fables-tales
Created October 21, 2014 09:29
Show Gist options
  • Save fables-tales/9da4777150cf958ba5b9 to your computer and use it in GitHub Desktop.
Save fables-tales/9da4777150cf958ba5b9 to your computer and use it in GitHub Desktop.
def bar
puts "welp"
end
def foo
bar
end
RSpec.describe "foo" do
it "calls bar" do
expect(self).to receive(:bar)
foo
end
end
@ntijoh-daniel-berg
Copy link

Hey, thanks! it worked.
I had tried the following:

s = double(self)
average(numbers: [3,5,2])
expect(s).to have_received(:sum).with(numbers: [3,5,2])

But that failed with
Failure/Error: expect(s).to have_received(:sum).with(numbers: [3,5,2]) Double #<RSpec::ExampleGroups::Average:0x007fa281b1dbd8 @__memoized=nil> expected to have received sum, but that object is not a spy or method has not been stubbed.

@fables-tales
Copy link
Author

@itgsoddabe the double is a different object, so it doesn't receive anything. If you want to use the have_received style of expectation you'd do this:

allow(self).to receive(:sum)
average(numbers: [3,5,2])
expect(self).to have_received(:sum).with (numbers: [3,5,2])

If you do want to use the double you could also do this:

s = double()
s.average(numbers: [3,5,2])
expect(s).to have_received(:sum).with (numbers: [3,5,2])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment