[rspec-users] (no subject)

Pat Maddox pat.maddox at gmail.com
Fri Feb 27 11:44:18 EST 2009


On Fri, Feb 27, 2009 at 8:14 AM, Marcus Roberts
<M.Roberts at nittygritty.net> 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")
>
> in a spec in another file that runs later, when we access MachineInstance as
> what should be a regular activerecord class, it's still a mock:
>
> Spec::Mocks::MockExpectationError in 'MachineInstance should create a new
> instance given valid attributes'
> Mock 'MachineInstance' received unexpected message :create! with
> ({:name=>"value for name", :machine_image_id=>1})
>
> We're running rspec 1.1.12.
>
> Thanks for any pointers.
>
> Marcus
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

Unless RSpec is doing some magic under the hood that I'm not aware of,
this is expected behavior - you're reassigning the constant value of
MachineInstance.

What you really want to do is mock/stub directly on MacineInstance.  e.g.

MachineInstance.should_receive(:create).and_return @mock_machine

Pat


More information about the rspec-users mailing list