-
Notifications
You must be signed in to change notification settings - Fork 4
/
4_Virtual.cs
executable file
·123 lines (104 loc) · 2.93 KB
/
4_Virtual.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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MoqKoans.KoansHelpers;
namespace MoqKoans
{
[TestClass]
public class Moq4_Virtual : Koan
{
// A concrete class to test Moq with.
// Note that this class contains a virtual method.
public class Vehicle
{
public int GetHorsepower() { return 1001; }
public virtual int GetNumerOfWheels() { return 4; }
}
[TestMethod]
public void MoqCanCreateMocksAroundConcreteClasses()
{
// note that Vehicle is not an interface.
var mockVehicle = new Mock<Vehicle>();
Assert.AreEqual(___, mockVehicle.Object is Vehicle);
}
[TestMethod]
public void NonVirtualMethodsAreWrappered_TheBaseMethodIsStillCalled()
{
var mockVehicle = new Mock<Vehicle>();
Assert.AreEqual(___, mockVehicle.Object.GetHorsepower());
}
[TestMethod]
public void VirtualMethodsAreHandledByMoq()
{
var mockVehicle = new Mock<Vehicle>();
Assert.AreEqual(___, mockVehicle.Object.GetNumerOfWheels());
}
[TestMethod]
public void VirtualMethodsCanBeSetupTheSameAsInterfaces()
{
var mockVehicle = new Mock<Vehicle>();
mockVehicle.Setup(m => m.GetNumerOfWheels()).Returns(18);
Assert.AreEqual(___, mockVehicle.Object.GetNumerOfWheels());
}
[TestMethod]
public void NonVirtualMethodsCanNotBeSetup()
{
var mockVehicle = new Mock<Vehicle>();
var exceptionWasThrown = false;
try
{
mockVehicle.Setup(m => m.GetHorsepower()).Returns(170);
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
// An abstract class to test Moq with.
public abstract class Person
{
public abstract string GetFirstName();
public abstract string GetLastName();
}
[TestMethod]
public void MoqCanMockAbstractClasses()
{
var mock = new Mock<Person>();
Assert.IsInstanceOfType(mock.Object, typeof(___));
}
[TestMethod]
public void WithLooseBehaviorMockedAbstractMethodsReturnTheirDefaultValue()
{
var mock = new Mock<Person>(MockBehavior.Loose);
Assert.AreEqual(___, mock.Object.GetFirstName());
}
[TestMethod]
public void WithStrictBehaviorMockedAbstractMethodsThrowAnExceptionUnlessSetup()
{
var mock = new Mock<Person>(MockBehavior.Strict);
mock.Setup(x => x.GetFirstName()).Returns("Fred");
Assert.AreEqual(___, mock.Object.GetFirstName());
var exceptionWasThrown = false;
try
{
mock.Object.GetLastName();
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
[TestMethod]
public void SetupAMockPersonWithTheNameJohnDoe()
{
var mock = new Mock<Person>(MockBehavior.Strict);
mock.___();
mock.___();
var person = mock.Object;
Assert.AreEqual("John", person.GetFirstName());
Assert.AreEqual("Doe", person.GetLastName());
}
}
}