# Using mocks

**URL:** <https://www.ruby-forum.com/t/using-mocks/77745>\
**Category:** Rails\
**Created:** [December 27, 2005, 8:18pm UTC](https://www.ruby-forum.com/t/using-mocks/77745 "2005-12-27T20:18:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 27, 2005, 8:18pm UTC](https://www.ruby-forum.com/t/using-mocks/77745/1 "2005-12-27T20:18:35Z")

</div>

Hi,

What’s the purpose of test/mocks? I couldn’t quite complete the Wiki  
entry on this because all the information I _could_ find was in the  
readme.

- Rowan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 27, 2005, 9:12pm UTC](https://www.ruby-forum.com/t/using-mocks/77745/2 "2005-12-27T21:12:44Z")

</div>

As an example, let’s say you have a model called Order. Order has a  
method called authorize which when you run in production mode  
actually calls PayPal or some other credit agencies. But in  
development mode you want to mock this process. What you should do is  
to place order.rb file into your test/mocks/development directory and  
provide a mock implementation of the authorize method. For example:

require\_dependency ‘models/order’  
class Order  
def authorize  
set\_success(@total \> 500.dollars)  
end  
end

Kent.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 27, 2005, 9:39pm UTC](https://www.ruby-forum.com/t/using-mocks/77745/3 "2005-12-27T21:39:50Z")

</div>

Thank you for clearing this up for me.

Which mock library is mostly used for developing with Rails? I’m  
assuming [http://raa.ruby-lang.org/project/test-unit-mock](http://raa.ruby-lang.org/project/test-unit-mock), since that  
seems to be used by LoginGenerator, but since there are others, …

- Rowan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 28, 2005, 5:41am UTC](https://www.ruby-forum.com/t/using-mocks/77745/4 "2005-12-28T05:41:33Z")

</div>

> Rails doesn’t use a mock library as such - it just arranges for  
> source under test/mocks/ to be at the top of require()s search path.

Defining a dummy class in test/mocks/ doesn’t make it a mock. It  
means you’ve defined a stub in a poorly named directory.

FWIW, I enjoy using FlexMock in Rails. Install the gem, or copy  
flexmock.rb to lib/

-Steven.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 28, 2005, 12:37am UTC](https://www.ruby-forum.com/t/using-mocks/77745/5 "2005-12-28T00:37:53Z")

</div>

Rails doesn’t use a mock library as such - it just arranges for  
source under test/mocks/ to be at the top of require()s search path.

If you look at the code below, first the main Order class is loaded,  
then  
you just redefine the method you want to change (that’s the nice thing  
about  
Rubys open classes).

On 27/12/05, BigSmoke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which mock library is mostly used for developing with Rails? I’m  
> assuming [http://raa.ruby-lang.org/project/test-unit-mock](http://raa.ruby-lang.org/project/test-unit-mock), since that  
> seems to be used by LoginGenerator, but since there are others, …

> On 12/27/05, Kent S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > require\_dependency ‘models/order’  
> > class Order  
> > def authorize  
> > set\_success(@total \> 500.dollars)  
> > end  
> > end

–  
Rasputin :: Jack of All Trades - Master of Nuns  
[http://number9.hellooperator.net/](http://number9.hellooperator.net/)
