[rspec-users] Testing has_many :through

Lourens Naude lourens at rentacarhire.com
Tue Dec 12 21:54:03 EST 2006


Instead of assigns[:member].projects.size.should_be 1 use assigns 
[:member].projects.count.should_be 1
AR associations is returned as enumerable (Array) where  
Enumerable#size wouldn't force a DB query with the recent association  
loading patches on Edge ( likely 1.2 as well ).

association.count forces roundtrip to the DB, effectively dealing  
with your line #2 in the process.

Just a thought, works fine for me with #count, been using Rspec since  
0.7.2 on Edge and most of these little quirks/errors occur at a  
higher level than Rspec itself.

- Lourens

On 2006/12/13, at 00:48, s.ross wrote:

> I'm working with a has_many :through association, where a Member
> has_many Projects through ProjectViewers. The problem I'm running
> into is that Rails doesn't load the association so line (3) fails
> until I explicitly access a member of the collection (in this case, I
> iterated it). Is there a better way?
> Thanks,
> Steve
> 1. assigns[:member].should_not_be_nil
> 2. assigns[:member].projects.each{|p| puts "project #{p.name}"}  #
> force association to be loaded
> 3. assigns[:member].projects.size.should_be 1
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list