0
Interaction testing using mock objects 4
What you will learn <ul><li>Interaction testing
Understand mock objects
Mock vs Stub
Mock object best practices </li></ul>
Interaction Testing <ul><li>How an object  interact  with other object?
action-driven  testing
Watering system example </li></ul>
Understanding mock object <ul><li>A  fake  object
Verify  object interact as expected
Decide  whether the unit test has passed or failed </li></ul>
Mock vs Stub <ul><li>Stubs can’t  fail  tests, but mocks can
Assert  is perform on mock </li></ul>LogError(string) Test LogAnalyzer MockWebService :IWebService Test
Creating simple mock <ul><li>Extract  an interface
Upcoming SlideShare
Loading in...5
×

Interaction testing using mock objects

1,530

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,530
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Interaction testing using mock objects"

  1. 1. Interaction testing using mock objects 4
  2. 2. What you will learn <ul><li>Interaction testing
  3. 3. Understand mock objects
  4. 4. Mock vs Stub
  5. 5. Mock object best practices </li></ul>
  6. 6. Interaction Testing <ul><li>How an object interact with other object?
  7. 7. action-driven testing
  8. 8. Watering system example </li></ul>
  9. 9. Understanding mock object <ul><li>A fake object
  10. 10. Verify object interact as expected
  11. 11. Decide whether the unit test has passed or failed </li></ul>
  12. 12. Mock vs Stub <ul><li>Stubs can’t fail tests, but mocks can
  13. 13. Assert is perform on mock </li></ul>LogError(string) Test LogAnalyzer MockWebService :IWebService Test
  14. 14. Creating simple mock <ul><li>Extract an interface
  15. 15. Save some state for later assertion and verification
  16. 16. Test spy (xUnit Test Pattern) </li></ul>
  17. 17. ... <ul><li>What the test might look like?
  18. 18. Mock should be reusable </li><ul><li>No test code inside mock object code </li></ul></ul>
  19. 19. Together mock and stub <ul><li>Stub when it won't verify the test result
  20. 20. We’ll assert against mock that it was called correctly </li></ul>LogError(string) SendMail() Assert LogAnalyzer EmailService WebService Test
  21. 21. Mock? Stub? <ul><li>Decide what should be stub and mock? </li></ul>
  22. 22. Some interesting questions? <ul><li>Why are we doing several asserts in a single test?
  23. 23. It is tedious to create manual mocks and stubs. How can we overcome that?
  24. 24. Could we use a Mock object as Stub? </li></ul>
  25. 25. One mock per test <ul><li>Test only one thing
  26. 26. No more than one mock
  27. 27. Stubs all other fake objects
  28. 28. If you need more mocks? in a test, you are testing more than one thing. </li></ul>
  29. 29. Stub chains <ul><li>Factory classes and methods
  30. 30. Chains of lie - “ phishing ” attack
  31. 31. Override virtual method
  32. 32. Create special wrapper classes to avoid chains </li></ul>
  33. 33. writing manual mocks and stubs <ul><li>It takes time
  34. 34. It's difficult to implement when interface have many properties, methods and events
  35. 35. A lot of boilerplate code to save data
  36. 36. Multiple assertions for method with more parameters
  37. 37. Hard to reuse for other tests </li></ul>
  38. 38. Last but not least <ul><li>One of the most common problems encountered by people who write tests is using mocks too much in their tests.
  39. 39. If you see “verify” and “stub” on the same variable in the same test, you most likely are over-specifying your test.
  40. 40. Just make sure your test remains readable .
  41. 41. Use mocking framework </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×