forked from devlooped/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtectedAsMockFixture.cs
559 lines (440 loc) · 13.1 KB
/
ProtectedAsMockFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
using System;
using System.Collections.Generic;
using Moq.Protected;
using Xunit;
namespace Moq.Tests
{
public class ProtectedAsMockFixture
{
private Mock<Foo> mock;
private IProtectedAsMock<Foo, Fooish> protectedMock;
public ProtectedAsMockFixture()
{
this.mock = new Mock<Foo>();
this.protectedMock = this.mock.Protected().As<Fooish>();
}
[Fact]
public void Setup_throws_when_expression_null()
{
var actual = Record.Exception(() =>
{
this.protectedMock.Setup(null);
});
Assert.IsType<ArgumentNullException>(actual);
}
[Fact]
public void Setup_throws_ArgumentException_when_expression_contains_nonexistent_method()
{
var actual = Record.Exception(() =>
{
this.protectedMock.Setup(m => m.NonExistentMethod());
});
Assert.IsType<ArgumentException>(actual);
Assert.Contains("does not have matching protected member", actual.Message);
}
[Fact]
public void Setup_throws_ArgumentException_when_expression_contains_nonexistent_property()
{
var actual = Record.Exception(() =>
{
this.protectedMock.Setup(m => m.NonExistentProperty);
});
Assert.IsType<ArgumentException>(actual);
Assert.Contains("does not have matching protected member", actual.Message);
}
[Fact]
public void Setup_TResult_throws_when_expression_null()
{
var actual = Record.Exception(() => this.protectedMock.Setup<object>(null));
Assert.IsType<ArgumentNullException>(actual);
}
[Fact]
public void Setup_can_setup_simple_method()
{
bool doSomethingImplInvoked = false;
this.protectedMock.Setup(m => m.DoSomethingImpl()).Callback(() => doSomethingImplInvoked = true);
this.mock.Object.DoSomething();
Assert.True(doSomethingImplInvoked);
}
[Fact]
public void Setup_TResult_can_setup_simple_method_with_return_value()
{
this.protectedMock.Setup(m => m.GetSomethingImpl()).Returns(() => 42);
var actual = this.mock.Object.GetSomething();
Assert.Equal(42, actual);
}
[Fact]
public void Setup_can_match_exact_arguments()
{
bool doSomethingImplInvoked = false;
this.protectedMock.Setup(m => m.DoSomethingImpl(1)).Callback(() => doSomethingImplInvoked = true);
this.mock.Object.DoSomething(0);
Assert.False(doSomethingImplInvoked);
this.mock.Object.DoSomething(1);
Assert.True(doSomethingImplInvoked);
}
[Fact]
public void Setup_can_involve_matchers()
{
bool doSomethingImplInvoked = false;
this.protectedMock.Setup(m => m.DoSomethingImpl(It.Is<int>(i => i == 1))).Callback(() => doSomethingImplInvoked = true);
this.mock.Object.DoSomething(0);
Assert.False(doSomethingImplInvoked);
this.mock.Object.DoSomething(1);
Assert.True(doSomethingImplInvoked);
}
[Fact]
public void Setup_can_setup_generic_method()
{
var handledMessages = new List<string>();
var mock = new Mock<MessageHandlerBase>();
mock.Protected().As<MessageHandlerBaseish>()
.Setup(m => m.HandleImpl(It.IsAny<string>()))
.Callback((string message) =>
{
handledMessages.Add(message);
});
mock.Object.Handle("Hello world.", 3);
Assert.Contains("Hello world.", handledMessages);
Assert.Equal(3, handledMessages.Count);
}
[Fact]
public void Setup_can_automock()
{
this.protectedMock.Setup(m => m.Nested.Method(1)).Returns(123);
Assert.Equal(123, mock.Object.GetNested().Method(1));
}
[Fact]
public void SetupGet_can_setup_readonly_property()
{
this.protectedMock.SetupGet(m => m.ReadOnlyPropertyImpl).Returns(42);
var actual = this.mock.Object.ReadOnlyProperty;
Assert.Equal(42, actual);
}
[Fact]
public void SetupGet_can_setup_readwrite_property()
{
this.protectedMock.SetupGet(m => m.ReadWritePropertyImpl).Returns(42);
var actual = this.mock.Object.ReadWriteProperty;
Assert.Equal(42, actual);
}
[Fact]
public void SetUpGet_can_automock()
{
this.protectedMock.SetupGet(m => m.Nested.Value).Returns(42);
var actual = mock.Object.GetNested().Value;
Assert.Equal(42, actual);
}
[Fact]
public void SetupGet_can_setup_virtual_property()
{
this.protectedMock.SetupGet(m => m.VirtualGet).Returns(42);
var actual = mock.Object.GetVirtual();
Assert.Equal(42, actual);
}
[Fact]
public void SetupProperty_can_setup_readwrite_property()
{
this.protectedMock.SetupProperty(m => m.ReadWritePropertyImpl, 42);
var actualBeforeSetting = this.mock.Object.ReadWriteProperty;
this.mock.Object.ReadWriteProperty = 17;
var actualAfterSetting = this.mock.Object.ReadWriteProperty;
Assert.Equal(42, actualBeforeSetting);
Assert.Equal(17, actualAfterSetting);
}
[Fact]
public void SetupProperty_cannot_setup_readonly_property()
{
var exception = Record.Exception(() =>
{
this.protectedMock.SetupProperty(m => m.ReadOnlyPropertyImpl);
});
Assert.NotNull(exception);
}
[Fact]
public void SetupSequence_TResult_can_setup_property()
{
this.protectedMock.SetupSequence(m => m.ReadOnlyPropertyImpl)
.Returns(1)
.Throws(new InvalidOperationException())
.Returns(3);
var actual = new List<int>();
actual.Add(this.mock.Object.ReadOnlyProperty);
var exception = Record.Exception(() =>
{
actual.Add(this.mock.Object.ReadOnlyProperty);
});
actual.Add(this.mock.Object.ReadOnlyProperty);
Assert.Equal(new[] { 1, 3 }, actual);
Assert.IsType<InvalidOperationException>(exception);
}
[Fact]
public void SetupSequence_can_setup_actions()
{
this.protectedMock.SetupSequence(m => m.DoSomethingImpl())
.Pass()
.Pass().
Throws(new InvalidOperationException());
this.mock.Object.DoSomething();
this.mock.Object.DoSomething();
var exception = Record.Exception(() =>
{
this.mock.Object.DoSomething();
});
this.mock.Object.DoSomething();
Assert.IsType<InvalidOperationException>(exception);
}
[Fact]
public void SetUpSet_should_setup_setters()
{
this.protectedMock.SetupSet(fish => fish.ReadWritePropertyImpl = 999).Throws(ExpectedException.Instance);
mock.Object.ReadWriteProperty = 123;
Assert.Throws<ExpectedException>(() => mock.Object.ReadWriteProperty = 999);
}
[Fact]
public void SetUpSet_should_setup_setters_with_property_type()
{
int value = 0;
this.protectedMock.SetupSet<int>(fish => fish.ReadWritePropertyImpl = 999).Callback(i => value = i);
mock.Object.ReadWriteProperty = 123;
Assert.Equal(0, value);
mock.Object.ReadWriteProperty = 999;
Assert.Equal(999, value);
}
[Fact]
public void SetUpSet_should_work_recursively()
{
this.protectedMock.SetupSet(f => f.Nested.Value = 999).Throws(ExpectedException.Instance);
mock.Object.GetNested().Value = 1;
Assert.Throws<ExpectedException>(() => mock.Object.GetNested().Value = 999);
}
[Fact]
public void SetUpSet_Should_Work_With_Indexers()
{
this.protectedMock.SetupSet(
o => o[
It.IsInRange(0, 5, Range.Inclusive),
It.IsIn("Bad", "JustAsBad")
] = It.Is<int>(i => i > 10)
).Throws(ExpectedException.Instance);
mock.Object.SetMultipleIndexer(1, "Ok", 999);
Assert.Throws<ExpectedException>(() => mock.Object.SetMultipleIndexer(1, "Bad", 999));
}
[Fact]
public void SetupSet_can_setup_virtual_property()
{
this.protectedMock.SetupSet(m => m.VirtualSet = 999).Throws(new ExpectedException());
mock.Object.SetVirtual(123);
Assert.Throws<ExpectedException>(() => mock.Object.SetVirtual(999));
}
[Fact]
public void VerifySet_Should_Work()
{
void VerifySet(Times? times = null,string failMessage = null)
{
this.protectedMock.VerifySet(
o => o[
It.IsInRange(0, 5, Moq.Range.Inclusive),
It.IsIn("Bad", "JustAsBad")
] = It.Is<int>(i => i > 10),
times,
failMessage
);
}
VerifySet(Times.Never());
mock.Object.SetMultipleIndexer(1, "Ok", 1);
VerifySet(Times.Never());
Assert.Throws<MockException>(() => VerifySet()); // AtLeastOnce
mock.Object.SetMultipleIndexer(1, "Bad", 999);
VerifySet(); // AtLeastOnce
mock.Object.SetMultipleIndexer(1, "JustAsBad", 12);
VerifySet(Times.Exactly(2));
Assert.Throws<MockException>(() => VerifySet(Times.AtMostOnce()));
var mockException = Assert.Throws<MockException>(() => VerifySet(Times.AtMostOnce(),"custom fail message"));
Assert.StartsWith("custom fail message", mockException.Message);
}
[Fact]
public void Verify_can_verify_method_invocations()
{
this.mock.Object.DoSomething();
this.protectedMock.Verify(m => m.DoSomethingImpl());
}
[Fact]
public void Verify_throws_if_invocation_not_occurred()
{
var exception = Record.Exception(() =>
{
this.protectedMock.Verify(m => m.DoSomethingImpl());
});
Assert.IsType<MockException>(exception);
}
[Fact]
public void Verify_can_verify_method_invocations_times()
{
this.mock.Object.DoSomething();
this.mock.Object.DoSomething();
this.mock.Object.DoSomething();
this.protectedMock.Verify(m => m.DoSomethingImpl(), Times.Exactly(3));
}
[Fact]
public void Verify_includes_failure_message_in_exception()
{
this.mock.Object.DoSomething();
this.mock.Object.DoSomething();
var exception = Record.Exception(() =>
{
this.protectedMock.Verify(m => m.DoSomethingImpl(), Times.Exactly(3), "Wasn't called three times.");
});
Assert.IsType<MockException>(exception);
Assert.Contains("Wasn't called three times.", exception.Message);
}
[Fact]
public void Verify_can_verify_generic_method()
{
var mock = new Mock<MessageHandlerBase>();
mock.Object.Handle("Hello world.", 3);
mock.Protected().As<MessageHandlerBaseish>().Verify(m => m.HandleImpl("Hello world."), Times.Exactly(3));
}
[Fact]
public void VerifyGet_can_verify_properties()
{
var _ = this.mock.Object.ReadOnlyProperty;
this.protectedMock.VerifyGet(m => m.ReadOnlyPropertyImpl);
}
[Fact]
public void VerifyGet_throws_if_property_query_not_occurred()
{
var _ = this.mock.Object.ReadOnlyProperty;
var exception = Record.Exception(() =>
{
this.protectedMock.VerifyGet(m => m.ReadOnlyPropertyImpl, Times.AtLeast(2));
});
}
[Fact]
public void VerifyGet_includes_failure_message_in_exception()
{
var exception = Record.Exception(() =>
{
this.protectedMock.VerifyGet(m => m.ReadOnlyPropertyImpl, Times.Once(), "Was not queried.");
});
Assert.IsType<MockException>(exception);
Assert.Contains("Was not queried.", exception.Message);
}
public interface INested
{
int Value { get; set; }
int Method(int value);
}
public abstract class Foo
{
protected Foo()
{
}
private int _virtualSet;
public virtual int VirtualSet
{
get
{
return _virtualSet;
}
protected set
{
_virtualSet = value;
}
}
public void SetVirtual(int value)
{
VirtualSet = value;
}
private int _virtualGet;
public virtual int VirtualGet
{
protected get
{
return _virtualGet;
}
set
{
_virtualGet = value;
}
}
public int GetVirtual()
{
return VirtualGet;
}
public int ReadOnlyProperty => this.ReadOnlyPropertyImpl;
public int ReadWriteProperty
{
get => this.ReadWritePropertyImpl;
set => this.ReadWritePropertyImpl = value;
}
public void DoSomething()
{
this.DoSomethingImpl();
}
public void DoSomething(int arg)
{
this.DoSomethingImpl(arg);
}
public int GetSomething()
{
return this.GetSomethingImpl();
}
protected abstract int ReadOnlyPropertyImpl { get; }
protected abstract int ReadWritePropertyImpl { get; set; }
protected abstract void DoSomethingImpl();
protected abstract void DoSomethingImpl(int arg);
protected abstract int GetSomethingImpl();
protected abstract INested Nested { get; set; }
public INested GetNested()
{
return Nested;
}
protected abstract int this[int i, string s] { get; set; }
public void SetMultipleIndexer(int index, string sIndex, int value)
{
this[index, sIndex] = value;
}
public int GetMultipleIndexer(int index, string sIndex)
{
return this[index, sIndex];
}
}
public interface Fooish
{
int VirtualGet { get; set; }
int VirtualSet { get; set; }
int ReadOnlyPropertyImpl { get; }
int ReadWritePropertyImpl { get; set; }
int NonExistentProperty { get; }
void DoSomethingImpl();
void DoSomethingImpl(int arg);
int GetSomethingImpl();
void NonExistentMethod();
INested Nested { get; set; }
int this[int i, string s] { get; set; }
}
public abstract class MessageHandlerBase
{
public void Handle<TMessage>(TMessage message, int count)
{
for (int i = 0; i < count; ++i)
{
this.HandleImpl(message);
}
}
protected abstract void HandleImpl<TMessage>(TMessage message);
}
public interface MessageHandlerBaseish
{
void HandleImpl<TMessage>(TMessage message);
}
public class ExpectedException : Exception
{
private static ExpectedException expectedException = new ExpectedException();
public static ExpectedException Instance => expectedException;
}
}
}