Mocketry is a mocking library for Pharo that allows creating mock objects to stub messages and verify message sends in tests. Some key features include:
- Creating mocks with Mock new and stubbing messages to return values or raise errors
- Stubbing messages on any object or globally
- Capturing message arguments and verifying sends with arguments
- Grouping expectations and verifies to run together
- Checking message sends occurred the correct number of times
- Flexible argument matching for stubs
- Automated test cleanup of global stubs
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’
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
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