@@ -4,59 +4,113 @@ import { Substitute, Arg } from "../../src/index";
4
4
5
5
interface Calculator {
6
6
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 ;
11
9
}
12
10
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 => {
14
12
const calculator = Substitute . for < Calculator > ( ) ;
15
13
16
14
// Do not mock and do not call
15
+
16
+ calculator . received ( 0 ) . add ( 1 , 2 ) ;
17
+ t . throws ( ( ) => calculator . received ( 1 ) . add ( 1 , 2 ) ) ;
17
18
calculator . didNotReceive ( ) . add ( Arg . any ( ) , Arg . any ( ) ) ;
18
19
calculator . didNotReceive ( ) . add ( 1 , Arg . any ( ) ) ;
19
20
calculator . didNotReceive ( ) . add ( 1 , 2 ) ;
20
21
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
+
21
30
t . pass ( ) ;
22
31
} ) ;
23
32
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 => {
25
34
const calculator = Substitute . for < Calculator > ( ) ;
26
35
27
36
// Do not mock, but call
28
37
calculator . add ( 1 , 2 ) ;
38
+ void calculator . mode ;
39
+ calculator . fakeSetting = true ;
29
40
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 ) ;
33
55
34
56
t . pass ( ) ;
35
57
} ) ;
36
58
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 => {
38
60
const calculator = Substitute . for < Calculator > ( ) ;
39
61
40
62
// Mock but do not call
41
63
calculator . add ( 1 , 2 ) . returns ( 3 ) ;
64
+ calculator . mode . returns ( true ) ;
42
65
66
+ calculator . received ( 0 ) . add ( 1 , 2 ) ;
67
+ t . throws ( ( ) => calculator . received ( 1 ) . add ( 1 , 2 ) ) ;
43
68
calculator . didNotReceive ( ) . add ( Arg . any ( ) , Arg . any ( ) ) ;
44
69
calculator . didNotReceive ( ) . add ( 1 , Arg . any ( ) ) ;
45
70
calculator . didNotReceive ( ) . add ( 1 , 2 ) ;
46
71
72
+ calculator . received ( 0 ) . mode ;
73
+ t . throws ( ( ) => calculator . received ( 1 ) . mode ) ;
74
+ calculator . didNotReceive ( ) . mode ;
75
+
47
76
t . pass ( ) ;
48
77
} ) ;
49
78
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 => {
51
80
const calculator = Substitute . for < Calculator > ( ) ;
52
81
53
82
// Mock and call
54
83
calculator . add ( 1 , 2 ) . returns ( 3 ) ;
55
84
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 ) ;
60
104
61
105
t . pass ( ) ;
62
106
} ) ;
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