-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrictMockTests.cs
168 lines (121 loc) · 4.55 KB
/
StrictMockTests.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
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks.Exceptions;
namespace Rhino.Mocks.Demo
{
[TestFixture]
public class StrictMockTests
{
[Test]
public void CallingMethodWithoutExpectationOrStubFailsTest()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
Assert.Throws<ExpectationViolationException>(() => mockList.Add("some text"));
}
[Test]
public void NotCallingMethodExpectedMethodFailsTestOnVerify()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text"));
Assert.Throws<ExpectationViolationException>(() => mockList.VerifyAllExpectations());
}
[Test]
public void HappilyAcceptsCallForExpectedMethod()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text"));
mockList.Add("some text");
mockList.VerifyAllExpectations();
}
[Test]
public void MultipleCallsToExpectedMethodFailsTestByDefault()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text"));
mockList.Add("some text");
Assert.Throws<ExpectationViolationException>(() => mockList.Add("other text"));
}
[Test]
public void CallingExpectedMethodWithUnexpectedArumentsFailsTest()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text"));
Assert.Throws<ExpectationViolationException>(() => mockList.Add("other text"));
}
[Test]
public void ThrowsExceptionWhenExpectedMethodIsNotCalledEnoughTimes()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text")).Repeat.Twice();
mockList.Add("some text");
Assert.Throws<ExpectationViolationException>(() => mockList.VerifyAllExpectations());
}
[Test]
public void HappilyAcceptsTwoCallsToExpectedMethodWithRepeat()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.Add("some text")).Repeat.Twice();
mockList.Add("some text");
mockList.Add("some text");
mockList.VerifyAllExpectations();
}
[Test]
public void ExpectedMethodReturnsConfiguredReturnValue()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.IndexOf("some text")).Return(1);
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
mockList.VerifyAllExpectations();
}
[Test]
public void ArgumentConstraintsMakeTestLessStrict()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.IndexOf(Arg.Text.EndsWith("text"))).Return(1);
mockList.IndexOf("some text");
mockList.VerifyAllExpectations();
}
[Test]
public void ArgumentConstraintsApiIsFluentAndNice()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.IndexOf(Arg.Text.EndsWith("text"))).Return(1);
mockList.Expect(l => l.IndexOf(Arg<string>.Matches(s => s == "why would you do this?"))).Return(2);
mockList.Expect(l => l.IndexOf(Arg.Is("soup"))).Return(3);
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
Assert.That(mockList.IndexOf("why would you do this?"), Is.EqualTo(2));
Assert.That(mockList.IndexOf("soup"), Is.EqualTo(3));
}
[Test]
public void VioatingArgumentConstraintFailsTest()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Expect(l => l.IndexOf(Arg.Text.EndsWith("text"))).Return(1);
Assert.Throws<ExpectationViolationException>(() => mockList.IndexOf("some words"));
}
[Test]
public void StubEnablesAMethodCallForSpecificArguments()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Stub(l => l.IndexOf("some text")).Return(1);
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
Assert.Throws<ExpectationViolationException>(() => mockList.IndexOf("other text"));
}
[Test]
public void NotCallingStubbedMethodIsOkay()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Stub(l => l.IndexOf("some text")).Return(1);
mockList.VerifyAllExpectations();
}
[Test]
public void StubEnablesMultipleCallsToAMethodForSpecificArguments()
{
var mockList = MockRepository.GenerateStrictMock<IList<string>>();
mockList.Stub(l => l.IndexOf("some text")).Return(1);
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
Assert.That(mockList.IndexOf("some text"), Is.EqualTo(1));
}
}
}