Mock some methods

Posted by tobi — 04:07 PM May 03

Raphael Bauduin has a nice write-up on mock_methods. A handy tool for unit-testing, which lets you replace any number of methods on existing objects for the duration of a block.

He explains how he uses it to test some difficult to test area in his paypal integration where the controller is dependent on outside information returned from paypal.

Testing Rails controllers communicating with external web services

Comments

  • Lee O'Mara 03 May 19:56

    Cool method. I can see how this has many useful appliactions. I’m still trying to grok the specifics of whats really going on.

    My only nitpik is the name—I think stub_methods would be more appropriate, since it’s not accounting for expectation(as Mock objects do1), instead it provides a hook to replace real implementation details with canned responses.

    1 TestDouble(Martin Fowler)

  • tobi 03 May 21:05

    Yea I agree. There are several simplifications which can be applied to this but it fulfills its main purpose pretty well.

  • Justin 04 May 11:26

    Great article. Thank you for posting this.

Commenting are now closed…