Using mocks


#1

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

#2

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.


#3

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, since that
seems to be used by LoginGenerator, but since there are others, …

  • Rowan

#4

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.


#5

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 wrote:

Which mock library is mostly used for developing with Rails? I’m
assuming 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 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/