[rspec-users] Problems with mock assigned to a constant

Matt Lins mattlins at gmail.com
Fri Jul 25 00:32:27 EDT 2008

I suppose the way I'm defining the stubs, differs from what Dave is
doing in his example.

I assumed that:

MyModel = mock('MyModel Class', :count => 1)

was the same as:


But, I'm starting to think they are not.  I haven't looked at the
rSpec internals to verify, other than the parameter name:

stubs_and_options+ lets you assign options and stub values
at the same time. The only option available is :null_object.
Anything else is treated as a stub value.

So, is this problem?


On Thu, Jul 24, 2008 at 11:21 PM, Matt Lins <mattlins at gmail.com> wrote:
> Scott,
> Thanks, your solution does work, although I'm not sure I like it.  I
> like to stub out behavior in my before block but also use mock
> expectations to verify behavior in my specs.  Similar to what Dave
> explains here:
> http://blog.davidchelimsky.net/2006/11/9/tutorial-rspec-stubs-and-mocks
> I defined the stubs in the before block:
> MyModel = mock('MyModel Class', :count => 1, :find => [@record])
> So, I'm assuming by adding the expectation in the first spec, I'm
> wiping out the stubs defined in the before block?  If that is the case
> why does the first spec not fail because of MyModel.find ?
> Also, FlexMock does not seem to behave this way(not that they need to
> behave the same), but I would question whether this behavior is
> intentional?
> -Matt
> On Thu, Jul 24, 2008 at 11:00 PM, Scott Taylor
> <mailing_lists at railsnewbie.com> wrote:
>> On Jul 24, 2008, at 11:49 PM, Matt Lins wrote:
>> Hi all,
>> Initially I thought this was a bug in the built-in mocking framework(and it still may be), but I better hash it out on the mailing list before I file/reopen the ticket:
>> http://rspec.lighthouseapp.com/projects/5645/tickets/478-mocks-on-constants#ticket-478-6
>> I thought my example illustrated my problem, but obviously I was passing the wrong arguments to the mock.  I revised my example to more clearly state my problem:
>> http://gist.github.com/2372
>> This is a snip of a some code from a library I'm writing.  When I run this spec I get the following:
>> # spec migration_spec.rb
>> .F
>> 1)
>> Spec::Mocks::MockExpectationError in 'Migration should find the records'
>> Mock 'MyModel Class' received unexpected message :count with (no args)
>> ./migration.rb:14:in `run'
>> ./migration_spec.rb:19:
>> Finished in 0.009164 seconds
>> 2 examples, 1 failure
>> ------------------------------------------
>> I want to mock out MyModel completely because it's an ActiveRecord object.  As suggested, if I define the MyModel class, this whole spec will pass.  But, I don't think I should need to do that, should I?  So it seems to only occur when I assign the mock to a constant.
>> So, even though MyModel.count is stubbed in the before block, the mock reports an unexpected message.  Note however the first example passes.
>> The first example passes because "should_receive" acts as a stub, too.
>> The second example fails, because in the second example there is no stub for the count method.  I'd suggest adding this line to before(:each) (or to the start of each test case):
>> MyModel.stub!(:count).and_return 0
>> Scott
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list