88using Microsoft . AspNetCore . Mvc . Testing ;
99using Microsoft . Extensions . DependencyInjection ;
1010using Microsoft . Extensions . DependencyInjection . Extensions ;
11- using Moq ;
11+ using NSubstitute ;
12+ using NSubstitute . ExceptionExtensions ;
1213
1314namespace Cnblogs . Architecture . IntegrationTests ;
1415
@@ -20,13 +21,13 @@ public async Task EventBus_PublishEvent_SuccessAsync()
2021 // Arrange
2122 const string data = "hello" ;
2223 var builder = new WebApplicationFactory < Program > ( ) ;
23- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
24+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
2425 builder = builder . WithWebHostBuilder (
2526 b => b . ConfigureServices (
2627 services =>
2728 {
2829 services . RemoveAll < IEventBusProvider > ( ) ;
29- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
30+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
3031 } ) ) ;
3132
3233 // Act
@@ -39,9 +40,9 @@ public async Task EventBus_PublishEvent_SuccessAsync()
3940 // Assert
4041 response . Should ( ) . BeSuccessful ( ) ;
4142 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 ) ) ;
4546 }
4647
4748 [ Fact ]
@@ -50,21 +51,21 @@ public async Task EventBus_Downgrading_DowngradeAsync()
5051 // Arrange
5152 const string data = "hello" ;
5253 var builder = new WebApplicationFactory < Program > ( ) ;
53- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
54+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
5455 builder = builder . WithWebHostBuilder (
5556 b => b . ConfigureServices (
5657 services =>
5758 {
5859 services . RemoveAll < IEventBusProvider > ( ) ;
59- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
60+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
6061 services . Configure < EventBusOptions > (
6162 o =>
6263 {
6364 o . FailureCountBeforeDowngrade = 1 ;
6465 o . DowngradeInterval = 3000 ;
6566 } ) ;
6667 } ) ) ;
67- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
68+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
6869 . ThrowsAsync ( new InvalidOperationException ( ) ) ;
6970
7071 // Act
@@ -77,9 +78,9 @@ public async Task EventBus_Downgrading_DowngradeAsync()
7778 // Assert
7879 response . Should ( ) . BeSuccessful ( ) ;
7980 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 ) ) ;
8384 }
8485
8586 [ Fact ]
@@ -88,13 +89,13 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
8889 // Arrange
8990 const string data = "hello" ;
9091 var builder = new WebApplicationFactory < Program > ( ) ;
91- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
92+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
9293 builder = builder . WithWebHostBuilder (
9394 b => b . ConfigureServices (
9495 services =>
9596 {
9697 services . RemoveAll < IEventBusProvider > ( ) ;
97- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
98+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
9899 services . Configure < EventBusOptions > (
99100 o =>
100101 {
@@ -103,7 +104,7 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
103104 o . DowngradeInterval = 3000 ;
104105 } ) ;
105106 } ) ) ;
106- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
107+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
107108 . ThrowsAsync ( new InvalidOperationException ( ) ) ;
108109 var client = builder . CreateClient ( ) ;
109110 await client . PostAsJsonAsync (
@@ -123,13 +124,13 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
123124 // Arrange
124125 const string data = "hello" ;
125126 var builder = new WebApplicationFactory < Program > ( ) ;
126- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
127+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
127128 builder = builder . WithWebHostBuilder (
128129 b => b . ConfigureServices (
129130 services =>
130131 {
131132 services . RemoveAll < IEventBusProvider > ( ) ;
132- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
133+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
133134 services . Configure < EventBusOptions > (
134135 o =>
135136 {
@@ -149,7 +150,7 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
149150 await Task . Delay ( 1000 ) ;
150151
151152 // 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 > ( ) ) ;
153154 }
154155
155156 [ Fact ]
@@ -158,38 +159,38 @@ public async Task EventBus_DowngradeThenRecover_RecoverAsync()
158159 // Arrange
159160 const string data = "hello" ;
160161 var builder = new WebApplicationFactory < Program > ( ) ;
161- var eventBusMock = new Mock < IEventBusProvider > ( ) ;
162+ var eventBusMock = Substitute . For < IEventBusProvider > ( ) ;
162163 builder = builder . WithWebHostBuilder (
163164 b => b . ConfigureServices (
164165 services =>
165166 {
166167 services . RemoveAll < IEventBusProvider > ( ) ;
167- services . AddScoped < IEventBusProvider > ( _ => eventBusMock . Object ) ;
168+ services . AddScoped < IEventBusProvider > ( _ => eventBusMock ) ;
168169 services . Configure < EventBusOptions > (
169170 o =>
170171 {
171172 o . FailureCountBeforeDowngrade = 1 ;
172173 o . DowngradeInterval = 4000 ;
173174 } ) ;
174175 } ) ) ;
175- eventBusMock . Setup ( x => x . PublishAsync ( It . IsAny < string > ( ) , It . IsAny < IntegrationEvent > ( ) ) )
176+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) )
176177 . ThrowsAsync ( new InvalidOperationException ( ) ) ;
177178 await builder . CreateClient ( ) . PostAsJsonAsync (
178179 "/api/v1/strings" ,
179180 new CreatePayload ( false , data ) ) ;
180181 await Task . Delay ( 1000 ) ; // failed, now it is downgraded
181182
182183 // Act
183- eventBusMock . Reset ( ) ;
184+ eventBusMock . PublishAsync ( Arg . Any < string > ( ) , Arg . Any < IntegrationEvent > ( ) ) . Returns ( Task . CompletedTask ) ;
185+ eventBusMock . ClearReceivedCalls ( ) ;
184186 await Task . Delay ( 2000 ) ; // recover
185187 await builder . CreateClient ( ) . PostAsJsonAsync (
186188 "/api/v1/strings" ,
187189 new CreatePayload ( false , data ) ) ;
188190 await Task . Delay ( 1000 ) ;
189191
190192 // 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 ) ) ;
194195 }
195196}
0 commit comments