@@ -18,24 +18,24 @@ export class Example {
18
18
return 1337 ;
19
19
}
20
20
21
- set v ( x : string | null | undefined ) {
21
+ set v ( x : string | null | undefined ) {
22
22
}
23
23
24
- received ( stuff : number | string ) {
24
+ received ( stuff : number | string ) {
25
25
26
26
}
27
27
28
28
returnPromise ( ) {
29
29
return Promise . resolve ( new Dummy ( ) ) ;
30
30
}
31
31
32
- foo ( ) : string | undefined | null {
32
+ foo ( ) : string | undefined | null {
33
33
return 'stuff' ;
34
- }
35
-
36
- bar ( a : number , b ?: number ) : number {
37
- return a + b || 0
38
- }
34
+ }
35
+
36
+ bar ( a : number , b ?: number ) : number {
37
+ return a + b || 0
38
+ }
39
39
}
40
40
41
41
let instance : Example ;
@@ -46,20 +46,15 @@ function initialize() {
46
46
substitute = Substitute . for < Example > ( ) ;
47
47
} ;
48
48
49
- test ( 'can call received twice' , t => {
49
+ test ( 'can call received twice' , t => {
50
50
initialize ( ) ;
51
51
52
52
substitute . c ( 'blah' , 'fuzz' ) ;
53
+ const expectedMessage = 'Expected 1337 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
54
+ t . throws ( ( ) => substitute . received ( 1337 ) . c ( 'foo' , 'bar' ) , { message : expectedMessage } ) ;
53
55
54
- t . throws ( ( ) => substitute . received ( 1337 ) . c ( 'foo' , 'bar' ) ,
55
- `Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
56
- All calls received to method c:
57
- -> call with arguments ['blah', 'fuzz']` ) ;
58
-
59
- t . throws ( ( ) => substitute . received ( 2117 ) . c ( 'foo' , 'bar' ) ,
60
- `Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
61
- All calls received to method c:
62
- -> call with arguments ['blah', 'fuzz']` ) ;
56
+ const expectedMessage2 = 'Expected 2117 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
57
+ t . throws ( ( ) => substitute . received ( 2117 ) . c ( 'foo' , 'bar' ) , { message : expectedMessage2 } ) ;
63
58
} ) ;
64
59
65
60
test ( 'class string field get returns' , t => {
@@ -75,7 +70,7 @@ test('class string field get returns', t => {
75
70
76
71
test ( 'class with method called "received" can be used for call count verification when proxies are suspended' , t => {
77
72
initialize ( ) ;
78
-
73
+
79
74
Substitute . disableFor ( substitute ) . received ( 2 ) ;
80
75
81
76
t . throws ( ( ) => substitute . received ( 2 ) . received ( 2 ) ) ;
@@ -84,7 +79,7 @@ test('class with method called "received" can be used for call count verificatio
84
79
85
80
test ( 'class with method called "received" can be used for call count verification' , t => {
86
81
initialize ( ) ;
87
-
82
+
88
83
Substitute . disableFor ( substitute ) . received ( 'foo' ) ;
89
84
90
85
t . notThrows ( ( ) => substitute . received ( 1 ) . received ( 'foo' ) ) ;
@@ -93,15 +88,15 @@ test('class with method called "received" can be used for call count verificatio
93
88
94
89
test ( 'partial mocks using function mimicks with all args' , t => {
95
90
initialize ( ) ;
96
-
91
+
97
92
substitute . c ( Arg . all ( ) ) . mimicks ( instance . c ) ;
98
93
99
94
t . deepEqual ( substitute . c ( 'a' , 'b' ) , 'hello a world (b)' ) ;
100
95
} ) ;
101
96
102
97
test ( 'class string field get received' , t => {
103
98
initialize ( ) ;
104
-
99
+
105
100
void substitute . a ;
106
101
void substitute . a ;
107
102
void substitute . a ;
@@ -114,13 +109,13 @@ test('class string field get received', t => {
114
109
115
110
test ( 'class string field set received' , t => {
116
111
initialize ( ) ;
117
-
112
+
118
113
substitute . v = undefined ;
119
114
substitute . v = null ;
120
115
substitute . v = 'hello' ;
121
116
substitute . v = 'hello' ;
122
117
substitute . v = 'world' ;
123
-
118
+
124
119
t . notThrows ( ( ) => substitute . received ( ) . v = 'hello' ) ;
125
120
t . notThrows ( ( ) => substitute . received ( 5 ) . v = Arg . any ( ) ) ;
126
121
t . notThrows ( ( ) => substitute . received ( ) . v = Arg . any ( ) ) ;
@@ -135,9 +130,9 @@ test('class string field set received', t => {
135
130
136
131
test ( 'class method returns with placeholder args' , t => {
137
132
initialize ( ) ;
138
-
133
+
139
134
substitute . c ( Arg . any ( ) , "there" ) . returns ( "blah" , "haha" ) ;
140
-
135
+
141
136
t . is ( substitute . c ( "hi" , "there" ) , 'blah' ) ;
142
137
t . is ( substitute . c ( "his" , "there" ) , 'haha' ) ;
143
138
t . is < any > ( substitute . c ( "his" , "there" ) , void 0 ) ;
@@ -146,15 +141,15 @@ test('class method returns with placeholder args', t => {
146
141
147
142
test ( 'partial mocks using function mimicks with specific args' , t => {
148
143
initialize ( ) ;
149
-
144
+
150
145
substitute . c ( 'a' , 'b' ) . mimicks ( instance . c ) ;
151
146
152
147
t . is ( substitute . c ( 'a' , 'b' ) , 'hello a world (b)' ) ;
153
148
} ) ;
154
149
155
150
test ( 'class method returns with specific args' , t => {
156
151
initialize ( ) ;
157
-
152
+
158
153
substitute . c ( "hi" , "there" ) . returns ( "blah" , "haha" ) ;
159
154
160
155
t . is ( substitute . c ( "hi" , "there" ) , 'blah' ) ;
@@ -165,32 +160,32 @@ test('class method returns with specific args', t => {
165
160
166
161
test ( 'returning other fake from promise works' , async t => {
167
162
initialize ( ) ;
168
-
163
+
169
164
const otherSubstitute = Substitute . for < Dummy > ( ) ;
170
165
substitute . returnPromise ( ) . returns ( Promise . resolve ( otherSubstitute ) ) ;
171
- t . is ( otherSubstitute , await substitute . returnPromise ( ) ) ;
166
+ t . is ( otherSubstitute , await substitute . returnPromise ( ) ) ;
172
167
} ) ;
173
168
174
169
test ( 'returning resolved promises works' , async t => {
175
170
initialize ( ) ;
176
-
171
+
177
172
substitute . returnPromise ( ) . returns ( Promise . resolve ( 1338 ) ) ;
178
173
179
174
t . is ( 1338 , await substitute . returnPromise ( ) ) ;
180
175
} ) ;
181
176
182
177
test ( 'class void returns' , t => {
183
178
initialize ( ) ;
184
-
179
+
185
180
substitute . foo ( ) . returns ( void 0 , null ) ;
186
181
187
182
t . is ( substitute . foo ( ) , void 0 ) ;
188
183
t . is ( substitute . foo ( ) , null ) ;
189
- } ) ;
184
+ } ) ;
190
185
191
186
test ( 'class method received' , t => {
192
187
initialize ( ) ;
193
-
188
+
194
189
void substitute . c ( "hi" , "there" ) ;
195
190
void substitute . c ( "hi" , "the1re" ) ;
196
191
void substitute . c ( "hi" , "there" ) ;
@@ -201,92 +196,92 @@ test('class method received', t => {
201
196
t . notThrows ( ( ) => substitute . received ( 1 ) . c ( 'hi' , 'the1re' ) ) ;
202
197
t . notThrows ( ( ) => substitute . received ( ) . c ( 'hi' , 'there' ) ) ;
203
198
204
- t . throws ( ( ) => substitute . received ( 7 ) . c ( 'hi' , 'there' ) ,
205
- `Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.
206
- All calls received to method c:
207
- -> call with arguments ['hi', 'there']
208
- -> call with arguments ['hi', 'the1re']
209
- -> call with arguments ['hi', 'there']
210
- -> call with arguments ['hi', 'there']
211
- -> call with arguments [ 'hi', 'there']` ) ;
199
+ const expectedMessage = 'Expected 7 calls to the method c with arguments [\ 'hi\ ', \ 'there\'], but received 4 of such calls.\n' +
200
+ 'All calls received to method c:\n' +
201
+ '-> call with arguments [\'hi\', \'there\']\n' +
202
+ ' -> call with arguments [\ 'hi\ ', \'the1re\']\n' +
203
+ ' -> call with arguments [\ 'hi\ ', \'there\']\n' +
204
+ ' -> call with arguments [\ 'hi\ ', \ 'there\']\n' +
205
+ ' -> call with arguments [\ 'hi\ ', \ 'there\']'
206
+ t . throws ( ( ) => { substitute . received ( 7 ) . c ( 'hi' , 'there' ) } , { message : expectedMessage } ) ;
212
207
} ) ;
213
208
214
209
test ( 'received call matches after partial mocks using property instance mimicks' , t => {
215
210
initialize ( ) ;
216
-
211
+
217
212
substitute . d . mimicks ( ( ) => instance . d ) ;
218
213
substitute . c ( 'lala' , 'bar' ) ;
219
214
220
215
substitute . received ( 1 ) . c ( 'lala' , 'bar' ) ;
221
216
substitute . received ( 1 ) . c ( 'lala' , 'bar' ) ;
222
217
223
218
t . notThrows ( ( ) => substitute . received ( 1 ) . c ( 'lala' , 'bar' ) ) ;
224
- t . throws ( ( ) => substitute . received ( 2 ) . c ( 'lala' , 'bar' ) ,
225
- `Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.
226
- All calls received to method c:
227
- -> call with arguments [ 'lala', 'bar']` ) ;
228
-
219
+ const expectedMessage = 'Expected 2 calls to the method c with arguments [\ 'lala\ ', \ 'bar\'], but received 1 of such call.\n' +
220
+ 'All calls received to method c:\n' +
221
+ '-> call with arguments [\'lala\', \'bar\']'
222
+ t . throws ( ( ) => substitute . received ( 2 ) . c ( 'lala' , 'bar' ) , { message : expectedMessage } ) ;
223
+
229
224
t . deepEqual ( substitute . d , 1337 ) ;
230
225
} ) ;
231
226
232
227
test ( 'partial mocks using property instance mimicks' , t => {
233
228
initialize ( ) ;
234
-
229
+
235
230
substitute . d . mimicks ( ( ) => instance . d ) ;
236
231
237
232
t . deepEqual ( substitute . d , 1337 ) ;
238
233
} ) ;
239
234
240
235
test ( 'verifying with more arguments fails' , t => {
241
- initialize ( )
242
- substitute . bar ( 1 )
243
- substitute . received ( ) . bar ( 1 )
244
- t . throws ( ( ) => substitute . received ( ) . bar ( 1 , 2 ) )
236
+ initialize ( )
237
+ substitute . bar ( 1 )
238
+ substitute . received ( ) . bar ( 1 )
239
+ t . throws ( ( ) => substitute . received ( ) . bar ( 1 , 2 ) )
245
240
} )
246
241
247
242
test ( 'verifying with less arguments fails' , t => {
248
- initialize ( )
249
- substitute . bar ( 1 , 2 )
250
- substitute . received ( ) . bar ( 1 , 2 )
251
- t . throws ( ( ) => substitute . received ( ) . bar ( 1 ) )
243
+ initialize ( )
244
+ substitute . bar ( 1 , 2 )
245
+ substitute . received ( ) . bar ( 1 , 2 )
246
+ t . throws ( ( ) => substitute . received ( ) . bar ( 1 ) )
252
247
} )
253
248
254
249
test ( 'return with more arguments is not matched fails' , t => {
255
- initialize ( )
256
- substitute . bar ( 1 , 2 ) . returns ( 3 )
257
- t . is ( 3 , substitute . bar ( 1 , 2 ) )
258
- t . is ( 'function' , typeof ( substitute . bar ( 1 ) ) )
250
+ initialize ( )
251
+ substitute . bar ( 1 , 2 ) . returns ( 3 )
252
+ t . is ( 3 , substitute . bar ( 1 , 2 ) )
253
+ t . is ( 'function' , typeof ( substitute . bar ( 1 ) ) )
259
254
} )
260
255
261
256
test ( 'return with less arguments is not matched' , t => {
262
- initialize ( )
263
- substitute . bar ( 1 ) . returns ( 3 )
264
- t . is ( 3 , substitute . bar ( 1 ) )
265
- t . is ( 'function' , typeof ( substitute . bar ( 1 , 2 ) . toString ) )
257
+ initialize ( )
258
+ substitute . bar ( 1 ) . returns ( 3 )
259
+ t . is ( 3 , substitute . bar ( 1 ) )
260
+ t . is ( 'function' , typeof ( substitute . bar ( 1 , 2 ) . toString ) )
266
261
} )
267
262
268
263
test ( 'can stub multiple primitive return values' , t => {
269
- initialize ( )
270
- substitute . bar ( 1 ) . returns ( 2 )
271
- substitute . bar ( 2 ) . returns ( 3 )
272
- t . is ( 2 , substitute . bar ( 1 ) )
273
- t . is ( 3 , substitute . bar ( 2 ) )
264
+ initialize ( )
265
+ substitute . bar ( 1 ) . returns ( 2 )
266
+ substitute . bar ( 2 ) . returns ( 3 )
267
+ t . is ( 2 , substitute . bar ( 1 ) )
268
+ t . is ( 3 , substitute . bar ( 2 ) )
274
269
} )
275
270
276
271
test ( 'can stub multiple Arg values' , t => {
277
- initialize ( )
278
- substitute . bar ( Arg . is ( x => x % 2 === 0 ) ) . returns ( 1 )
279
- substitute . bar ( Arg . is ( x => x % 2 !== 0 ) ) . returns ( 2 )
280
- t . is ( 1 , substitute . bar ( 4 ) )
281
- t . is ( 2 , substitute . bar ( 5 ) )
272
+ initialize ( )
273
+ substitute . bar ( Arg . is ( x => x % 2 === 0 ) ) . returns ( 1 )
274
+ substitute . bar ( Arg . is ( x => x % 2 !== 0 ) ) . returns ( 2 )
275
+ t . is ( 1 , substitute . bar ( 4 ) )
276
+ t . is ( 2 , substitute . bar ( 5 ) )
282
277
} )
283
278
284
279
285
280
test . skip ( 'can stub primitive & Arg values' , t => {
286
- initialize ( )
287
- substitute . bar ( 1 ) . returns ( 2 )
288
- substitute . bar ( Arg . any ( ) ) . returns ( 3 ) // throws 'substitute.bar(...).returns is not a function'
289
- t . is ( 5 , substitute . bar ( 2 ) )
290
- t . is ( 2 , substitute . bar ( 1 ) )
291
- t . is ( 3 , substitute . bar ( 2 ) )
281
+ initialize ( )
282
+ substitute . bar ( 1 ) . returns ( 2 )
283
+ substitute . bar ( Arg . any ( ) ) . returns ( 3 ) // throws 'substitute.bar(...).returns is not a function'
284
+ t . is ( 5 , substitute . bar ( 2 ) )
285
+ t . is ( 2 , substitute . bar ( 1 ) )
286
+ t . is ( 3 , substitute . bar ( 2 ) )
292
287
} )
0 commit comments