Skip to content

Commit 12a9ec4

Browse files
wschurmanffMathy
authored andcommitted
Update test for expected received and didNotReceive behavior (#76)
1 parent 59bd915 commit 12a9ec4

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

spec/issues/72.test.ts

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,113 @@ import { Substitute, Arg } from "../../src/index";
44

55
interface Calculator {
66
add(a: number, b: number): number;
7-
subtract(a: number, b: number): number;
8-
divide(a: number, b: number): number;
9-
10-
isEnabled: boolean;
7+
mode: boolean;
8+
fakeSetting: boolean;
119
}
1210

13-
test("check didNotReceive after not mocking and not calling a method", t => {
11+
test("check didNotReceive and received after not mocking and not calling a method or property", t => {
1412
const calculator = Substitute.for<Calculator>();
1513

1614
// Do not mock and do not call
15+
16+
calculator.received(0).add(1, 2);
17+
t.throws(() => calculator.received(1).add(1, 2));
1718
calculator.didNotReceive().add(Arg.any(), Arg.any());
1819
calculator.didNotReceive().add(1, Arg.any());
1920
calculator.didNotReceive().add(1, 2);
2021

22+
calculator.received(0).mode;
23+
t.throws(() => calculator.received(1).mode);
24+
calculator.didNotReceive().mode;
25+
26+
calculator.received(0).fakeSetting = true;
27+
t.throws(() => calculator.received(1).fakeSetting = true);
28+
calculator.didNotReceive().fakeSetting = true;
29+
2130
t.pass();
2231
});
2332

24-
test("check didNotReceive after not mocking but calling a method", t => {
33+
test("check didNotReceive and received after not mocking but calling a method or property", t => {
2534
const calculator = Substitute.for<Calculator>();
2635

2736
// Do not mock, but call
2837
calculator.add(1, 2);
38+
void calculator.mode;
39+
calculator.fakeSetting = true;
2940

30-
calculator.didNotReceive().add(Arg.any(), Arg.any());
31-
calculator.didNotReceive().add(1, Arg.any());
32-
calculator.didNotReceive().add(1, 2);
41+
calculator.received(1).add(1, 2);
42+
calculator.received(0).add(2, 2);
43+
t.throws(() => calculator.received(1).add(2, 2));
44+
t.throws(() => calculator.didNotReceive().add(Arg.any(), Arg.any()));
45+
t.throws(() => calculator.didNotReceive().add(1, Arg.any()));
46+
t.throws(() => calculator.didNotReceive().add(1, 2));
47+
48+
calculator.received(1).mode;
49+
t.throws(() => calculator.received(0).mode);
50+
t.throws(() => calculator.didNotReceive().mode);
51+
52+
calculator.received(1).fakeSetting = true;
53+
t.throws(() => calculator.received(0).fakeSetting = true);
54+
t.throws(() => calculator.didNotReceive().fakeSetting = true);
3355

3456
t.pass();
3557
});
3658

37-
test("check didNotReceive after mocking but not calling a method", t => {
59+
test("check didNotReceive and received after mocking but not calling a method or property", t => {
3860
const calculator = Substitute.for<Calculator>();
3961

4062
// Mock but do not call
4163
calculator.add(1, 2).returns(3);
64+
calculator.mode.returns(true);
4265

66+
calculator.received(0).add(1, 2);
67+
t.throws(() => calculator.received(1).add(1, 2));
4368
calculator.didNotReceive().add(Arg.any(), Arg.any());
4469
calculator.didNotReceive().add(1, Arg.any());
4570
calculator.didNotReceive().add(1, 2);
4671

72+
calculator.received(0).mode;
73+
t.throws(() => calculator.received(1).mode);
74+
calculator.didNotReceive().mode;
75+
4776
t.pass();
4877
});
4978

50-
test("check didNotReceive after mocking and calling a method", t => {
79+
test("check didNotReceive and received after mocking and calling a method or property", t => {
5180
const calculator = Substitute.for<Calculator>();
5281

5382
// Mock and call
5483
calculator.add(1, 2).returns(3);
5584
calculator.add(1, 2);
56-
57-
calculator.didNotReceive().add(Arg.any(), Arg.any());
58-
calculator.didNotReceive().add(1, Arg.any());
59-
calculator.didNotReceive().add(1, 2);
85+
calculator.mode.returns(true);
86+
void calculator.mode;
87+
calculator.fakeSetting.returns(true);
88+
calculator.fakeSetting = true;
89+
90+
calculator.received(1).add(1, 2);
91+
t.throws(() => calculator.received(0).add(1, 2));
92+
t.throws(() => calculator.received(2).add(1, 2));
93+
t.throws(() => calculator.didNotReceive().add(Arg.any(), Arg.any()));
94+
t.throws(() => calculator.didNotReceive().add(1, Arg.any()));
95+
t.throws(() => calculator.didNotReceive().add(1, 2));
96+
97+
calculator.received(1).mode;
98+
t.throws(() => calculator.received(0).mode);
99+
t.throws(() => calculator.didNotReceive().mode);
100+
101+
calculator.received(1).fakeSetting = true;
102+
t.throws(() => calculator.received(0).fakeSetting = true);
103+
t.throws(() => calculator.didNotReceive().fakeSetting = true);
60104

61105
t.pass();
62106
});
107+
108+
// this test should fail but doesn't
109+
test("check that received property does both throw and not throw", t => {
110+
const calculator = Substitute.for<Calculator>();
111+
void calculator.mode;
112+
t.throws(() => calculator.received(1).mode);
113+
t.notThrows(() => calculator.received(1).mode);
114+
115+
t.pass();
116+
});

0 commit comments

Comments
 (0)