[mocha-developer] Isn't it possible to stub / expect on :id ?

James Mead jamesmead44 at gmail.com
Wed Oct 25 17:59:19 EDT 2006


On 25/10/06, Francois Beausoleil <francois.beausoleil at gmail.com> wrote:
>
> Running this:
> @payout = stub_everything(:id => 141)
> Payout.stubs(:find).with(@payout.id).returns(@payout)
>
> Generates this warning:
> ./test/functional/payouts_controller_test.rb:22: warning: Object#id
> will be deprecated; use Object#object_id
>
> What am I missing ?  :id is a fairly frequent method to override in
> Rails-based applications.
>
> I'm using Mocha from svn://rubyforge.org/var/svn/mocha/trunk @ r70.
>

On 25/10/06, Francois Beausoleil <francois.beausoleil at gmail.com> wrote:

    Running this:
    @payout = stub_everything(:id => 141)
    Payout.stubs(:find).with(@payout.id).returns(@payout)

    Generates this warning:
    ./test/functional/payouts_controller_test.rb:22: warning: Object#id
    will be deprecated; use Object#object_id

    What am I missing ?  :id is a fairly frequent method to override in
    Rails-based applications.

    I'm using Mocha from svn://rubyforge.org/var/svn/mocha/trunk @ r70.


Hi Francois,

The reason you get this warning is that the mock object (@payout) inherits
Object methods like Object#id.This means that calls to @payout.id are
dispatched to the Object#id implementation and not to Mock#method_missing
which is what would normally return the stub result. You will see the same
warning if you do...

irb(main):001:0> Object.new.id
(irb):1: warning: Object#id will be deprecated; use Object#object_id
=> 179090

The reason Mock inherits from Object is that many of these methods can be
useful e.g. asserting that two references to mock objects are the same
requires the #eql? method.

I have been working on implementing a way of having a BlankSlate-type mock,
but am a bit undecided about the API. So far I have blank_mock() and
blank_stub() equivalents of mock() and stub(). But I haven't had any time to
spare for work on Mocha recently. Sorry.

To be honest we haven't run into this problem much at Reevoo. In your
example above, what's wrong with doing this instead...?

@payout = stub_everything()
Payout.stubs(:find).with(141).returns(@payout)

or...

@payout = Payout.new { |payout| payout.id = 141 }
Payout.stubs(:find).with(@payout.id).returns(@payout)

One other comment I would make, is that I rarely use the with() method with
a stubs() call. If it is important that the find() method is called with a
particular value, I would use expects() like this...

Payout.expects(:find).with(@payout.id).returns(@payout)

...on the other hand if the actual value is unimportant, I would use stubs()
without with()...

Payout.stubs(:find).returns(@payout)

Can you explain any more about what you are trying to test?

By the way, many thanks for your work on the Piston project.
-- 
James.
http://blog.floehopper.org
-- 
James.
http://blog.floehopper.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061025/4b45d2d4/attachment.html 


More information about the mocha-developer mailing list