230 likes | 297 Views
Using Mock Objects with Test Driven Development. Justin Kohlhepp jkohlhepp@gmail.com. What are mock objects?. From Wikipedia: simulated objects that mimic the behavior of real objects in controlled ways. Think “crash test dummy”. Terminology Mocks, Stubs, Fakes… Whatever!.
E N D
Using Mock Objects withTest Driven Development Justin Kohlhepp jkohlhepp@gmail.com
What are mock objects? From Wikipedia: simulated objects that mimic the behavior of real objects in controlled ways Think “crash test dummy”
Why use mocks? • Isolate code under test; less fragile tests • Difficult to set up dependencies • Verify interaction between objects • Side effects (sending emails, launching missiles) • Abnormal situations (network outages, disk full) • Multiple dev streams (actual class doesn’t exist) • Non-deterministic results (timezones) • Real code too slow (trips to database) • Real code interacts with user (i.e. UI)
Demo Simple Mocking Scenario
moq Basic Syntax http://code.google.com/p/moq/wiki/QuickStart
moq Framework http://code.google.com/p/moq/ • Pronounced "Mock-you" or just "Mock“ • .NET 3.5 or higher • Focused on simple, flexible API • Does not use record/replay style • Open source, BSD license • Active project since 2007
Demo: Advanced moq Features • Mocking Exceptions • Loose vs. Strict • Complex argument matching • Callbacks • Mock concrete classes, Partial Mocks • Automatic recursive mocks • Mock multiple interfaces • Access to protected members
Managing Dependencies • “new” keyword prevents isolation • Inversion of Control: class no longer controls its dependencies; no more “new” • Key patterns for IoC: Dependency Injection, Service Locator
Demo Dependency Injection
Dependency Injection Frameworks • Allow for externalizing dependency management • Can be configuration driven or code driven • Many choices: MS Unity, Castle Windsor, StructureMap, Ninject, etc.
Demo “Realistic” Mocking Scenario
moq – Under the Hood • Uses Castle DynamicProxy • Creates class on the fly that implements interface or derives from class • Tracks methods, properties that are setup on the mock, and how they are invoked during test execution
Mocking Pitfalls • Gaps in integration testing • Maintenance concerns • Testing influencing design • Tests are very “white box” – they know a lot about the thing they are testing • To verify or not to verify • Heisenbugs • Controversy
Conclusions • Mock objects are a powerful tool that you should have in your testing toolbox • Injecting mock objects requires some design considerations. Luckily this also leads to better code (according to some). • Remember: a hybrid approach is possible. Mock some things and use real instances of others. • With great power comes great responsibility
Useful Links • Wikipedia: Mock Objectshttp://bit.ly/7WWq9 • Fowler: Mocks Aren’t Stubshttp://bit.ly/7MdzF • QuickStart with moqhttp://bit.ly/13FYvA • Working Effectively with Legacy Codeby Michael Feathershttp://amzn.to/Mf59m