Mocha and ActiveRecord

Discussion in 'Ruby' started by J. B. Rainsberger, Nov 28, 2006.

  1. 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.
    --
    J. B. (Joe) Rainsberger :: http://www.jbrains.ca
    Your guide to software craftsmanship
    JUnit Recipes: Practical Methods for Programmer Testing
    2005 Gordon Pask Award for contribution Agile Software Practice
    J. B. Rainsberger, Nov 28, 2006
    #1
    1. Advertising

  2. James Mead wrote:
    > On 28/11/06, J. B. Rainsberger <> wrote:
    >>
    >> 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.
    >>

    >
    > 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.


    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.
    --
    J. B. (Joe) Rainsberger :: http://www.jbrains.ca
    Your guide to software craftsmanship
    JUnit Recipes: Practical Methods for Programmer Testing
    2005 Gordon Pask Award for contribution Agile Software Practice
    J. B. Rainsberger, Dec 2, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    13,462
  2. James Mead
    Replies:
    0
    Views:
    110
    James Mead
    Jul 19, 2006
  3. Joe Van Dyk

    Using Mocha to mock an object

    Joe Van Dyk, Oct 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    103
    Joe Van Dyk
    Oct 21, 2006
  4. Raymond O'Connor
    Replies:
    1
    Views:
    145
    Raymond O'Connor
    Mar 8, 2007
  5. Feng Tien
    Replies:
    2
    Views:
    114
    James Mead
    Nov 7, 2007
Loading...

Share This Page