100 likes | 312 Views
A brief introduction to test doubles and mocking frameworks. Mocking 101. Unit testing goals. Verification Feedback Speed Focus Isolation. Test Doubles. 4 types of test double Dummy – We just pass it round and don’t do anything with it Fake – Working implementations but take shortcuts
E N D
A brief introduction to test doubles and mocking frameworks Mocking 101
Unit testing goals • Verification • Feedback Speed • Focus • Isolation
Test Doubles • 4 types of test double • Dummy – We just pass it round and don’t do anything with it • Fake – Working implementations but take shortcuts • Stubs – Canned answers to expected calls • Mocks – Pre programmed with responses and expectations that certain calls should be made
Why? • Feedback cycle needs to be as small as possible to be useful • We need to remove pain points from being necessary • Database calls • UI • Third party libraries • We can remove reliance on the rest of the code being complete before knowing the current unit works
Mocking frameworks • Provide a repeatable way to set up mocks/stubs/fakes with little code cost. • Test logic can stay within test • We can develop as we test and get immediate results (TDD)
Rhino.Mocks • One of the most mature and feature complete mocking frameworks • Uses castle remoting to mock calls • Not able to mock static or sealed calls • Fast • Record/Replay Syntax • Lots of help and code examples available • Open Source
Typemock Isolator • Mature and fully featured • Uses IL Generation • Can mock static classes and calls • Can chain events • Commercial licence available for full feature set and support • Main commercial mocking framework so features ++
Moq • New Kid on the block • .net 3.5 only • No record/replay semantics, just expectations • Less code clutter to tests • Not feature complete • In active development • Open source
How? • It’s time to code
Links • Rhino Mocks: http://www.ayende.com/projects/rhino-mocks.aspx • Typemock: http://www.typemock.com/ • Moq: http://code.google.com/p/moq • Me: http://www.bgeek.netowen@bgeek.nethttp://www.twitter.com/buildmaster