forked from NagRock/ts-mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecording.multiple.behaviors.spec.ts
157 lines (137 loc) · 6.9 KB
/
recording.multiple.behaviors.spec.ts
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
import {instance, mock, when} from "../src/ts-mockito";
import {Foo} from "./utils/Foo";
describe("recording multiple behaviors", () => {
let mockedFoo: Foo;
let foo: Foo;
beforeEach(() => {
mockedFoo = mock(Foo);
foo = instance(mockedFoo);
});
describe("when more than one behavior matches", () => {
it("using added later", () => {
// given
const sampleValue = 3;
const firstStubResult = "first";
const secondStubResult = "second";
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(firstStubResult);
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(secondStubResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
const secondCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(secondStubResult);
expect(secondCallResult).toEqual(secondStubResult);
});
});
describe("when one of behaviors doesn't match", () => {
it("is skipped", () => {
// given
const sampleValue = 3;
const firstMatchingStubResult = "first";
const secondMatchingStubResult = "second";
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(firstMatchingStubResult);
when(mockedFoo.convertNumberToString(123)).thenReturn("not matching behavior");
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(secondMatchingStubResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
const secondCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(secondMatchingStubResult);
expect(secondCallResult).toEqual(secondMatchingStubResult);
});
});
describe("when calling same method multiple times", () => {
describe("used behaviors are removed", () => {
describe("and when no more than one matcher left", () => {
it("repeats last one", () => {
// given
const sampleValue = 3;
const firstMatchingStubResult = "first";
const secondMatchingStubResult = "second";
when(mockedFoo.convertNumberToString(sampleValue))
.thenReturn(firstMatchingStubResult)
.thenReturn(secondMatchingStubResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
const secondCallResult = foo.convertNumberToString(sampleValue);
const thirdCallResult = foo.convertNumberToString(sampleValue);
const fourthCallResult = foo.convertNumberToString(sampleValue);
const fifthCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(firstMatchingStubResult);
expect(secondCallResult).toEqual(secondMatchingStubResult);
expect(thirdCallResult).toEqual(secondMatchingStubResult);
expect(fourthCallResult).toEqual(secondMatchingStubResult);
expect(fifthCallResult).toEqual(secondMatchingStubResult);
});
});
});
});
describe("when multiple results has been set by one method call", () => {
it("uses one by another and repeat last one infinitely", () => {
// given
const sampleValue = 3;
const firstMatchingStubResult = "first";
const secondMatchingStubResult = "second";
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(firstMatchingStubResult, secondMatchingStubResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
const secondCallResult = foo.convertNumberToString(sampleValue);
const thirdCallResult = foo.convertNumberToString(sampleValue);
const fourthCallResult = foo.convertNumberToString(sampleValue);
const fifthCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(firstMatchingStubResult);
expect(secondCallResult).toEqual(secondMatchingStubResult);
expect(thirdCallResult).toEqual(secondMatchingStubResult);
expect(fourthCallResult).toEqual(secondMatchingStubResult);
expect(fifthCallResult).toEqual(secondMatchingStubResult);
});
});
describe("when return values are mixed with throw errors", () => {
it("uses one by one and repeat last one infinitely", () => {
// given
const sampleValue = 3;
const firstMatchingStubResult = "first";
const secondMatchingStubResult = "second";
const firstMatchingError = new Error("firstError");
when(mockedFoo.convertNumberToString(sampleValue))
.thenReturn(firstMatchingStubResult)
.thenThrow(firstMatchingError)
.thenReturn(secondMatchingStubResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
let error: Error;
try {
foo.convertNumberToString(sampleValue);
} catch (e) {
error = e;
}
const thirdCallResult = foo.convertNumberToString(sampleValue);
const fourthCallResult = foo.convertNumberToString(sampleValue);
const fifthCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(firstMatchingStubResult);
expect(error.message).toEqual(firstMatchingError.message);
expect(thirdCallResult).toEqual(secondMatchingStubResult);
expect(fourthCallResult).toEqual(secondMatchingStubResult);
expect(fifthCallResult).toEqual(secondMatchingStubResult);
});
});
describe("when just one behavior was set", () => {
it("behavior is not removed", () => {
// given
const sampleValue = 3;
const firstCallExpectedResult = "first";
when(mockedFoo.convertNumberToString(sampleValue)).thenReturn(firstCallExpectedResult);
// when
const firstCallResult = foo.convertNumberToString(sampleValue);
const secondCallResult = foo.convertNumberToString(sampleValue);
const thirdCallResult = foo.convertNumberToString(sampleValue);
// then
expect(firstCallResult).toEqual(firstCallExpectedResult);
expect(secondCallResult).toEqual(firstCallExpectedResult);
expect(thirdCallResult).toEqual(firstCallExpectedResult);
});
});
});