A brief introduction to test doubles and mocking frameworks
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.net   [email_address] http://www.twitter.com/buildmaster

Mocking 101

  • 1.
    A brief introductionto test doubles and mocking frameworks
  • 2.
    Unit testing goalsVerification Feedback Speed Focus Isolation
  • 3.
    Test Doubles 4types 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
  • 4.
    Why? Feedback cycleneeds 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
  • 5.
    Mocking frameworks Providea 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)
  • 6.
    Rhino.Mocks One ofthe 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
  • 7.
    Typemock Isolator Matureand 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 ++
  • 8.
    Moq New Kidon 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
  • 9.
  • 10.
    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.net [email_address] http://www.twitter.com/buildmaster