Forum: Ruby on Rails Using mocks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
BigSmoke (Guest)
on 2005-12-27 21:18
(Received via mailing list)
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
Kent S. (Guest)
on 2005-12-27 22:12
(Received via mailing list)
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.
BigSmoke (Guest)
on 2005-12-27 22:39
(Received via mailing list)
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
Dick D. (Guest)
on 2005-12-28 01:37
(Received via mailing list)
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/
Steven R. Baker (Guest)
on 2005-12-28 06:41
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.