SlideShare a Scribd company logo
1 of 46
Download to read offline
Mocketry
Mock objects in the way it should be
https://github.com/dionisiydk/Mocketry
Have no object for
test?
Create mock
yourMock := Mock new
yourMock := Mock new
• No context helpers
• yourMock := context newMock
• No duplicated mock names
• yourMock := Mock named: ‘yourMock’
• Auto names from test variables (for debugging).
• But you can specify name if you want
• yourMock := Mock named: ‘mockName’
Create multiple mocks
at once
[:mockA :mockB | «your code» ] runWithMocks
Stub messages to
objects
mock stub someMessage willReturn: 100.
mock someMessage should be: 100
Stub messages to
objects
rect := 0@0 corner: 2@3.
rect stub width willReturn: 1000
rect area should equal: 3000
“area = width * height”
Stub messages to
global objects
DateAndTime stub now willReturn: #constantValue.
DateAndTime now should be: #constantValue
Stub messages to global
objects
Be careful with this
DateAndTime stub now willReturn: #constantValue.
DateAndTime now should be: #constantValue.
DateAndTime recoverFromGHMutation
All global stubs are recovered automatically
on test completion
Stub messages with
arguments
mock stub messageWith: arg1 and: arg2
mock stub messageWith: (Kind of: String) and: arg2
mock stub messageWith: Any and: arg2
mock stub messageWith: [:arg | true]
Stub messages with
arguments
(mock stub messageWith: (Instance of: SmallInteger)) willReturn: #anyInt.
(mock stub messageWith: (Kind of: String)) willReturn: #anyString.
(mock stub messageWith: 10) willReturn: #ten.
(mock messageWith: 10) should be: #ten.
(mock messageWith: 20) should be: #anyInt.
(mock messageWith: ‘test’) should be: #anyString
Last defined expectation has more priority
than previously defined
Capture message
arguments
mock stub messageWith: Arg argName.
mock messageWith: #argValue.
Arg argName should be: #argValue.
mock messageWith: #argValue2.
Arg argName fromLastCall should be: #argValue2.
Message sends usage
rules
mock stub someMessage willReturn: #default.
mock stub someMessage willReturn: 1; useOnce.
mock stub someMessage willReturn: 2; useTwice.
mock someMessage should be: 2.
mock someMessage should be: 2.
mock someMessage should be: 1.
mock someMessage should be: #default
Last defined expectation has more priority
than previously defined
Expected actions
mock stub someMessage willReturn: #result
mock stub someMessage willRaise: Error new
(mock stub someMessageWith: #arg) will: [#result]
(mock stub someMessageWith: #arg1 and: #arg2)
will: [:arg1 :arg2 | #result]
mock stub someMessage
willReturnValueFrom: #(result1 result2)
Put extra conditions
for message send
mock := Mock new.
mock someMessage
when: [flag] is: true;
when: [flag] is: (Kind of: Boolean);
when: [flag] satisfy: [:value | true or: [false]].
flag := true.
mock someMessage.
flag := false.
mock someMessage «will fail immediately on call»
Stub messages to
any object
rect := 0@0 corner: 2@3.
Any stub width willReturn: 1000
rect area should equal: 3000
Stub messages to
any object
rect := 0@0 corner: 2@3.
Any stub width willReturn: 1000
rect area should equal: 3000. «It’s not working :)»
rect area should equal: 6
Stub messages to
any object
rect := 0@0 corner: 2@3.
rect stub. «It turns on message interception»
Any stub width willReturn: 1000
rect area should equal: 3000.
Stub any message to
object
mock := Mock new.
mock stub anyMessage willReturn: 1000
mock someMessage should equal: 1000.
mock someMessage2 should equal: 1000
Stub any message to
object
rect := 0@0 corner: 2@3.
rect stub anyMessage willReturn: 1000
rect width should equal: 1000.
rect area should equal: 1000.
Stub any message to
any object
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
Any stub anyMessage willReturn: 1000
mock someMessage should equal: 1000.
rect area should equal: 1000.
mock anyMessage should equal: 1000.
Stub messages to set
of objects
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
(Kind of: Rectangle) stub width willReturn: 1000
mock width should not equal: 1000.
rect width should equal: 1000
rect area should equal: 3000
Stub messages to any
mock
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
Mock stub width willReturn: 1000
mock width should equal: 1000.
rect width should equal: 2
Stub group of
message sends
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
[mock someMessage willReturn: 100.
rect width willReturn: 5000] should expect
Without expectation mock
returns new special mock
on any message
mock := Mock new.
mock someMessage
should beInstanceOf: MockForMessageReturn
Automock is converted to
false in boolean
expressions
mock := Mock new.
autoMock := mock someMessage.
autoMock
ifTrue: [result := #trueDone] ifFalse: [result := #falseDone].
result should be: #falseDone.
autoMock should be: false
Automock is converted to
zero in arithmetic
expressions
mock := Mock new.
autoMock := mock someMessage.
result := 1 + autoMock.
result should be: 1.
autoMock should be: 0
Verify that messages
were sent to objects
mock someMessage.
mock should receive someMessage.
mock should not receive anotherMessage
Verify that messages
were sent objects
rect := 0@0 corner: 2@3.
rect area.
rect should receive width
Verify that messages
were sent objects
rect := 0@0 corner: 2@3.
rect area.
rect should receive width «it will fail :)»
rect should not receive width
Verify that messages
were sent objects
rect := 0@0 corner: 2@3.
rect stub. «it turns on message interception»
rect area.
rect should receive width
Verify messages with
arguments
mock messageWith: 10.
mock messageWith: ‘test’.
mock should receive messageWith: 10.
mock should receive messageWith: (Instance of: SmallInteger).
mock should receive messageWith: (Kind of: String).
Verify messages to
captured arguments
mock stub messageWith: Arg rectangle.
rect := 0@0 corner: 2@3.
mock messageWith: rect.
rect area.
Arg rectangle should receive width.
Arg rectangle should be: rect
Verify how many times
message was sent
mock someMessage.
mock someMessage.
mock should receive someMessage twice
Verify message send result
rect := 0@0 corner: 2@3.
rect stub.
rect area.
rect should receive area which should equal: 6
Verify object sender
result := mock someMessage.
result should beReturnedFrom: [mock someMessage].
result should not beReturnedFrom: [mock someMessage2].
Verify that messages
were sent to any object
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
mock width.
rect area.
Any should receive width.
Any should receive area. “fail because mock not received area”
Verify that any message
were sent to object
mock := Mock new.
mock should not receive anyMessage
mock someMessage.
mock should receive anyMessage
Verify that any message
were sent to object
rect := 0@0 corner: 2@3.
rect stub. «It turns on message interception»
rect should not receive anyMessage
rect area.
rect should receive anyMessage
Verify that any message
were sent to any object
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
mock someMessage.
Any should receive anyMessage. “fail because rect not received any message”
rect area.
Any should receive anyMessage.
Verify that messages were
sent to set of objects
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
rect2 := 0@0 corner: 5@6.
rect2 stub.
rect area.
rect2 width.
(Kind of: Rectangle) should receive width.
(Kind of: Rectangle) should receive area. “fail because rect2 not received #area”
Verify group of
message sends
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
mock someMessage.
rect area.
[rect width. mock someMessage] should beDone
Verify group of message
sends in exact order
mock := Mock new.
rect := 0@0 corner: 2@3.
rect stub.
mock someMessage.
rect area.
[mock someMessage. rect width] should beDoneInOrder
Verify expectations
[:mockA :mockB |
[mockB someMessage.
mockA someMessage]
should lenient satisfy:
[mockA someMessage willReturn: 10.
mockB someMessage willReturn: 400]
] runWithMocks
Verify expectations in
exact order
[:mockA :mockB |
[mockB someMessage.
mockA someMessage]
should strictly satisfy:
[mockA someMessage willReturn: 10.
mockB someMessage willReturn: 400]
] runWithMocks
Let’s use Mocketry
in Pharo tests
The end

More Related Content

Similar to Mock objects in the way it should be: Mockery

Антипаттерны модульного тестирования
Антипаттерны модульного тестированияАнтипаттерны модульного тестирования
Антипаттерны модульного тестированияMitinPavel
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsPatchSpace Ltd
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joinedennui2342
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach placesdn
 
Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Srinivasan R
 
Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytestSuraj Deshmukh
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkSerge Stinckwich
 
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)Amazon Web Services
 
please code in c#- please note that im a complete beginner- northwind.docx
please code in c#- please note that im a complete beginner-  northwind.docxplease code in c#- please note that im a complete beginner-  northwind.docx
please code in c#- please note that im a complete beginner- northwind.docxAustinaGRPaigey
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errorsPVS-Studio
 
Ruxmon cve 2012-2661
Ruxmon cve 2012-2661Ruxmon cve 2012-2661
Ruxmon cve 2012-2661snyff
 
Mobile Weekend Budapest presentation
Mobile Weekend Budapest presentationMobile Weekend Budapest presentation
Mobile Weekend Budapest presentationPéter Ádám Wiesner
 
以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界Amazon Web Services
 
Maintaining Your Code Clint Eastwood Style
Maintaining Your Code Clint Eastwood StyleMaintaining Your Code Clint Eastwood Style
Maintaining Your Code Clint Eastwood StyleRebecca Wirfs-Brock
 
Intel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionIntel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionAndrey Karpov
 

Similar to Mock objects in the way it should be: Mockery (20)

Антипаттерны модульного тестирования
Антипаттерны модульного тестированияАнтипаттерны модульного тестирования
Антипаттерны модульного тестирования
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joined
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach places
 
Rspec
RspecRspec
Rspec
 
Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013
 
Vbscript
VbscriptVbscript
Vbscript
 
Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytest
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
 
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)
AWS re:Invent 2016: NEW LAUNCH! Lambda Everywhere (IOT309)
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
please code in c#- please note that im a complete beginner- northwind.docx
please code in c#- please note that im a complete beginner-  northwind.docxplease code in c#- please note that im a complete beginner-  northwind.docx
please code in c#- please note that im a complete beginner- northwind.docx
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errors
 
Ruxmon cve 2012-2661
Ruxmon cve 2012-2661Ruxmon cve 2012-2661
Ruxmon cve 2012-2661
 
Mobile Weekend Budapest presentation
Mobile Weekend Budapest presentationMobile Weekend Budapest presentation
Mobile Weekend Budapest presentation
 
以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界
 
Maintaining Your Code Clint Eastwood Style
Maintaining Your Code Clint Eastwood StyleMaintaining Your Code Clint Eastwood Style
Maintaining Your Code Clint Eastwood Style
 
Send email
Send emailSend email
Send email
 
Intel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionIntel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correction
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

Mock objects in the way it should be: Mockery

  • 1. Mocketry Mock objects in the way it should be https://github.com/dionisiydk/Mocketry
  • 2. Have no object for test?
  • 4. yourMock := Mock new • No context helpers • yourMock := context newMock • No duplicated mock names • yourMock := Mock named: ‘yourMock’ • Auto names from test variables (for debugging). • But you can specify name if you want • yourMock := Mock named: ‘mockName’
  • 5. Create multiple mocks at once [:mockA :mockB | «your code» ] runWithMocks
  • 6. Stub messages to objects mock stub someMessage willReturn: 100. mock someMessage should be: 100
  • 7. Stub messages to objects rect := 0@0 corner: 2@3. rect stub width willReturn: 1000 rect area should equal: 3000 “area = width * height”
  • 8. Stub messages to global objects DateAndTime stub now willReturn: #constantValue. DateAndTime now should be: #constantValue
  • 9. Stub messages to global objects Be careful with this DateAndTime stub now willReturn: #constantValue. DateAndTime now should be: #constantValue. DateAndTime recoverFromGHMutation All global stubs are recovered automatically on test completion
  • 10. Stub messages with arguments mock stub messageWith: arg1 and: arg2 mock stub messageWith: (Kind of: String) and: arg2 mock stub messageWith: Any and: arg2 mock stub messageWith: [:arg | true]
  • 11. Stub messages with arguments (mock stub messageWith: (Instance of: SmallInteger)) willReturn: #anyInt. (mock stub messageWith: (Kind of: String)) willReturn: #anyString. (mock stub messageWith: 10) willReturn: #ten. (mock messageWith: 10) should be: #ten. (mock messageWith: 20) should be: #anyInt. (mock messageWith: ‘test’) should be: #anyString Last defined expectation has more priority than previously defined
  • 12. Capture message arguments mock stub messageWith: Arg argName. mock messageWith: #argValue. Arg argName should be: #argValue. mock messageWith: #argValue2. Arg argName fromLastCall should be: #argValue2.
  • 13. Message sends usage rules mock stub someMessage willReturn: #default. mock stub someMessage willReturn: 1; useOnce. mock stub someMessage willReturn: 2; useTwice. mock someMessage should be: 2. mock someMessage should be: 2. mock someMessage should be: 1. mock someMessage should be: #default Last defined expectation has more priority than previously defined
  • 14. Expected actions mock stub someMessage willReturn: #result mock stub someMessage willRaise: Error new (mock stub someMessageWith: #arg) will: [#result] (mock stub someMessageWith: #arg1 and: #arg2) will: [:arg1 :arg2 | #result] mock stub someMessage willReturnValueFrom: #(result1 result2)
  • 15. Put extra conditions for message send mock := Mock new. mock someMessage when: [flag] is: true; when: [flag] is: (Kind of: Boolean); when: [flag] satisfy: [:value | true or: [false]]. flag := true. mock someMessage. flag := false. mock someMessage «will fail immediately on call»
  • 16. Stub messages to any object rect := 0@0 corner: 2@3. Any stub width willReturn: 1000 rect area should equal: 3000
  • 17. Stub messages to any object rect := 0@0 corner: 2@3. Any stub width willReturn: 1000 rect area should equal: 3000. «It’s not working :)» rect area should equal: 6
  • 18. Stub messages to any object rect := 0@0 corner: 2@3. rect stub. «It turns on message interception» Any stub width willReturn: 1000 rect area should equal: 3000.
  • 19. Stub any message to object mock := Mock new. mock stub anyMessage willReturn: 1000 mock someMessage should equal: 1000. mock someMessage2 should equal: 1000
  • 20. Stub any message to object rect := 0@0 corner: 2@3. rect stub anyMessage willReturn: 1000 rect width should equal: 1000. rect area should equal: 1000.
  • 21. Stub any message to any object mock := Mock new. rect := 0@0 corner: 2@3. rect stub. Any stub anyMessage willReturn: 1000 mock someMessage should equal: 1000. rect area should equal: 1000. mock anyMessage should equal: 1000.
  • 22. Stub messages to set of objects mock := Mock new. rect := 0@0 corner: 2@3. rect stub. (Kind of: Rectangle) stub width willReturn: 1000 mock width should not equal: 1000. rect width should equal: 1000 rect area should equal: 3000
  • 23. Stub messages to any mock mock := Mock new. rect := 0@0 corner: 2@3. rect stub. Mock stub width willReturn: 1000 mock width should equal: 1000. rect width should equal: 2
  • 24. Stub group of message sends mock := Mock new. rect := 0@0 corner: 2@3. rect stub. [mock someMessage willReturn: 100. rect width willReturn: 5000] should expect
  • 25. Without expectation mock returns new special mock on any message mock := Mock new. mock someMessage should beInstanceOf: MockForMessageReturn
  • 26. Automock is converted to false in boolean expressions mock := Mock new. autoMock := mock someMessage. autoMock ifTrue: [result := #trueDone] ifFalse: [result := #falseDone]. result should be: #falseDone. autoMock should be: false
  • 27. Automock is converted to zero in arithmetic expressions mock := Mock new. autoMock := mock someMessage. result := 1 + autoMock. result should be: 1. autoMock should be: 0
  • 28. Verify that messages were sent to objects mock someMessage. mock should receive someMessage. mock should not receive anotherMessage
  • 29. Verify that messages were sent objects rect := 0@0 corner: 2@3. rect area. rect should receive width
  • 30. Verify that messages were sent objects rect := 0@0 corner: 2@3. rect area. rect should receive width «it will fail :)» rect should not receive width
  • 31. Verify that messages were sent objects rect := 0@0 corner: 2@3. rect stub. «it turns on message interception» rect area. rect should receive width
  • 32. Verify messages with arguments mock messageWith: 10. mock messageWith: ‘test’. mock should receive messageWith: 10. mock should receive messageWith: (Instance of: SmallInteger). mock should receive messageWith: (Kind of: String).
  • 33. Verify messages to captured arguments mock stub messageWith: Arg rectangle. rect := 0@0 corner: 2@3. mock messageWith: rect. rect area. Arg rectangle should receive width. Arg rectangle should be: rect
  • 34. Verify how many times message was sent mock someMessage. mock someMessage. mock should receive someMessage twice
  • 35. Verify message send result rect := 0@0 corner: 2@3. rect stub. rect area. rect should receive area which should equal: 6
  • 36. Verify object sender result := mock someMessage. result should beReturnedFrom: [mock someMessage]. result should not beReturnedFrom: [mock someMessage2].
  • 37. Verify that messages were sent to any object mock := Mock new. rect := 0@0 corner: 2@3. rect stub. mock width. rect area. Any should receive width. Any should receive area. “fail because mock not received area”
  • 38. Verify that any message were sent to object mock := Mock new. mock should not receive anyMessage mock someMessage. mock should receive anyMessage
  • 39. Verify that any message were sent to object rect := 0@0 corner: 2@3. rect stub. «It turns on message interception» rect should not receive anyMessage rect area. rect should receive anyMessage
  • 40. Verify that any message were sent to any object mock := Mock new. rect := 0@0 corner: 2@3. rect stub. mock someMessage. Any should receive anyMessage. “fail because rect not received any message” rect area. Any should receive anyMessage.
  • 41. Verify that messages were sent to set of objects mock := Mock new. rect := 0@0 corner: 2@3. rect stub. rect2 := 0@0 corner: 5@6. rect2 stub. rect area. rect2 width. (Kind of: Rectangle) should receive width. (Kind of: Rectangle) should receive area. “fail because rect2 not received #area”
  • 42. Verify group of message sends mock := Mock new. rect := 0@0 corner: 2@3. rect stub. mock someMessage. rect area. [rect width. mock someMessage] should beDone
  • 43. Verify group of message sends in exact order mock := Mock new. rect := 0@0 corner: 2@3. rect stub. mock someMessage. rect area. [mock someMessage. rect width] should beDoneInOrder
  • 44. Verify expectations [:mockA :mockB | [mockB someMessage. mockA someMessage] should lenient satisfy: [mockA someMessage willReturn: 10. mockB someMessage willReturn: 400] ] runWithMocks
  • 45. Verify expectations in exact order [:mockA :mockB | [mockB someMessage. mockA someMessage] should strictly satisfy: [mockA someMessage willReturn: 10. mockB someMessage willReturn: 400] ] runWithMocks
  • 46. Let’s use Mocketry in Pharo tests The end