8
8
using Microsoft . AspNetCore . Mvc . Testing ;
9
9
using Microsoft . Extensions . DependencyInjection ;
10
10
using Microsoft . Extensions . DependencyInjection . Extensions ;
11
- using Moq ;
11
+ using NSubstitute ;
12
+ using NSubstitute . ExceptionExtensions ;
12
13
13
14
namespace Cnblogs . Architecture . IntegrationTests ;
14
15
@@ -20,13 +21,13 @@ public async Task EventBus_PublishEvent_SuccessAsync()
20
21
// Arrange
21
22
const string data = "hello" ;
22
23
var builder = new WebApplicationFactory < Program > ( ) ;
23
- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
24
+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
24
25
builder = builder . WithWebHostBuilder (
25
26
b => b . ConfigureServices (
26
27
services =>
27
28
{
28
29
services . RemoveAll < IEventBusProvider > ( ) ;
29
- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
30
+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
30
31
} ) ) ;
31
32
32
33
// Act
@@ -39,9 +40,9 @@ public async Task EventBus_PublishEvent_SuccessAsync()
39
40
// Assert
40
41
response . Should ( ) . BeSuccessful ( ) ;
41
42
content . Should ( ) . BeNullOrEmpty ( ) ;
42
- eventBusMock . Verify (
43
- x => x . PublishAsync ( It . IsAny < string > ( ) , It . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ,
44
- Times . Once ) ;
43
+ await eventBusMock . Received ( 1 ) . PublishAsync (
44
+ Arg . Any < string > ( ) ,
45
+ Arg . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ;
45
46
}
46
47
47
48
[ Fact ]
@@ -50,21 +51,21 @@ public async Task EventBus_Downgrading_DowngradeAsync()
50
51
// Arrange
51
52
const string data = "hello" ;
52
53
var builder = new WebApplicationFactory < Program > ( ) ;
53
- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
54
+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
54
55
builder = builder . WithWebHostBuilder (
55
56
b => b . ConfigureServices (
56
57
services =>
57
58
{
58
59
services . RemoveAll < IEventBusProvider > ( ) ;
59
- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
60
+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
60
61
services . Configure < EventBusOptions > (
61
62
o =>
62
63
{
63
64
o . FailureCountBeforeDowngrade = 1 ;
64
65
o . DowngradeInterval = 3000 ;
65
66
} ) ;
66
67
} ) ) ;
67
- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
68
+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
68
69
. ThrowsAsync ( new InvalidOperationException ( ) ) ;
69
70
70
71
// Act
@@ -77,9 +78,9 @@ public async Task EventBus_Downgrading_DowngradeAsync()
77
78
// Assert
78
79
response . Should ( ) . BeSuccessful ( ) ;
79
80
content . Should ( ) . BeNullOrEmpty ( ) ;
80
- eventBusMock . Verify (
81
- x => x . PublishAsync ( It . IsAny < string > ( ) , It . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ,
82
- Times . Exactly ( 2 ) ) ;
81
+ await eventBusMock . Received ( 2 ) . PublishAsync (
82
+ Arg . Any < string > ( ) ,
83
+ Arg . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ;
83
84
}
84
85
85
86
[ Fact ]
@@ -88,13 +89,13 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
88
89
// Arrange
89
90
const string data = "hello" ;
90
91
var builder = new WebApplicationFactory < Program > ( ) ;
91
- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
92
+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
92
93
builder = builder . WithWebHostBuilder (
93
94
b => b . ConfigureServices (
94
95
services =>
95
96
{
96
97
services . RemoveAll < IEventBusProvider > ( ) ;
97
- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
98
+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
98
99
services . Configure < EventBusOptions > (
99
100
o =>
100
101
{
@@ -103,7 +104,7 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
103
104
o . DowngradeInterval = 3000 ;
104
105
} ) ;
105
106
} ) ) ;
106
- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
107
+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
107
108
. ThrowsAsync ( new InvalidOperationException ( ) ) ;
108
109
var client = builder . CreateClient ( ) ;
109
110
await client . PostAsJsonAsync (
@@ -123,13 +124,13 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
123
124
// Arrange
124
125
const string data = "hello" ;
125
126
var builder = new WebApplicationFactory < Program > ( ) ;
126
- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
127
+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
127
128
builder = builder . WithWebHostBuilder (
128
129
b => b . ConfigureServices (
129
130
services =>
130
131
{
131
132
services . RemoveAll < IEventBusProvider > ( ) ;
132
- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
133
+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
133
134
services . Configure < EventBusOptions > (
134
135
o =>
135
136
{
@@ -149,7 +150,7 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
149
150
await Task . Delay ( 1000 ) ;
150
151
151
152
// Assert
152
- eventBusMock . Verify ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) , Times . Once ) ;
153
+ await eventBusMock . Received ( 1 ) . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) ) ;
153
154
}
154
155
155
156
[ Fact ]
@@ -158,38 +159,38 @@ public async Task EventBus_DowngradeThenRecover_RecoverAsync()
158
159
// Arrange
159
160
const string data = "hello" ;
160
161
var builder = new WebApplicationFactory < Program > ( ) ;
161
- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
162
+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
162
163
builder = builder . WithWebHostBuilder (
163
164
b => b . ConfigureServices (
164
165
services =>
165
166
{
166
167
services . RemoveAll < IEventBusProvider > ( ) ;
167
- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
168
+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
168
169
services . Configure < EventBusOptions > (
169
170
o =>
170
171
{
171
172
o . FailureCountBeforeDowngrade = 1 ;
172
173
o . DowngradeInterval = 4000 ;
173
174
} ) ;
174
175
} ) ) ;
175
- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
176
+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
176
177
. ThrowsAsync ( new InvalidOperationException ( ) ) ;
177
178
await builder . CreateClient ( ) . PostAsJsonAsync (
178
179
"/api/v1/strings" ,
179
180
new CreatePayload ( false , data ) ) ;
180
181
await Task . Delay ( 1000 ) ; // failed, now it is downgraded
181
182
182
183
// Act
183
- eventBusMock . Reset ( ) ;
184
+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) ) . Returns ( Task . CompletedTask ) ;
185
+ eventBusMock . ClearReceivedCalls ( ) ;
184
186
await Task . Delay ( 2000 ) ; // recover
185
187
await builder . CreateClient ( ) . PostAsJsonAsync (
186
188
"/api/v1/strings" ,
187
189
new CreatePayload ( false , data ) ) ;
188
190
await Task . Delay ( 1000 ) ;
189
191
190
192
// Assert
191
- eventBusMock . Verify (
192
- x => x . PublishAsync ( It . IsAny < string > ( ) , It . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ,
193
- Times . Exactly ( 2 ) ) ;
193
+ await eventBusMock . Received ( 2 )
194
+ . PublishAsync ( Arg . Any < string > ( ) , Arg . Is < TestIntegrationEvent > ( t => t . Message == data ) ) ;
194
195
}
195
196
}
0 commit comments