-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathResetTests.cs
More file actions
206 lines (164 loc) · 5.52 KB
/
Copy pathResetTests.cs
File metadata and controls
206 lines (164 loc) · 5.52 KB
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
using TUnit.Mocks;
using TUnit.Mocks.Arguments;
using TUnit.Mocks.Exceptions;
namespace TUnit.Mocks.Tests;
/// <summary>
/// T073 Integration Tests: Mock reset — verify that Reset clears setups, call history,
/// and allows fresh re-configuration.
/// </summary>
public class ResetTests
{
[Test]
public async Task Reset_Clears_All_Setups()
{
// Arrange
var mock = ICalculator.Mock();
mock.Add(1, 2).Returns(42);
mock.Add(3, 4).Returns(99);
ICalculator calc = mock.Object;
await Assert.That(calc.Add(1, 2)).IsEqualTo(42);
await Assert.That(calc.Add(3, 4)).IsEqualTo(99);
// Act
mock.Reset();
// Assert — after reset, all setups are gone, returns default
await Assert.That(calc.Add(1, 2)).IsEqualTo(0);
await Assert.That(calc.Add(3, 4)).IsEqualTo(0);
}
[Test]
public async Task Reset_Clears_Call_History()
{
// Arrange
var mock = ICalculator.Mock();
ICalculator calc = mock.Object;
calc.Add(1, 2);
calc.Add(3, 4);
calc.Add(5, 6);
// Verify calls were recorded
mock.Add(Any(), Any()).WasCalled(Times.Exactly(3));
// Act
mock.Reset();
// Assert — after reset, call history is cleared
mock.Add(Any(), Any()).WasNeverCalled();
await Assert.That(true).IsTrue();
}
[Test]
public async Task Reset_Allows_Fresh_Setup()
{
// Arrange
var mock = ICalculator.Mock();
mock.Add(1, 2).Returns(42);
ICalculator calc = mock.Object;
await Assert.That(calc.Add(1, 2)).IsEqualTo(42);
// Act — reset and reconfigure with different return value
mock.Reset();
mock.Add(1, 2).Returns(100);
// Assert — new setup is in effect
await Assert.That(calc.Add(1, 2)).IsEqualTo(100);
}
[Test]
public async Task Reset_Allows_Fresh_Verification()
{
// Arrange
var mock = ICalculator.Mock();
ICalculator calc = mock.Object;
calc.Add(1, 2);
calc.Add(1, 2);
mock.Add(1, 2).WasCalled(Times.Exactly(2));
// Act
mock.Reset();
// Make one new call
calc.Add(1, 2);
// Assert — verification reflects only post-reset calls
mock.Add(1, 2).WasCalled(Times.Once);
await Assert.That(true).IsTrue();
}
[Test]
public async Task Reset_On_Strict_Mock_Restores_Strict_Behavior()
{
// Arrange — strict mock with a configured method
var mock = ICalculator.Mock(MockBehavior.Strict);
mock.Add(1, 2).Returns(3);
ICalculator calc = mock.Object;
await Assert.That(calc.Add(1, 2)).IsEqualTo(3);
// Act — reset clears all setups
mock.Reset();
// Assert — strict mock throws again for unconfigured calls
var exception = Assert.Throws<MockStrictBehaviorException>(() =>
{
calc.Add(1, 2);
});
await Assert.That(exception).IsNotNull();
await Assert.That(exception.Message).Contains("Add");
}
[Test]
public async Task Reset_Clears_String_Method_Setup()
{
// Arrange
var mock = IGreeter.Mock();
mock.Greet("Alice").Returns("Hello, Alice!");
IGreeter greeter = mock.Object;
await Assert.That(greeter.Greet("Alice")).IsEqualTo("Hello, Alice!");
// Act
mock.Reset();
// Assert — after reset, returns default (empty string for non-nullable string)
var result = greeter.Greet("Alice");
await Assert.That(result).IsNotEqualTo("Hello, Alice!");
}
[Test]
public async Task Reset_Clears_Void_Method_Call_History()
{
// Arrange
var mock = ICalculator.Mock();
ICalculator calc = mock.Object;
calc.Log("message1");
calc.Log("message2");
mock.Log(Any()).WasCalled(Times.Exactly(2));
// Act
mock.Reset();
// Assert — void method call history is cleared
mock.Log(Any()).WasNeverCalled();
await Assert.That(true).IsTrue();
}
[Test]
public async Task Reset_Followed_By_New_Setup_And_Verification()
{
// Arrange — full lifecycle: setup, use, reset, re-setup, re-use, re-verify
var mock = ICalculator.Mock();
mock.Add(1, 1).Returns(10);
ICalculator calc = mock.Object;
calc.Add(1, 1);
mock.Add(1, 1).WasCalled(Times.Once);
// Act — reset
mock.Reset();
// Re-setup with new values
mock.Add(1, 1).Returns(20);
// Re-use
calc.Add(1, 1);
calc.Add(1, 1);
// Assert — new setup and history
await Assert.That(calc.Add(1, 1)).IsEqualTo(20);
mock.Add(1, 1).WasCalled(Times.Exactly(3)); // 2 from re-use + 1 from the assert line
await Assert.That(true).IsTrue();
}
[Test]
public async Task Multiple_Resets()
{
// Arrange
var mock = ICalculator.Mock();
ICalculator calc = mock.Object;
// First cycle
mock.Add(1, 1).Returns(10);
await Assert.That(calc.Add(1, 1)).IsEqualTo(10);
mock.Reset();
// Second cycle
mock.Add(1, 1).Returns(20);
await Assert.That(calc.Add(1, 1)).IsEqualTo(20);
mock.Reset();
// Third cycle
mock.Add(1, 1).Returns(30);
await Assert.That(calc.Add(1, 1)).IsEqualTo(30);
mock.Reset();
// After final reset — returns default
await Assert.That(calc.Add(1, 1)).IsEqualTo(0);
}
}