Mocks and stubs in Test::Unit when you are used to RSpec

Posted About 13 years ago. Visible to the public.

We are maintaining some vintage projects with tests written in Test::Unit instead of RSpec. Mocks and stubs are not features of Test::Unit, but you can use the Mocha Show archive.org snapshot gem to add those facilities.

The following is a quick crash course to using mocks and stubs in Mocha, written for RSpec users:

|---------------------------------------------------------|
| RSpec | Mocha |
|---------------------------------------------------------|
| obj = double() | obj = mock() |
| obj.stub(:method => 'value') | obj.stubs(:method).returns('value') |
| obj.should_receive(:method).with('argument').and_return('value') | obj.expects(:method).with('argument').returns('value') |
| obj.should_not_receive(:method) | obj.expects(:method).never |
| obj.should_receive(:method).once | obj.expects(:method).once |
| obj.should_receive(:method).with('argument', anything) | obj.expects(:method).with('argument', anything) |
| obj.should_receive(:method).with(hash_including(:key => 'value')) | obj.expects(:method).with(has_entries(:key => 'value')) |
|---------------------------------------------------------|

Henning Koch
Last edit
Over 11 years ago
Keywords
mock, mocking, mocks, stub, stubbing, stubs, double, doubles
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-03-17 09:03)