Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mock Objects Presentation


Published on

  • Be the first to comment

  • Be the first to like this

Mock Objects Presentation

  1. 1. Mock Objects and Mock Frameworks Andriy Buday
  2. 2. Weather Service Example <ul><li>It is still raining in Lviv </li></ul><ul><li>So how to test sunny weather? </li></ul>Introduction…
  3. 3. What are Mock Objects? <ul><li>Any dummy object that stands in for a real object that is not available, or is difficult to use in a test case </li></ul><ul><li>A mock object must have ability to set up expectations and provide a self-validation mechanism </li></ul>
  4. 4. Why use mock objects? <ul><li>Help keep design decoupled </li></ul><ul><li>Check code’s usage of another object </li></ul><ul><li>Make tests run faster </li></ul><ul><li>Make it easier to write code for problematic resources </li></ul><ul><li>Defer having to write a class </li></ul><ul><li>Test components in isolation from the rest of the system </li></ul>
  5. 5. Why use mock objects? <ul><li>Promote interface-based design </li></ul><ul><li>Refine interfaces </li></ul><ul><li>Test unusual, unlikely and exceptional situations </li></ul>
  6. 6. When to use Mock Objects? <ul><li>Non-deterministic or random behaviour </li></ul><ul><li>Difficult setup </li></ul><ul><li>Behavior hard to cause </li></ul><ul><li>Slow real objects </li></ul><ul><li>UIs </li></ul><ul><li>Test needs to make queries not supported by real object </li></ul><ul><li>Non-existent real objects </li></ul>
  7. 7. Diff Between Mocks and Stubs <ul><li>Dummy </li></ul><ul><li>are not really used </li></ul><ul><li>Fake </li></ul><ul><li>have working implementation but not real </li></ul><ul><li>Stubs </li></ul><ul><li>pass prepared answers </li></ul><ul><li>Mocks </li></ul><ul><li>provide expectation logic </li></ul>
  8. 8. Warehouse Example <ul><li>Regular tests </li></ul><ul><li>Nmock </li></ul><ul><li>Rhino Mocks </li></ul><ul><li>Paskal Mock </li></ul>
  9. 9. Rhino Mocks <ul><li>Dynamic mocks </li></ul><ul><li>on concrete class </li></ul><ul><li>mock some methods </li></ul><ul><li>Partial mocks </li></ul><ul><li>on class/interface </li></ul><ul><li>every member should be mocks </li></ul><ul><li>compiler default for not expected methods called </li></ul><ul><li>Strict mocks </li></ul><ul><li>similar to partial </li></ul><ul><li>test fail if not expected method was called </li></ul>Rhino can kill weather dilemma easily
  10. 10. Questions?