-
Notifications
You must be signed in to change notification settings - Fork 4
/
5_Properties.cs
executable file
·114 lines (92 loc) · 2.51 KB
/
5_Properties.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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MoqKoans.KoansHelpers;
namespace MoqKoans
{
[TestClass]
public class Moq5_Properties : Koan
{
// This is an Interface with some properties.
public interface IPerson
{
string Name { get; set; }
int Age { get; set; }
}
[TestMethod]
public void WithLooseBehaviorMethodsWillNotSaveAValueAndAlwaysReturnTheirDefault()
{
var person = new Mock<IPerson>(MockBehavior.Loose).Object;
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
person.Name = "Joe";
person.Age = 36;
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
}
[TestMethod]
public void WithStrictBehaviorGettingOrSettingAPropertyThatHasNotBeenSetupThrowsException()
{
var person = new Mock<IPerson>(MockBehavior.Strict).Object;
var exceptionWasThrown = false;
try
{
var name = person.Name;
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
exceptionWasThrown = false;
try
{
person.Name = "John";
}
catch (Exception)
{
exceptionWasThrown = true;
}
Assert.AreEqual(___, exceptionWasThrown);
}
[TestMethod]
public void SetupPropertyCausesAPropertyToRetainValuesAsExpected()
{
var mock = new Mock<IPerson>();
var person = mock.Object;
mock.SetupProperty(x => x.Name);
mock.SetupProperty(x => x.Age);
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
person.Name = "Joe";
person.Age = 36;
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
}
[TestMethod]
public void SetupPropertyCanSpecifyAnInitialValue()
{
var mock = new Mock<IPerson>();
var person = mock.Object;
mock.SetupProperty(x => x.Name, "Fred");
mock.SetupProperty(x => x.Age, 21);
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
person.Name = "Joe";
person.Age = 36;
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
}
[TestMethod]
public void SetupAllPropertiesCanBeUsedToSimulateCallingSetupPropertyForEachIndividualProperty()
{
var mock = new Mock<IPerson>(MockBehavior.Strict);
var person = mock.Object;
mock.SetupAllProperties();
person.Name = "Joe";
person.Age = 36;
Assert.AreEqual(___, person.Name);
Assert.AreEqual(___, person.Age);
}
}
}