[rspec-users] (no subject)

Matt Wynne matt at mattwynne.net
Fri Feb 27 11:46:09 EST 2009

On 27 Feb 2009, at 16:14, Marcus Roberts wrote:

> Apologies as this feels like an FAQ, but the only answer I can find  
> refers to a bug in a much earlier version of rspec, and this feels  
> like a common thing to do, so I suspect we're doing something stupid.
> The issue seems to be that if we mock a class, that mock carries  
> between specs when running 'rake spec' - the specs pass when run  
> individually.
> In one spec, we mock an active record model:
> MachineInstance = mock("MachineInstance")
 From this code above, you're setting a constant, 'MachineInstance'.  
Ruby will keep that constant for subsequent test runs - nothing to do  
with RSpec.

Assuming you want to mock an *instance* of your MachineInstance class,  
use this in your spec:

machine_instance = mock("MachineInstance")

AFAIK Ruby uses the case of the first letter to determine whether  
something's a (global) constant or a (local) variable. You want the  
latter, I think, so use lower case letters.

If what you want to do is mock the class itself, that's a different  
story, and we can talk about that.

Matt Wynne

More information about the rspec-users mailing list