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

James Mead jamesmead44 at gmail.com
Thu Aug 17 10:18:35 EDT 2006

On 17/08/06, John Wilger <johnwilger at gmail.com> wrote:
> 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.

Thanks. This was pretty much the reason for creating Mocha (and Stubba).

    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?

If you want to assert that "create" is called, all you need to do is change
"stubs" to "expects"...

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

As long as you are using the latest (0.2) version of Mocha, this expectation
should be auto-verified.

If not, just add a line at the end of the test: Product.verify

Expectations are verified, stubs are not. Does that make sense?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060817/b7f3350d/attachment.html 

More information about the mocha-developer mailing list