SlideShare a Scribd company logo
Overview
§  Why Mock Objects?
    §  “Mockist” vs. “Classic” TDD

    §  “Mockist” and “Classic” TDD

§  Mocks and Smalltalk:

      §    The Mocketry framework introduction
§    Examples

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

Mock Objects and Smalltalk              2
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?                 3
Public Opinion



…seems to be about testing



                             4
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?                         5
Why Mock Objects?
§  Cut off dependencies in tests
§  Test-Driven Decomposition

      §  Discover Responsibility (for collaborators)
      §  Thinking vs. Speculating/Fantasizing




                Seamless TDD
Why Mock Objects?                                       6
What Is The Problem?

§    Dependencies
      §    How to cut them off?


§    Novel Collaborators
      §    Where to cut?

Seamless TDD                       7
What Is The problem?

§    Dependencies
      §    How to cut them off?


§    Novel Collaborators
      §    Where to cut?

Seamless TDD                       8
Dependencies
We have:
§  System Under Development (SUD)

§  Collaborators

§  Collaborators’ collaborators …



             Complex Test

Seamless TDD — What’s the Problem?   9
So What?
We have to implement collaborators
§  … without tests

§  … loosing focus on SUD




              Digression
Seamless TDD — Dependencies      10
Dependencies: Example

§    Mocks Aren’t Stubs by Martin Fowler


§    Filling orders from warehouse




Seamless TDD — Dependencies             11
Filling Order
OrderTests >>    !
  testIsFilledIfEnoughInWarehouse!
!

| order |!
order:= Order on: 50 of: #product.!
order fillFrom: warehouse.!
self assert: order isFilled!




Seamless TDD — Dependencies     12
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            13
Digression Detected!
§  I develop Order
§  I don’t want to think about
    Warehouse




Seamless TDD — Dependencies       14
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         15
…And Even More Digression
Reduce amount of #product at the
warehouse
test…!
…!
self assert: !
  (warehouse !
    amountOf: #product)!
                     isZero!
Seamless TDD — Dependencies        16
… 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         17
… Much More Digression
       More complex test cases


 Collaborators’ logic becomes more
       and more complex…

           This can engulf
Seamless TDD — Dependencies      18
Not-So-Seamless TDD
§  SUD is Order
§  Warehouse blures SUD

      §  #add:of:
      §  #amountOf:

§    No explicit tests for Warehouse


Seamless TDD — Dependencies             19
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           20
Mocking Warehouse
…!
[:warehouse| !
[order fillFrom: warehouse]!
  should satisfy:!
     [(warehouse !
         has: 50 of: #product)!
             willReturn: true. !
      warehouse !
         remove: 50 of: #product]!
] runScenario.!
…!
Seamless TDD — Dependencies     21
The Mocketry
    Framework
                             Mock Objects in
                             Smalltalk World
Mock Objects and Smalltalk                22
Behavior Expectations

When you do this with SUD,
  expect that to happen
      with collaborators

Collaborators are mocked

Mocketry                     23
Behavior Expectations
    Do this      Exercise

 Expect that       Verify

                  Scenario
Mocketry                     24
Mocketry Scenario Pattern
SomeTestCases >> testCase
[

     testScenario

] runScenario

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

Mocketry – Behavior Expectations      26
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          27
Mocketry Scenario Pattern
SomeTestCases >> testCase
[
     [exercise] should strictly satisfy: [behaviorExpectations]

     [exercise] should strictly satisfy: [behaviorExpectations]

     … do anything
] runScenario

Mocketry – Behavior Expectations                           28
Mocketry Scenario Pattern
SomeTestCases >> testCase
[ :mock |
     [exercise] should strictly satisfy: [behaviorExpectations]

     [exercise] should strictly satisfy: [behaviorExpectations]

     … do anything
] runScenario

Mocketry – Behavior Expectations                           29
Mocketry Scenario Pattern
SomeTestCases >> testCase
[ :mock |
     [exercise] should strictly satisfy: [behaviorExpectations]

     [exercise] should strictly satisfy: [behaviorExpectations]

     … do anything
] runScenario

Mocketry – Behavior Expectations                           30
Mocketry Scenario Pattern
SomeTestCases >> testCase
[ :mock1 :mock2 :mock3 |
     [exercise] should strictly satisfy: [behaviorExpectations]

     [exercise] should strictly satisfy: [behaviorExpectations]

     … do anything
] runScenario

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


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

Mocketry – Behavior Expectations   33
State Specification DSL
§    resultObject should <expectation>
      §  result should be: anotherObject
      §  result should equal: anotherObject

      §  …




Mocketry                                       34
Mocketry
§  There is much more…
§  Ask me

§  …or Dennis Kudryashov (the

    Author)




                                 35
Mocking Warehouse
OrderTests >> !
testIsFilledIfEnoughInWarehouse!
| order |!
order := Order on: 50 of: #product.!
!

[:warehouse| !
  [order fillFrom: warehouse]!
    should satisfy:!
   [(warehouse has: 50 of: #product)!
           willReturn: true. !
    warehouse remove: 50 of: #product]!
] runScenario.!
!
self assert: order isFilled !

Seamless TDD — Dependencies — Example   36
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                 37
What We Get
§  No need to implement Warehouse
§  Just specify expectations

§  … right in the test

§    Focus on the SUD



Why Mock Objects                     38
What’s the problem?
§  Dependencies
§  Novel Сollaborators




Seamless TDD              39
Where to Start?
         A novel system to implement

                 Object 2
   Object 1                     Object N
                            …
                Object 3

Seamless TDD — Novel Collaborators         40
Where to Cut?
         A novel system to implement
                          Feature
       Feature Feature Feature
      Feature            Feature
                 Feature
           Feature
                       Feature
        FeatureFeature

Seamless TDD — Novel Collaborators     41
Where to Start?
§    Try to guess
      §  … and be ready to abandon test(s)
      §  … or get a mess

                   Or
§  Analyze thoroughly

      §  … up-front decomposition
      §  … without tests — just a fantasy

Seamless TDD — Novel Collaborators            42
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        43
Bulls and Cows Game
Scenario:
§  User creates a game object

§  User starts the game

      §    Game should generate a key
§    …


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




Seamless TDD — Novel Collaborators   45
What Can I Do?
§    Spontaneous representation
      §    Do you feel lucky?
§    Analyze thoroughly
      §    Give up TDD
§    Postpone the test
      §    Not a solution


Seamless TDD — Novel Collaborators   46
What Can I Do?
§  …
§  Create a new class for key

      §    Unnecessary complexity?




Seamless TDD — Novel Collaborators    47
What Can I Do?


       That was a Digression!




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



Seamless TDD — Novel Collaborators   49
testGeneratesKeyOnStart
[ :keyGen |!
 game keyGenerator: keyGen.!
 [ game start ]!
   should satisfy:!
     [keyGen createKey!
        willReturn: #key]!
 game key should be: #key!
] runScenario.!
Seamless TDD — Novel Collaborators   50
What We Get
§    Key generation functionality
      §  is revealed
      §  moved to another object

§    Dependency Injection
      §  fake key can be created for tests
      §  KeyGenerator refactored to Turn

§    No risk of incorrect decision
Seamless TDD — Novel Collaborators            51
What We Get
Seamless TDD:
§  No digression

§  No up-front decomposition

§  No up-front design

§  No speculating / fantasizing




Seamless TDD — Novel Collaborators   52
Complete Example

Mock Objects For Top-Down Design
by Bulls-and-Cows Example

            Just ask!



                               53
Classic vs. Mockist TDD

         State vs. Behavior?
         Result vs. Intention?
           No contradiction!
       Mockist approach
   complements “classic” TDD
Seamless TDD                     54
Classic and Mockist TDD

§    Top-Down with Mockist TDD
      §    Analysis and Decomposition

§    Bottom-Up with Classic TDD
      §    Synthesis and “real-object” testing


                                                  55

More Related Content

PDF
When Tdd Goes Awry (IAD 2013)
PDF
Real developers-dont-need-unit-tests
PDF
Pragmatic Not Dogmatic TDD Agile2012 by Joseph Yoder and Rebecca Wirfs-Brock
PDF
Practical Guide to Unit Testing
PDF
Unit testing best practices with JUnit
PPTX
Unit testing patterns for concurrent code
PDF
如何提升 iOS 開發速度?
PDF
20170302 tryswift tasting_tests
When Tdd Goes Awry (IAD 2013)
Real developers-dont-need-unit-tests
Pragmatic Not Dogmatic TDD Agile2012 by Joseph Yoder and Rebecca Wirfs-Brock
Practical Guide to Unit Testing
Unit testing best practices with JUnit
Unit testing patterns for concurrent code
如何提升 iOS 開發速度?
20170302 tryswift tasting_tests

Similar to Mock Objects and Smalltalk (20)

PDF
Bdd and-testing
PDF
Behaviour Driven Development and Thinking About Testing
 
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
PDF
Evolve your coding with some BDD
PDF
TDD Workshop UTN 2012
PDF
Tdd is not about testing
PPTX
TDD - Seriously, try it! - Opensouthcode
PDF
TDD (with FLOW3)
PDF
Test Driven Development SpeedRun
PPTX
Test Doubles
PDF
Test driven development
PPTX
Understanding TDD - theory, practice, techniques and tips.
PPTX
Coding Naked
PDF
(automatic) Testing: from business to university and back
PPTX
TDD - Seriously, try it - Codemotion (May '24)
KEY
Driving application development through behavior driven development
PPTX
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
PDF
CBDW2014 - Behavior Driven Development with TestBox
PDF
Developing an Ember Test Strategy - EmberConf 2019
Bdd and-testing
Behaviour Driven Development and Thinking About Testing
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Evolve your coding with some BDD
TDD Workshop UTN 2012
Tdd is not about testing
TDD - Seriously, try it! - Opensouthcode
TDD (with FLOW3)
Test Driven Development SpeedRun
Test Doubles
Test driven development
Understanding TDD - theory, practice, techniques and tips.
Coding Naked
(automatic) Testing: from business to university and back
TDD - Seriously, try it - Codemotion (May '24)
Driving application development through behavior driven development
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
CBDW2014 - Behavior Driven Development with TestBox
Developing an Ember Test Strategy - EmberConf 2019
Ad

More from ESUG (20)

PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
PDF
Directing Generative AI for Pharo Documentation
PDF
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
PDF
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
PDF
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
PDF
Analysing Python Machine Learning Notebooks with Moose
PDF
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
PDF
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
PDF
Package-Aware Approach for Repository-Level Code Completion in Pharo
PDF
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
PDF
An Analysis of Inline Method Refactoring
PDF
Identification of unnecessary object allocations using static escape analysis
PDF
Control flow-sensitive optimizations In the Druid Meta-Compiler
PDF
Clean Blocks (IWST 2025, Gdansk, Poland)
PDF
Encoding for Objects Matters (IWST 2025)
PDF
Challenges of Transpiling Smalltalk to JavaScript
PDF
Immersive experiences: what Pharo users do!
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
PDF
Cavrois - an Organic Window Management (ESUG 2025)
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
Micromaid: A simple Mermaid-like chart generator for Pharo
Directing Generative AI for Pharo Documentation
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
Analysing Python Machine Learning Notebooks with Moose
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
Package-Aware Approach for Repository-Level Code Completion in Pharo
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
An Analysis of Inline Method Refactoring
Identification of unnecessary object allocations using static escape analysis
Control flow-sensitive optimizations In the Druid Meta-Compiler
Clean Blocks (IWST 2025, Gdansk, Poland)
Encoding for Objects Matters (IWST 2025)
Challenges of Transpiling Smalltalk to JavaScript
Immersive experiences: what Pharo users do!
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
Cavrois - an Organic Window Management (ESUG 2025)
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Cloud computing and distributed systems.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Modernizing your data center with Dell and AMD
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Building Integrated photovoltaic BIPV_UPV.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Modernizing your data center with Dell and AMD
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm

Mock Objects and Smalltalk

  • 1. Overview §  Why Mock Objects? §  “Mockist” vs. “Classic” TDD §  “Mockist” and “Classic” TDD §  Mocks and Smalltalk: §  The Mocketry framework introduction §  Examples Mock Objects and Smalltalk 1
  • 2. Why Mock Objects? Do we need Mocks at all (in Smalltalk)? Mock Objects and Smalltalk 2
  • 3. 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? 3
  • 4. Public Opinion …seems to be about testing 4
  • 5. 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? 5
  • 6. Why Mock Objects? §  Cut off dependencies in tests §  Test-Driven Decomposition §  Discover Responsibility (for collaborators) §  Thinking vs. Speculating/Fantasizing Seamless TDD Why Mock Objects? 6
  • 7. What Is The Problem? §  Dependencies §  How to cut them off? §  Novel Collaborators §  Where to cut? Seamless TDD 7
  • 8. What Is The problem? §  Dependencies §  How to cut them off? §  Novel Collaborators §  Where to cut? Seamless TDD 8
  • 9. Dependencies We have: §  System Under Development (SUD) §  Collaborators §  Collaborators’ collaborators … Complex Test Seamless TDD — What’s the Problem? 9
  • 10. So What? We have to implement collaborators §  … without tests §  … loosing focus on SUD Digression Seamless TDD — Dependencies 10
  • 11. Dependencies: Example §  Mocks Aren’t Stubs by Martin Fowler §  Filling orders from warehouse Seamless TDD — Dependencies 11
  • 12. Filling Order OrderTests >> ! testIsFilledIfEnoughInWarehouse! ! | order |! order:= Order on: 50 of: #product.! order fillFrom: warehouse.! self assert: order isFilled! Seamless TDD — Dependencies 12
  • 13. 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 13
  • 14. Digression Detected! §  I develop Order §  I don’t want to think about Warehouse Seamless TDD — Dependencies 14
  • 15. 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 15
  • 16. …And Even More Digression Reduce amount of #product at the warehouse test…! …! self assert: ! (warehouse ! amountOf: #product)! isZero! Seamless TDD — Dependencies 16
  • 17. … 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 17
  • 18. … Much More Digression More complex test cases Collaborators’ logic becomes more and more complex… This can engulf Seamless TDD — Dependencies 18
  • 19. Not-So-Seamless TDD §  SUD is Order §  Warehouse blures SUD §  #add:of: §  #amountOf: §  No explicit tests for Warehouse Seamless TDD — Dependencies 19
  • 20. 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 20
  • 21. Mocking Warehouse …! [:warehouse| ! [order fillFrom: warehouse]! should satisfy:! [(warehouse ! has: 50 of: #product)! willReturn: true. ! warehouse ! remove: 50 of: #product]! ] runScenario.! …! Seamless TDD — Dependencies 21
  • 22. The Mocketry Framework Mock Objects in Smalltalk World Mock Objects and Smalltalk 22
  • 23. Behavior Expectations When you do this with SUD, expect that to happen with collaborators Collaborators are mocked Mocketry 23
  • 24. Behavior Expectations Do this Exercise Expect that Verify Scenario Mocketry 24
  • 25. Mocketry Scenario Pattern SomeTestCases >> testCase [ testScenario ] runScenario Mocketry – Behavior Expectations 25
  • 26. Mocketry Scenario Pattern SomeTestCases >> testCase [ [exercise] should strictly satisfy: [behaviorExpectations] ] runScenario Mocketry – Behavior Expectations 26
  • 27. 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 27
  • 28. Mocketry Scenario Pattern SomeTestCases >> testCase [ [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations 28
  • 29. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock | [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations 29
  • 30. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock | [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations 30
  • 31. Mocketry Scenario Pattern SomeTestCases >> testCase [ :mock1 :mock2 :mock3 | [exercise] should strictly satisfy: [behaviorExpectations] [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry – Behavior Expectations 31
  • 32. Trivial Example 1 TrueTests >> testDoesNotExecuteIfFalseBlock [ :block | [true ifFalse: block ] should satisfiy: [“nothing expected”] ] runScenario Mocketry – Behavior Expectations 32
  • 33. Trivial Example 2 TrueTests >> testExecutesIfTrueBlock [ :block | [true ifTrue: block] should satisfiy: [block value] ] runScenario Mocketry – Behavior Expectations 33
  • 34. State Specification DSL §  resultObject should <expectation> §  result should be: anotherObject §  result should equal: anotherObject §  … Mocketry 34
  • 35. Mocketry §  There is much more… §  Ask me §  …or Dennis Kudryashov (the Author) 35
  • 36. Mocking Warehouse OrderTests >> ! testIsFilledIfEnoughInWarehouse! | order |! order := Order on: 50 of: #product.! ! [:warehouse| ! [order fillFrom: warehouse]! should satisfy:! [(warehouse has: 50 of: #product)! willReturn: true. ! warehouse remove: 50 of: #product]! ] runScenario.! ! self assert: order isFilled ! Seamless TDD — Dependencies — Example 36
  • 37. 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 37
  • 38. What We Get §  No need to implement Warehouse §  Just specify expectations §  … right in the test §  Focus on the SUD Why Mock Objects 38
  • 39. What’s the problem? §  Dependencies §  Novel Сollaborators Seamless TDD 39
  • 40. Where to Start? A novel system to implement Object 2 Object 1 Object N … Object 3 Seamless TDD — Novel Collaborators 40
  • 41. Where to Cut? A novel system to implement Feature Feature Feature Feature Feature Feature Feature Feature Feature FeatureFeature Seamless TDD — Novel Collaborators 41
  • 42. Where to Start? §  Try to guess §  … and be ready to abandon test(s) §  … or get a mess Or §  Analyze thoroughly §  … up-front decomposition §  … without tests — just a fantasy Seamless TDD — Novel Collaborators 42
  • 43. 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 43
  • 44. Bulls and Cows Game Scenario: §  User creates a game object §  User starts the game §  Game should generate a key §  … Seamless TDD — Novel Collaborators 44
  • 45. testGeneratesKeyOnStart ! ! self assert: key …?! “How to represent key?!”! Seamless TDD — Novel Collaborators 45
  • 46. What Can I Do? §  Spontaneous representation §  Do you feel lucky? §  Analyze thoroughly §  Give up TDD §  Postpone the test §  Not a solution Seamless TDD — Novel Collaborators 46
  • 47. What Can I Do? §  … §  Create a new class for key §  Unnecessary complexity? Seamless TDD — Novel Collaborators 47
  • 48. What Can I Do? That was a Digression! Seamless TDD — Novel Collaborators 48
  • 49. testGeneratesKeyOnStart |key|! game start.! key := game key.! self assert: ! !key isKindOf: Code! Seamless TDD — Novel Collaborators 49
  • 50. testGeneratesKeyOnStart [ :keyGen |! game keyGenerator: keyGen.! [ game start ]! should satisfy:! [keyGen createKey! willReturn: #key]! game key should be: #key! ] runScenario.! Seamless TDD — Novel Collaborators 50
  • 51. What We Get §  Key generation functionality §  is revealed §  moved to another object §  Dependency Injection §  fake key can be created for tests §  KeyGenerator refactored to Turn §  No risk of incorrect decision Seamless TDD — Novel Collaborators 51
  • 52. What We Get Seamless TDD: §  No digression §  No up-front decomposition §  No up-front design §  No speculating / fantasizing Seamless TDD — Novel Collaborators 52
  • 53. Complete Example Mock Objects For Top-Down Design by Bulls-and-Cows Example Just ask! 53
  • 54. Classic vs. Mockist TDD State vs. Behavior? Result vs. Intention? No contradiction! Mockist approach complements “classic” TDD Seamless TDD 54
  • 55. Classic and Mockist TDD §  Top-Down with Mockist TDD §  Analysis and Decomposition §  Bottom-Up with Classic TDD §  Synthesis and “real-object” testing 55