[rspec-users] Class mocking
matt at mattwynne.net
Fri Feb 27 12:18:19 EST 2009
On 27 Feb 2009, at 17:08, Marcus Roberts wrote:
> >Unless RSpec is doing some magic under the hood that I'm not aware
> >this is expected behavior - you're reassigning the constant value of
> >What you really want to do is mock/stub directly on
> MacineInstance. e.g.
> >MachineInstance.should_receive(:create).and_return @mock_machine
> If we did that, would that stub/mock carry over into other spec
> files run later by autospec, which is what we're seeing.
> I can see we're changing the definition of MachineInstance in the
> spec we do that in, but should that carry over to later specs?
That's the way Ruby behaves, yes.
Once you assign the constant MachineInstance to point to your mock,
Rails' magic auto-file-loading which normally happens when Ruby fires
a 'missing constant error' will never run, so your real class will
never get loaded.
Assigning constants in your tests is a pretty bad idea unless you know
exactly what you're doing.
More information about the rspec-users