Using Mocha to test Rails associations, and association dependencies

Posted by nick.stielau on August 08, 2007

I just started using Mocha to mock and stub some of my rails tests, after finding out first-hand that fixtures suck and indeed need fixin'. Here's what I like about Mocha:

  • Easy to use
  • Easy to read
  • Decreases the need for complexly dependant fixtures
  • Speeds up tests once fixtures are removed

Not only that, but Mocha puts some constraints on your tests that force you to be a better tester. If you mock and stub instead of using fixtures, you need to build your tests up to a complete test from an empty mock, rather than whittling them down from a full Ruby object with lots of data. For example,

RUBY:
  1. def test_items_should_return_uniq_set_of_all_rentals_items
  2.  
  3. bob = Renter.new
  4.  
  5. bob.expects(:rentals).returns([stub(:items => [:item_1, :item_2, :item_3]),
  6.  
  7. stub(:items => [:item_3, :item_4, :item_5])])
  8.  
  9. assert_equal bob.items, [:item_1, :item_2, :item_3, :item_4, :item_5]
  10.  
  11. end

Before I might have been tempted to play with the +Item+ objects, but with Mocha, they are just symbols, so the test is more transparent.

I am still grappling with some aspects of Mocha and testing in general. Like custom SQL. If you want to test your custom SQL, you need data in the database at some point. Its seems like the higher level stuff is better for mocking, and the lower level stuff gets a little trickier. Here is how I tackled testing a ActiveRecord association, and its dependency:

RUBY:
  1. require File.dirname(__FILE__) + '/../test_helper'
  2.  
  3. class RenterTest <Test::Unit::TestCase
  4. def test_rental_association
  5. bob = Renter.new
  6. bob.rentals <<Rental.new
  7. assert !bob.rentals.empty?
  8. end
  9.  
  10. def test_rental_dependency
  11. bob = Renter.new
  12. bob.rentals <<Rental.new
  13. bob.rentals.first.expects(:destroy)
  14. bob.destroy
  15. end
  16. end

Railscasts can get you started.

technorati tags:, , , , ,

Blogged with Flock

Comments

Leave a response

Comments