Stub rails association.sum?

91 views
Skip to first unread message

James Silberbauer

unread,
Nov 20, 2009, 12:11:36 PM11/20/09
to mocha-developer
Hi

What would be the correct way in a rails test to stub
model.association.sum(:attribute) ?

eg. if the code to be tested was @order.items.sum(:cost),
do something like: @order.items.stubs(:sum).returns(50.0)

This did work for me with rails 2.2.2 but doesn't work with 2.3.4.

David Chelimsky

unread,
Nov 23, 2009, 7:53:47 AM11/23/09
to mocha-d...@googlegroups.com
My guess is that the items object you're stubbing on is not the same items object that receives sum. Try inspecting the object ID of @order.items in your test and in the code that calls sum and see if they're the same.

HTH,
David

James Silberbauer

unread,
Nov 23, 2009, 9:25:21 AM11/23/09
to mocha-developer
Thanks for the help. The object ids are the same, though.

I've added the equivalent of this to the test (my models are not
orders & items):
@order.items << Item.make(:order => @order)
@order.items.stubs(:sum).returns(50.0)
puts @order.items.sum(:cost).to_s
and the output is 0.0

James

On Nov 23, 2:53 pm, David Chelimsky <dchelim...@gmail.com> wrote:
> On Fri, Nov 20, 2009 at 11:11 AM, James Silberbauer
> <james...@telkomsa.net>wrote:

James Mead

unread,
Nov 23, 2009, 10:31:19 AM11/23/09
to mocha-d...@googlegroups.com
I don't think object ids are much help with AR association proxies -
there's usually too much voodoo going on.

I just created a new Rails project using 2.3.3 and created a model
with a has_many association to another model.

I can successfully stub a method on the association proxy. This was
using Test::Unit on Ruby 1.8.6 with Mocha as a gem.

I'm just about to try the same on 2.3.4.

James: Can you give us a few more details...

- what version of Ruby
- which test framework and what version
- how are you loading the test framework
- what version of Mocha
- how are you using Mocha - gem, plugin
- where or how are you loading Mocha

Thanks, James.


2009/11/23 James Silberbauer <jame...@telkomsa.net>:

James Mead

unread,
Nov 23, 2009, 10:55:35 AM11/23/09
to mocha-d...@googlegroups.com
I've now tried it with Rails 2.3.4 and it seems to work fine [1].

Could you try doing the same i.e. creating a new blank Rails project
with two models like this and seeing if you still have the same
problem?

Cheers, James.

[1] https://gist.github.com/0c9d98a66dd94eb17167


2009/11/23 James Mead <james...@gmail.com>:
Message has been deleted

James Silberbauer

unread,
Nov 23, 2009, 3:07:43 PM11/23/09
to mocha-developer
Thanks James,

I created a new project (mocha 0.9.8 and test/unit) and it failed too:
http://gist.github.com/241313
When I looked for differences between your code and mine to eliminate,
and commented out config.gem "mocha" in config/environment.rb, the
test passed.

I've done the same change to the real project but that hasn't made a
difference.
At least I have something to investigate though.

Other differences:
Ruby 1.8.7
I'm using Shoulda & Faker gems
And Machinist plugin
But these last were not used in the blank project.

James

On Nov 23, 5:55 pm, James Mead <jamesmea...@gmail.com> wrote:
> I've now tried it with Rails 2.3.4 and it seems to work fine [1].
>
> Could you try doing the same i.e. creating a new blank Rails project
> with two models like this and seeing if you still have the same
> problem?
>
> Cheers, James.
>
> [1]https://gist.github.com/0c9d98a66dd94eb17167
>
> 2009/11/23 James Mead <jamesmea...@gmail.com>:

James Mead

unread,
Nov 23, 2009, 4:16:36 PM11/23/09
to mocha-d...@googlegroups.com
Can you give more details of your environment.rb file i.e. is Shoulda
being loaded in that file? If so, does it appear before or after the
config.gem "mocha" line? What version of Shoulda are you using? Also
are you *explicitly* loading Test::Unit in environment.rb? When I have
some time I'll see if I can reproduce your problem. Cheers, James.


2009/11/23 James Silberbauer <jame...@telkomsa.net>:
> --
>
> You received this message because you are subscribed to the Google Groups "mocha-developer" group.
> To post to this group, send email to mocha-d...@googlegroups.com.
> To unsubscribe from this group, send email to mocha-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mocha-developer?hl=.
>
>
>

James Silberbauer

unread,
Nov 24, 2009, 2:39:13 AM11/24/09
to mocha-developer
This is a loading issue as you surmised.

My environment.rb had this:
config.gem "thoughtbot-shoulda", :lib => "shoulda", :source =>
"http://gems.github.com"
config.gem "faker"
config.gem "mocha"

which I have changed to this:
config.gem "thoughtbot-shoulda", :lib => false, :source => "http://
gems.github.com"
config.gem "faker", :lib => false
config.gem "mocha", :lib => false

Now the gems are not loaded from environment.rb and normality has been
restored to my tests.

Thank you for your help, James
James

On Nov 23, 11:16 pm, James Mead <jamesmea...@gmail.com> wrote:
> Can you give more details of your environment.rb file i.e. is Shoulda
> being loaded in that file? If so, does it appear before or after the
> config.gem "mocha" line? What version of Shoulda are you using? Also
> are you *explicitly* loading Test::Unit in environment.rb? When I have
> some time I'll see if I can reproduce your problem. Cheers, James.
>
> 2009/11/23 James Silberbauer <james...@telkomsa.net>:

James Mead

unread,
Nov 24, 2009, 5:26:22 AM11/24/09
to mocha-d...@googlegroups.com
2009/11/24 James Silberbauer <jame...@telkomsa.net>:
> This is a loading issue as you surmised.
>
> My environment.rb had this:
>  config.gem "thoughtbot-shoulda", :lib => "shoulda", :source =>
> "http://gems.github.com"
>  config.gem "faker"
>  config.gem "mocha"
>
> which I have changed to this:
>  config.gem "thoughtbot-shoulda", :lib => false, :source => "http://
> gems.github.com"
>  config.gem "faker", :lib => false
>  config.gem "mocha", :lib => false
>
> Now the gems are not loaded from environment.rb and normality has been
> restored to my tests.
>
> Thank you for your help, James

Hi James,

Well done for figuring it out.

I've added a ticket [1] to add some debug logging which might help
diagnose problems like this in the future.

Cheers, James.

[1] http://floehopper.lighthouseapp.com/projects/22289/tickets/58
Reply all
Reply to author
Forward
0 new messages