# Testing with has\_many associations

**URL:** <https://www.ruby-forum.com/t/testing-with-has-many-associations/134236>\
**Category:** RSpec\
**Created:** [April 9, 2008, 7:25am UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236 "2008-04-09T07:25:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 9, 2008, 7:25am UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236/1 "2008-04-09T07:25:00Z")

</div>

Hi I’m trying to be good and practice full BDD on my current project,  
and don’t want to abandon it as I have previously (expediency triumphed  
unfortunately). So expect me to be making frequent ‘noob’ style posts…

My current issue is with testing assignation across a has\_many  
relationship. I’m aware I shouldn’t be testing the functionality of  
Rails, but this is behaviour of my code.

cart\_spec.rb:

describe Cart do  
before(:each) do  
@product = mock “Trousers”  
@product.stub!(:class).and\_return(“Product”)  
@product.stub!(:name).and\_return(“Brown Trousers”)  
@product.stub!(:price).and\_return(23.99)  
@cart = Cart.new  
end

it “should have 1 item after adding a Product” do  
@cart.add\_product(@product)  
@cart.items.should have(1).item  
end

it “should have 1 item but with quantity 2 after adding the same  
product twice” do  
@cart.add\_product(@product)  
@cart.add\_product(@product)  
@cart.items.should have(1).item  
end  
end

cart.rb:

class Cart \< ActiveRecord::Base

has\_many :items, :class\_name =\> “CartItem”, :dependent =\> :destroy

def add\_product(product)  
current = self.items.find\_by\_name(product.name)  
if current  
current.increment\_quantity  
else  
self.items \<\< CartItem.new\_from\_product(product)  
end  
end

end

fails with:

‘Cart should have 1 item but with quantity 2 after adding the same  
product twice’ FAILED expected 1 item, got 2

* * *

Can anyone explain to me what I’m missing?

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 9, 2008, 11:33am UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236/2 "2008-04-09T11:33:42Z")

</div>

On 9 Apr 2008, at 06:25, Andy C. wrote:

> current = self.items.find\_by\_name(product.name)

You’re finding by the CartItem name and passing the product name - it  
looks like that’s drawing a blank match when you’re expecting it not to.

How about a cart\_item\_spec.rb:

describe CartItem do  
it “should be created successfully from a new product” do  
CartItem.new\_from\_product(mock\_model(Product, :name =\> “name” ))  
CartItem.find\_by\_name(“name”).should\_not be\_nil  
end  
end

I’m guessing this spec will fail with your current code as I don’t  
think CartItem::new\_from\_product is working.

I’d also probably break down the specs differently and wrap the call  
to items and make the specs more contained:

class Cart  
def self.find\_items\_by\_name(name)  
items.find\_by\_name(name)  
end  
end

…and mock this method when testing add\_product:

it “should add the product when it doesn’t already exist in the cart” do  
@cart.should\_receive(:find\_items\_by\_name).with(“name”).and  
return(nil)  
@cart.add\_product(@product)  
@cart.items.should have(1).item  
end

it “should increment quantity when it does find a product” do  
@cart.should\_receive(:find\_items\_by\_name).with(“name”).and  
return(@product)  
@cart.add\_product(@product)  
@cart.items.should have(1).item  
end

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 9, 2008, 11:57am UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236/3 "2008-04-09T11:57:22Z")

</div>

Chris Parsons wrote:

> I’m guessing this spec will fail with your current code as I don’t  
> think CartItem::new\_from\_product is working.

Wow. I’ve never had code I hadn’t posted correctly debugged before. You  
were absolutely right, thanks! Seems my testing approach in that spec  
was somehow faking out the all the tests I made.

> I’d also probably break down the specs differently and wrap the call  
> to items and make the specs more contained:

I think this is where I’m going wrong, I keep letting my tests get too  
big. Even when I think they are small. Another good rule I’ve picked up  
from this is to always try and use less than two dots to abstract the  
models…

Big help thanks Chris.

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 9, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236/4 "2008-04-09T13:22:32Z")

</div>

Chris Parsons wrote:

> it “should increment quantity when it does find a product” do  
> @cart.should\_receive(:find\_items\_by\_name).with(“name”).and  
> return(@product)  
> @cart.add\_product(@product)  
> @cart.items.should have(1).item  
> end

My final solution for this was to write…

describe Cart do  
before(:each) do  
@product1 = mock\_model Product, :name =\> “Brown Trousers”, :price =\>  
23.99  
@product2 = mock\_model Product, :name =\> “Yellow Shirt”, :price =\>  
15.74  
@cart\_item1 = mock\_model CartItem, :name =\> “Brown Trousers”, :price  
=\> 23.99, :quantity =\> 1  
@cart = Cart.new  
end

it “should increment quantity when it does find a product” do  
@cart\_item1.should\_receive(:increment\_quantity).once.with(:no\_args).and\_return(2)  
@cart.should\_receive(:find\_item\_by\_name).twice.with(“Brown  
Trousers”).and\_return(nil, @cart\_item1)  
@cart.add\_product(@product1)  
@cart.add\_product(@product1)  
@cart.items.should have(1).item  
end  
end

Which seems to work. I’m right?

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 9, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/testing-with-has-many-associations/134236/5 "2008-04-09T15:26:54Z")

</div>

On 9 Apr 2008, at 12:22, Andy C. wrote:

> end
> 
> Which seems to work. I’m right?

Looks good. Don’t forget that with that extra mocking your coverage  
has dropped on CartItem so you’ll need to ensure that it’s correctly  
tested also.

Cheers  
Chris
