[mocha-developer] expectations on stubs (stubba) or mock methods on existing classes?

John Wilger johnwilger at gmail.com
Thu Aug 17 10:06:10 EDT 2006


Hello,

First, thanks for releasing Mocha. As someone who's been practicing
TDD much longer than I've been using Rails, one of my biggest
complaints has always been the fact that the "Unit" tests are more
like intefration tests because of how tightly coupled the domain model
is when using ActiveRecord. Stubba looks like it could really help
aleviate this problem.

The one problem I'm having though, is that I want to be able to stub
out a method on an existing class _and_ use the mock expectations on
that method.

Basically, I have a Product class (ActiveRecord) that has a
class-method Product.import(data_hash). I want to test that this
method calls -- among other things -- Product.create with the correct
arguments.

I was trying to do:

    def test_import_should_create_product_when_it_does_not_exist
      Product.stubs( :create ).with( expected_arguments ).times( 1 )
      Product.import( import_line )
    end

I would expect that to either a) raise an exception because it's
invalid or b) fail the test since the implementation of Product.import
does not currently call Product.create. Instead, the test passes.

Any ideas how I can make this work as expected?

-- 
Regards,
John Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland


More information about the mocha-developer mailing list