John Wilger
2006-Aug-17 14:06 UTC
[mocha-developer] expectations on stubs (stubba) or mock methods on existing classes?
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
James Mead
2006-Aug-17 14:18 UTC
[mocha-developer] expectations on stubs (stubba) or mock methods on existing classes?
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 ) end 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? James. http://blog.floehopper.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060817/b7f3350d/attachment.html