# Mocha and ActiveRecord

**URL:** <https://www.ruby-forum.com/t/mocha-and-activerecord/77459>\
**Category:** Ruby\
**Created:** [November 28, 2006, 3:45am UTC](https://www.ruby-forum.com/t/mocha-and-activerecord/77459 "2006-11-28T03:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 28, 2006, 3:45am UTC](https://www.ruby-forum.com/t/mocha-and-activerecord/77459/1 "2006-11-28T03:45:36Z")

</div>

Everyone:

I’ve been using Mocha, which I rather love, to help me test-drive some  
Rails code. I’ve run up against something a little clunky, and I’m  
hoping one of you can help me out.

Suppose I have an Order, which has\_many OrderItems. Suppose I want to  
write a test that checks that when I ask the Order to do something, it  
asks its order items to do that something. I tried to do this like so:

def test\_delegates  
order\_items = []  
3.times {  
item = mock(“order item”, :do\_something =\> nil)  
order\_items.push(item)  
}

```
 order = Order.new(:order_items => order_items)
 order.do_something

```

end

This works with normal objects, but it seems ActiveRecord objects don’t  
like someone passing them Mocha::Mock objects in their constructors.  
They check that the order items, in this case, are OrderItem objects.

Is there something in ActiveRecord I can override to relax this  
behavior? I don’t like having to create an OrderItem just to add a  
single stub or expectation.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 28, 2006, 10:28am UTC](https://www.ruby-forum.com/t/mocha-and-activerecord/77459/2 "2006-11-28T10:28:24Z")

</div>

On 28/11/06, J. B. Rainsberger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> }
> 
> ```
> 
> behavior? I don’t like having to create an OrderItem just to add a  
> single stub or expectation.

What we tend to do in this case is stub the collection method itself  
i.e.  
Order#order\_items.

So you could do something like this…

def test\_should\_do\_something\_to\_all\_order\_items  
order\_items = Array.new(3) { mock(‘order item’, :do\_something =\> nil)  
}  
order = Order.new  
order.stubs(:order\_items).returns(order\_items)

order.do\_something  
end

When I have a spare couple of hours I’ll be releasing a new version of  
Mocha  
that supports mocking of the is\_a?. This will allow you to make this  
work  
with ActiveRecord, but with the disadvantage that you are coupling your  
test  
to the innards of ActiveRecord.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 28, 2006, 10:44am UTC](https://www.ruby-forum.com/t/mocha-and-activerecord/77459/3 "2006-11-28T10:44:25Z")

</div>

BTW - it’s probably better to use the Mocha mailing list -  
[http://rubyforge.org/mailman/listinfo/mocha-developer](http://rubyforge.org/mailman/listinfo/mocha-developer)

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [December 2, 2006, 3:50am UTC](https://www.ruby-forum.com/t/mocha-and-activerecord/77459/4 "2006-12-02T03:50:30Z")

</div>

James M. wrote:

> > ```
> > order_items.push(item)
> > 
> > ```
> > 
> > Is there something in ActiveRecord I can override to relax this  
> > order\_items = Array.new(3) { mock(‘order item’, :do\_something =\> nil) }  
> > test  
> > to the innards of ActiveRecord.
> 
> I hope that helps.

That’s not bad. I think I’d rather stub the collection than sorry about  
is\_a?. I’ll give this a shot and let you know how it goes.

Thank you, James.
