views:

338

answers:

2

I am finding holes in my coverage because I have been mocking my models in controller examples. When I remove a model's method upon which a controller depends, I do not get a failure.

Coming from TDD in statically typed languages, I would always mock dependencies to the object under test that hit the database to increase speed. I would still get failures in the above example, since my mocks subclassed the original object. I am looking for best practices in a dynamic language.

Thanks.

UPDATE:

After getting a lot of conflicting opinions on this, it seems it boils down to which philosophy you buy into.

The Rspec community appears to embrace heavily stubbing dependencies to achieve isolation of the object under test. Acceptance tests (traditionally known as integration tests ;) are used to ensure your objects work with their runtime dependencies.

The shoulda / Test::Unit community appears to stay away from stubbing as much as possible. This allows your tests to confirm your object under test actually works with its dependencies.

This video summarizes this nicely: http://vimeo.com/3296561

+2  A: 

Yes, in your controller examples, mock your models. In your model examples, test your models.

Jonathan Julian
How do you solve the lack of coverage? I know I can do it with integration tests, but I feel like my unit test should also uncover these problems.
Lee
+1  A: 

If you're using Mocha, the following should do it.

Mocha::Configuration.prevent(:stubbing_non_existent_method)
Mike