Skip to content

Instantly share code, notes, and snippets.

@zombor
Created January 14, 2014 20:42
Show Gist options
  • Save zombor/8425298 to your computer and use it in GitHub Desktop.
Save zombor/8425298 to your computer and use it in GitHub Desktop.
class Foo
end
class Foobar < Foo
end
describe 'foo' do
it 'works weird' do
Foo.should_receive(:new)
Foobar.new
end
end
@myronmarston
Copy link

I'd expect that to pass. Here's why:

  • When you mock or stub a method rspec defines that method on the given object (Foo, in this case), so that it can respond to that message based on how you've configured it (e.g. and_return, and_raise, etc).
  • There's no definition of Foobar.new, so when Foobar receives the new message, it goes up the ancestor chain to find a definition.
  • Normally it would find Class#new, but because Foo.new exists now, it finds that first, and executes it.

I can see why this would be confusing, but hopefully that makes sense.

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