[rspec-users] Problems with mock assigned to a constant
mailing_lists at railsnewbie.com
Fri Jul 25 00:38:36 EDT 2008
On Jul 25, 2008, at 12:21 AM, Matt Lins wrote:
> 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:
> I defined the stubs in the before block:
> MyModel = mock('MyModel Class', :count => 1, :find => [@record])
Ah - well, I missed this part. This make much more sense.
Btw, aren't you seeing warnings every time you run your specs?
Redefining the constant for your test, is, IMHO, the most ugly
solution you can take (and plus, it'll break in many circumstances -
for instance, it probably wont' play well with rails loading
schemes). One way around this is by using dependency injection - I
would highly recommend you use this technique. *DON'T* use the
constant technique, unless you really know what your doing.
Unfortunately there are times when DI doesn't work (especially in the
rails world) - in those cases, you really have no other option besides
stubbing the class methods directly.
> 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 ?
Well - what is happening between each test case? Are the classes
(defined elsewhere) being reloaded each time? Are you getting a
warning when you redefine MyModel? Do you understand how rails is
(re)loading this stuff?
More information about the rspec-users