Skip to content
This repository was archived by the owner on Oct 2, 2025. It is now read-only.

Commit 4217e2e

Browse files
author
Felix Jendrusch
committed
Update README.md
1 parent e18261b commit 4217e2e

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let mock = Mock<[Int]>()
4343
mock.expect(matches([any(), matches { $0 > 0 }])) // expects [_, n > 0]
4444
mock.record([0, 1]) // succeeds
4545
mock.verify() // succeeds
46-
mock.record([1, 0]) // fails (fast)
46+
mock.record([1, 0]) // unexpected, fails (fast)
4747
```
4848

4949
The order of expectations may also be ignored:
@@ -55,7 +55,7 @@ mock.expect(matches([1, 0]))
5555
mock.record([1, 0]) // succeeds
5656
mock.record([0, 1]) // succeeds
5757
mock.verify() // succeeds
58-
mock.record([0, 0]) // fails (fast)
58+
mock.record([0, 0]) // unexpected, fails (fast)
5959
```
6060

6161
### Nice mocks
@@ -66,10 +66,10 @@ Nice mocks allow unexpected interactions while still respecting the order of exp
6666
let mock = Mock<[Int?]>(strict: false)
6767
mock.expect(matches([some(0)]))
6868
mock.expect(matches([some(any())]))
69-
mock.record([nil]) // succeeds
70-
mock.verify() // fails
71-
mock.record([1]) // fails (fast)
69+
mock.record([nil]) // unexpected, ignored
70+
mock.record([1]) // out of order, ignored
7271
mock.record([0]) // succeeds
72+
mock.verify() // fails
7373
mock.record([1]) // succeeds
7474
mock.verify() // succeeds
7575
```
@@ -80,6 +80,7 @@ Of course, nice mocks can ignore the order of expectations too:
8080
let mock = Mock<[String: Int?]>(strict: false, ordered: false)
8181
mock.expect(matches(["zero": some(0)]))
8282
mock.expect(matches(["none": none()]))
83+
mock.record(["some": 1]) // unexpected, ignored
8384
mock.record(["none": nil]) // succeeds
8485
mock.record(["zero": 0]) // succeeds
8586
mock.verify() // succeeds
@@ -92,7 +93,7 @@ In addition to normal expectations, nice mocks allow negative expectations to be
9293
```swift
9394
let mock = Mock<Int>(nice: true)
9495
mock.reject(0)
95-
mock.record(0) // fails (fast)
96+
mock.record(0) // rejected, fails (fast)
9697
```
9798

9899
### Verification with delay
@@ -103,7 +104,8 @@ Verification may also be performed with a delay, allowing expectations to be ful
103104
let mock = Mock<Int>()
104105
mock.expect(1)
105106

106-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
107+
let when = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))
108+
dispatch_after(when, dispatch_get_main_queue()) {
107109
mock.record(1) // succeeds
108110
}
109111

@@ -116,17 +118,17 @@ Stubs, when invoked, return a value based on their set up behavior, or, if an in
116118

117119
```swift
118120
let stub = Stub<(Int, Int), Int>()
119-
let behavior = stub.on(equals((4, 3)), returnValue: 9)
120-
stub.on(matches((any(), any()))) { $0.0 + $0.1 }
121-
try! stub.invoke((4, 3)) // returns 9
122-
try! stub.invoke((4, 4)) // returns 8
121+
let behavior = stub.on(equals((2, 3)), returnValue: 4)
122+
stub.on(matches((any(), any()))) { x, y in x * y }
123+
try! stub.invoke((2, 3)) // returns 4
124+
try! stub.invoke((3, 3)) // returns 9
123125
```
124126

125127
Behavior may also be disposed:
126128

127129
```swift
128130
behavior.dispose()
129-
try! stub.invoke((4, 3)) // returns 7
131+
try! stub.invoke((2, 3)) // returns 6
130132
```
131133

132134
## Example

0 commit comments

Comments
 (0)