1 / 56

Mock Objects in the Smalltalk World

Mock Objects in the Smalltalk World. Dennis Schetinin. Overview. Why Mock Objects? “Mockist” vs. “Classic” TDD “Mockist” and “ Classic” TDD Mocks and Smalltalk: The Mocketry framework i ntroduction Examples. Why Mock Objects?. Do we need Mocks at all (in Smalltalk)?.

crwys
Télécharger la présentation

Mock Objects in the Smalltalk World

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mock Objects in the Smalltalk World Dennis Schetinin

  2. Overview • Why Mock Objects? • “Mockist” vs. “Classic” TDD • “Mockist” and “Classic” TDD • Mocks and Smalltalk: • The Mocketry framework introduction • Examples Mock Objects and Smalltalk

  3. Why Mock Objects? Do we need Mocks at all (in Smalltalk)? Mock Objects and Smalltalk

  4. Public Opinion • Smalltalk vs. Mock Objects? • Few/rare special cases • With mock you don’t test real thing • Use mocks for external objects only • Use other means to involve complex external objects • Speed up by other means Do we need Mock Objects at all?

  5. Public Opinion …seems to be about testing

  6. Smalltalk and Mock Objects • “Mock Objects” is a TDD technique • … about developing systems • … not just testing • … useful in all languages • Smalltalk makes mocks • much easier to use Why Mock Objects?

  7. Why Mock Objects? • Cut off dependencies in tests • Test-Driven Decomposition • Discover Responsibility (for collaborators) • Thinking vs. Speculating/Fantasizing Seamless TDD Why Mock Objects?

  8. What Is The Problem? • Dependencies • How to cut them off? • Novel Collaborators • Where to cut? Seamless TDD

  9. What Is The problem? • Dependencies • How to cut them off? • Novel Collaborators • Where to cut? Seamless TDD

  10. Dependencies We have: • System Under Development (SUD) • Collaborators • Collaborators’ collaborators … Complex Test Seamless TDD — What’s the Problem?

  11. So What? Wehave to implement collaborators • … without tests • … loosing focus on SUD Digression Seamless TDD — Dependencies

  12. Dependencies: Example • Mocks Aren’t Stubs by Martin Fowler • Filling orders from warehouse Seamless TDD — Dependencies

  13. Filling Order OrderTests >> testIsFilledIfEnoughInWarehouse | order | order:= Order on: 50 of: #product. order fillFrom: warehouse. self assert: order isFilled Seamless TDD — Dependencies

  14. Filling Order OrderTests >> testIsFilledIfEnoughInWarehouse | order warehouse | warehouse := Warehouse new. “Put #product there” “…but how?!” order := Order on: 50 of: #product. order fillFrom: warehouse. … Seamless TDD — Dependencies

  15. Digression Detected! • I develop Order • I don’t want to think about Warehouse Seamless TDD — Dependencies

  16. Filling Order OrderTests >> testIsFilledIfEnoughInWarehouse | order warehouse | warehouse := Warehouse new. warehouse add: 50 of: #product. order := Order on: 50 of: #prod. order fillFrom: warehouse. … Seamless TDD — Dependencies

  17. …And Even More Digression Reduce amount of #product at the warehouse test… … self assert: (warehouse amountOf:#product) isZero Seamless TDD — Dependencies

  18. … And Even More Digression Another test case: If there isn’t enough #product in the warehouse, • do not fill order • do not remove #product from warehouse Seamless TDD — Dependencies

  19. … Much More Digression More complex test cases Collaborators’ logic becomes more and more complex… This can engulf Seamless TDD — Dependencies

  20. Not-So-Seamless TDD • SUD is Order • WarehousebluresSUD • #add:of: • #amountOf: • No explicit tests for Warehouse Seamless TDD — Dependencies

  21. Mocking Warehouse OrderTests >> testIsFilledIfEnoughInWarehouse | order | order := Order on: 50 of: #product. [ :warehouse| [order fillFrom: warehouse] should satisfy: [“expectations”] ] runScenario. self assert: order isFilled Seamless TDD — Dependencies

  22. Mocking Warehouse … [:warehouse| [order fillFrom: warehouse] should satisfy: [(warehouse has: 50 of: #product) willReturn: true. warehouse remove: 50 of:#product] ] runScenario. … Seamless TDD — Dependencies

  23. The Mocketry Framework Mock Objects in Smalltalk World Mock Objects and Smalltalk

  24. Behavior Expectations When you do this with SUD, expect that to happen with collaborators Collaborators are mocked Mocketry

  25. Behavior Expectations Do this Expect that Scenario Mocketry Exercise Verify

  26. Mocketry Scenario Pattern SomeTestCases >> testCase [ testScenario ] runScenario Mocketry – Behavior Expectations

  27. Mocketry Scenario Pattern SomeTestCases >> testCase [ [exercise] should strictly satisfy: [behaviorExpectations] ] runScenario Mocketry – Behavior Expectations

  28. Behavior Expectations • Just send mock objects the messages they should receive warehouse has: 50 of: #product • Specify their reaction (warehouse has: 50 of: #product) willReturn: true Mocketry — Behavior Expectations

  29. Mocketry Scenario Pattern SomeTestCases >> testCase [ [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations

  30. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock| [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations

  31. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock| [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations

  32. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock1 :mock2 :mock3 | [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations

  33. Trivial Example 1 TrueTests >> testDoesNotExecuteIfFalseBlock [ :block | [true ifFalse: block ] should satisfiy: [“nothing expected”] ] runScenario Mocketry – Behavior Expectations

  34. Trivial Example 2 TrueTests >> testExecutesIfTrueBlock [ :block | [true ifTrue: block] should satisfiy: [block value] ] runScenario Mocketry – Behavior Expectations

  35. State Specification DSL • resultObjectshould <expectation> • result should be:anotherObject • result should equal:anotherObject • … Mocketry

  36. Mocketry • There is much more… • Ask me • …or Dennis Kudryashov (the Author)

  37. Mocking Warehouse OrderTests >> testIsFilledIfEnoughInWarehouse | order | order := Order on: 50 of: #product. [:warehouse| [order fillFrom: warehouse] should satisfy: [(warehouse has: 50 of: #product) willReturn: true. warehouseremove: 50 of:#product] ] runScenario. self assert: order isFilled Seamless TDD — Dependencies — Example

  38. Mocking Warehouse OrderTests >> testIsNotFilledIfNotEnoughInWarehouse | order | order := Order on: #amount of: #product. [:warehouse| [order fillFrom: warehouse] should satisfy: [(warehouse has: 50 of: #product) willReturn: false. “Nothing else is expected” ] ] runScenario. self assert: order isFilled Seamless TDD — Dependencies

  39. What We Get • No need to implement Warehouse • Just specify expectations • … right in the test • Focus on the SUD Why Mock Objects

  40. What’s the problem? • Dependencies • Novel Сollaborators Seamless TDD

  41. Where to Start? A novel system to implement Object 2 Object 1 ObjectN … Object 3 Seamless TDD — Novel Collaborators

  42. Where to Cut? A novel system to implement Feature Feature Feature Feature Feature Feature Feature Feature Feature Feature Feature Seamless TDD — Novel Collaborators

  43. Where to Start? • Try to guess • … and be ready to abandon test(s) • … or get a mess Or • Analyzethoroughly • … up-front decomposition • … without tests — just a fantasy Seamless TDD — Novel Collaborators

  44. Novel Collaborators: Example Bulls and Cows Game • Computer generates a secret key • e.g., a 4-digit number • Human player tries to disclose it Seamless TDD — Novel Collaborators

  45. Bulls and Cows Game Scenario: • User creates a game object • User starts the game • Game should generate a key • … Seamless TDD — Novel Collaborators

  46. testGeneratesKeyOnStart self assert: key …? “How to represent key?!” Seamless TDD — Novel Collaborators

  47. What Can IDo? • Spontaneous representation • Do you feel lucky? • Analyze thoroughly • Give up TDD • Postpone the test • Not a solution Seamless TDD — Novel Collaborators

  48. What Can I Do? • … • Create a new class for key • Unnecessary complexity? Seamless TDD — Novel Collaborators

  49. What Can I Do? That was a Digression! Seamless TDD — Novel Collaborators

  50. testGeneratesKeyOnStart |key| game start. key := game key. self assert: key isKindOf: Code Seamless TDD — Novel Collaborators

More Related