Testing with Mock Objects

2,320 views

Published on

A presentation on the value of mock objects when using TDD.

Published in: Technology, Business
  • Be the first to comment

Testing with Mock Objects

  1. 1. testing with mock objects
  2. 2. when i say testing… <ul><li>== developer / unit tests </li></ul><ul><li>== integration tests (sometimes) </li></ul><ul><li>!= functional / acceptance tests </li></ul>
  3. 3. when i say mock… <ul><li>test stub </li></ul><ul><li>mock object </li></ul><ul><li>dummy object </li></ul><ul><li>fake object </li></ul>
  4. 4. mock vs. stub interaction vs. state <ul><li>the difference between mocks and stubs </li></ul><ul><li>usually the same library provides both </li></ul><ul><li>interaction = mock </li></ul><ul><li>state = stub </li></ul>
  5. 5. why?
  6. 6. testing is easy in isolation
  7. 7. testing is harder with dependencies …
  8. 8. … so remove the dependencies (for developer testing)
  9. 9. for me… <ul><li>lots of time in the airport </li></ul><ul><li>lots of time in the airplane </li></ul><ul><li>lots of time on the train </li></ul><ul><li>not enough family time </li></ul><ul><li>not enough snowboarding </li></ul>
  10. 10. scenarios to think about <ul><li>fee per transaction </li></ul><ul><li>distributed development </li></ul><ul><li>parallel development </li></ul><ul><li>plug-ins / providers </li></ul><ul><li>automated test duration </li></ul>
  11. 11. scenarios to think about <ul><li>third-party tools or services </li></ul><ul><li>non-deterministic outcomes </li></ul><ul><li>external systems </li></ul><ul><li>the mainframe crew </li></ul><ul><li>defer infrastructure choices </li></ul>
  12. 12. demo
  13. 13. thoughts
  14. 14. … on architecture and design <ul><li>similar to the benefits of tdd </li></ul><ul><li>writing to interfaces = code empathy </li></ul><ul><li>easier to test = good design </li></ul><ul><li>highly cohesive / loosely coupled </li></ul>
  15. 15. … on implementation <ul><li>good libraries exist </li></ul><ul><li>roll your own only for extreme cases </li></ul><ul><li>for .Net… </li></ul><ul><li>NMock, NMock2, Rhino Mocks, DotNetMock </li></ul>
  16. 16. … on automated builds <ul><li>developer tests: use mocks (if necessary) </li></ul><ul><li>acceptance tests: don’t use mocks </li></ul>
  17. 17. … on a few useful patterns <ul><li>code to interfaces </li></ul><ul><li>thin proxies </li></ul><ul><li>self-shunt </li></ul><ul><li>dependency injection </li></ul><ul><li>providers </li></ul>
  18. 18. learn more
  19. 19. http:// www.martinfowler.com/articles/mocksArentStubs.html
  20. 20. http:// xunitpatterns.com /
  21. 21. http:// www.nmock.org
  22. 22. http:// www.mockobjects.com
  23. 23. http:// www.codeproject.com/dotnet/tdd_in_dotnet.asp

×