-
Notifications
You must be signed in to change notification settings - Fork 4
/
2_Methods.cs
executable file
·363 lines (297 loc) · 10.1 KB
/
2_Methods.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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MoqKoans.KoansHelpers;
namespace MoqKoans
{
[TestClass]
public class Moq2_Methods : Koan
{
// This is an interface that we will be mocking.
public interface IVolume
{
int Louder(int amount);
int Quieter(int amount);
string CurrentVolume();
}
[TestMethod]
public void IfAMockIsCreatedWithTheLooseBehaviorThenAllMethodsReturnTheirDefaultValues()
{
var volumeMock = new Mock<IVolume>(MockBehavior.Loose);
var volume = volumeMock.Object;
Assert.AreEqual(___, volume.CurrentVolume());
Assert.AreEqual(___, volume.Louder(0));
Assert.AreEqual(___, volume.Quieter(0));
}
[TestMethod]
public void IfNotSpecifiedTheDefaultBehaviorIsLoose()
{
var volumeMock = new Mock<IVolume>();
var volume = volumeMock.Object;
Assert.AreEqual(___, volume.CurrentVolume());
Assert.AreEqual(___, volume.Louder(0));
Assert.AreEqual(___, volume.Quieter(0));
}
[TestMethod]
public void CanPassAnyValueToAMockMethod()
{
IVolume volume = new Mock<IVolume>().Object;
Assert.AreEqual(___, volume.Louder(0));
Assert.AreEqual(___, volume.Louder(50));
Assert.AreEqual(___, volume.Louder(-12));
}
[TestMethod]
public void IfAMockIsCreatedWithTheStrictBehaviorThenAllMethodsThrowAnExceptionIfCalled()
{
var volumeMock = new Mock<IVolume>(MockBehavior.Strict);
var volume = volumeMock.Object;
var exceptionWasThrown = false;
try
{
volume.CurrentVolume();
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
[TestMethod]
public void TheSetupMethodChangesTheBehaviorOfAMockedMethod()
{
var volumeMock = new Mock<IVolume>(MockBehavior.Strict);
var volume = volumeMock.Object;
// This tells Moq that the CurrentVolume() method should return string.Empty when called.
volumeMock.Setup(m => m.CurrentVolume()).Returns(string.Empty);
var exceptionWasThrown = false;
try
{
volume.CurrentVolume();
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
[TestMethod]
public void TheSetupMethodCanSpecifyAReturnValueForTheMethod()
{
var mock = new Mock<IVolume>(MockBehavior.Strict);
mock.Setup(m => m.CurrentVolume()).Returns("100");
Assert.AreEqual(___, mock.Object.CurrentVolume());
}
[TestMethod]
public void WriteASetupMethodToMakeCurrentVolumeReturnTheExpectedValue()
{
var mock = new Mock<IVolume>();
mock.___();
Assert.AreEqual("yay!", mock.Object.CurrentVolume());
}
[TestMethod]
public void MultipleSetupMethodsForTheSameMethodUsesTheLastOneRun()
{
var mock = new Mock<IVolume>();
mock.Setup(m => m.CurrentVolume()).Returns("10");
mock.Setup(m => m.CurrentVolume()).Returns("50");
Assert.AreEqual(___, mock.Object.CurrentVolume());
}
[TestMethod]
public void TheValuePassedToReturnsIsEvaluatedWhenReturnsIsCalledNotWhenTheMethodIsCalled()
{
// this behavior often trips up new users to Moq!
var currentVolume = 50;
var mock = new Mock<IVolume>();
mock.Setup(m => m.CurrentVolume()).Returns(currentVolume.ToString());
Assert.AreEqual(___, mock.Object.CurrentVolume());
currentVolume = 10;
Assert.AreEqual(___, mock.Object.CurrentVolume());
}
[TestMethod]
public void TheReturnsMethodCanAlsoBeGivenADelegateOrLambdaToEvaluateEachTimeTheMethodIsCalled()
{
var currentVolume = 50;
var mock = new Mock<IVolume>();
mock.Setup(m => m.CurrentVolume()).Returns(() => currentVolume.ToString());
Assert.AreEqual(___, mock.Object.CurrentVolume());
currentVolume = 10;
Assert.AreEqual(___, mock.Object.CurrentVolume());
// Ask yourself; why does this behave differently than the previous test?
}
[TestMethod]
public void LambdasPassedToReturnsCanDoComplicatedThings()
{
var currentVolume = 50;
var mock = new Mock<IVolume>();
mock.Setup(m => m.Louder(It.IsAny<int>()))
.Returns<int>(input =>
{
var newVolume = currentVolume + input;
if (newVolume > 50)
return 100;
return 0;
});
Assert.AreEqual(___, mock.Object.Louder(10));
Assert.AreEqual(___, mock.Object.Louder(-10));
}
[TestMethod]
public void WhenAMethodTakesInputParametersTheSetupMethodCanHandleThem_ItIsAny_MatchesAllValues()
{
var mock = new Mock<IVolume>();
// This call to .Setup() tells Moq that when any int is passed to Louder(), return 10.
mock.Setup(m => m.Louder(It.IsAny<int>())).Returns(10);
Assert.AreEqual(___, mock.Object.Louder(0));
Assert.AreEqual(___, mock.Object.Louder(50));
Assert.AreEqual(___, mock.Object.Louder(-2));
}
[TestMethod]
public void ItIs_CanUseASpecificValue()
{
var mock = new Mock<IVolume>();
mock.Setup(m => m.Louder(10)).Returns(10);
Assert.AreEqual(___, mock.Object.Louder(10));
Assert.AreEqual(___, mock.Object.Louder(50));
}
[TestMethod]
public void MultipleSetupMethodsCanTakeDifferentParameters()
{
var mock = new Mock<IVolume>();
mock.Setup(m => m.Louder(1)).Returns(10);
mock.Setup(m => m.Louder(2)).Returns(20);
Assert.AreEqual(___, mock.Object.Louder(1));
Assert.AreEqual(___, mock.Object.Louder(2));
}
[TestMethod]
public void ItIs_CanTakeLambdaExpressionAsAParameterMatchFilter()
{
var mock = new Mock<IVolume>();
mock.Setup(m => m.Louder(It.Is<int>(p => p >= 0))).Returns(10);
mock.Setup(m => m.Louder(It.Is<int>(p => p < 0))).Returns(-10);
Assert.AreEqual(___, mock.Object.Louder(5));
Assert.AreEqual(___, mock.Object.Louder(-2));
}
[TestMethod]
public void SetupTheMockQuieterMethodToReturnTheDesiredResultsToMakeTheTestPass()
{
var mock = new Mock<IVolume>();
mock.___();
mock.___();
Assert.AreEqual(0, mock.Object.Quieter(-2));
Assert.AreEqual(0, mock.Object.Quieter(-1));
Assert.AreEqual(100, mock.Object.Quieter(1));
Assert.AreEqual(100, mock.Object.Quieter(2));
}
[TestMethod]
public void SetupCanReturnValuesFromVariables()
{
var someObject = new { ReturnValue = "I Am A Return Value!" };
var mock = new Mock<IVolume>();
mock.Setup(x => x.CurrentVolume()).Returns(someObject.ReturnValue);
Assert.AreEqual(___, mock.Object.CurrentVolume());
}
[TestMethod]
public void SetupCanReturnThePassedInParameter()
{
var mock = new Mock<IVolume>();
var volume = mock.Object;
mock.Setup(m => m.Louder(It.IsAny<int>())).Returns<int>(p => p);
// The <int> generic on .Returns() tells it the value of the parameter being passed in from the Louder() method.
Assert.AreEqual(___, volume.Louder(1));
Assert.AreEqual(___, volume.Louder(5));
Assert.AreEqual(10, volume.Louder(____));
Assert.AreEqual(20, volume.Louder(____));
}
[TestMethod]
public void WriteASingleSetupMethodForQuieterSoThatItAlwaysReturnsOneLessThanThePassedInValue()
{
var mock = new Mock<IVolume>();
var volume = mock.Object;
mock.___();
Assert.AreEqual(0, volume.Quieter(1));
Assert.AreEqual(1, volume.Quieter(2));
Assert.AreEqual(2, volume.Quieter(3));
}
// This interface has a method that takes more than 1 parameter.
public interface IAddition
{
int Add(int left, int right);
}
[TestMethod]
public void CanWorkWithMultipleParameters()
{
var mock = new Mock<IAddition>();
mock.Setup(m => m.Add(It.IsAny<int>(), It.IsAny<int>())).Returns<int, int>((left, right) => left + right);
Assert.AreEqual(___, mock.Object.Add(1, 2));
Assert.AreEqual(10, mock.Object.Add(____, ____));
}
[TestMethod]
public void SetupMethodsCanBeToldThThrowAnExceptionWhenCalled()
{
var mock = new Mock<IVolume>();
mock.Setup(x => x.CurrentVolume()).Throws(new InvalidOperationException("Calling CurrentVolume() will throw this Exception."));
var exceptionWasThrown = false;
try
{
mock.Object.CurrentVolume();
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
[TestMethod]
public void CreateAMockIAdditionThatOnlyAddsPositiveNumbersAndThrowsAnExceptionIfEitherNumerIsNegative()
{
// hint: remember that MockBehavior.Strict will cause an Exception if the parameters don't match any .Setup() filters.
var mock = new ___();
mock.____();
Assert.AreEqual(3, mock.Object.Add(1, 2));
Assert.AreEqual(10, mock.Object.Add(0, 10));
try
{
mock.Object.Add(5, -5);
Assert.Fail("The .Add() method did not throw an Exception when a negative number was passed in.");
}
catch (Exception)
{
// expecting an Exception because we passed in a negative number.
}
}
[TestMethod]
public void SetupMethodsCanExecuteADelegateOrLambdaWhenCalled()
{
bool louderWasCalled = false, quieterWasCalled = false;
var mock = new Mock<IVolume>();
mock.Setup(x => x.Louder(It.IsAny<int>())).Callback(() => louderWasCalled = true);
mock.Setup(x => x.Quieter(It.IsAny<int>())).Callback(() => quieterWasCalled = true);
mock.Object.Louder(5);
Assert.AreEqual(___, louderWasCalled);
Assert.AreEqual(___, quieterWasCalled);
}
[TestMethod]
public void SetupMethodCanPerformAnActionAndReturnAValue()
{
bool louderWasCalled = false;
// In this form, .Callback() performs an action and .Returns() sets a return value.
var mock = new Mock<IVolume>();
mock.Setup(x => x.Louder(It.IsAny<int>()))
.Callback(() => louderWasCalled = true)
.Returns<int>(input => input);
var result = mock.Object.Louder(5);
Assert.AreEqual(___, louderWasCalled);
Assert.AreEqual(___, result);
// The same thing can be done like this, in a single lambda in .Returns()
mock = new Mock<IVolume>();
mock.Setup(x => x.Louder(It.IsAny<int>()))
.Returns<int>(input =>
{
louderWasCalled = true;
return input;
});
result = mock.Object.Louder(5);
Assert.AreEqual(___, louderWasCalled);
Assert.AreEqual(___, result);
}
}
}