Skip to content

Commit 0909536

Browse files
committed
Replace Moq with NSubstitute due privacy reasons
1 parent 3e80d78 commit 0909536

12 files changed

+108
-129
lines changed

Tests/Client/Tests/Actions/DisableWarningsActionTests.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using MediatR;
2-
using Moq;
2+
using NSubstitute;
33

44
using System;
55
using System.Collections.Generic;
@@ -21,15 +21,14 @@ public class DisableWarningsActionTests
2121
{
2222
private IUserManageService CreateMockService()
2323
{
24-
var _userManageService = new Mock<IUserManageService>();
25-
_userManageService.Setup(
26-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
27-
.ReturnsAsync(
24+
var _userManageService = Substitute.For<IUserManageService>();
25+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
26+
.Returns(
2827
new Response<Unit>()
2928
{
3029
Result = new Unit()
3130
});
32-
return _userManageService.Object;
31+
return _userManageService;
3332
}
3433

3534
[Fact]

Tests/Client/Tests/Actions/EnableWarningsActionTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
2-
using Moq;
2+
3+
using NSubstitute;
34

45
using System;
56
using System.Collections.Generic;
@@ -20,15 +21,14 @@ public class EnableWarningsActionTests
2021
{
2122
private IUserManageService CreateMockService()
2223
{
23-
var _userManageService = new Mock<IUserManageService>();
24-
_userManageService.Setup(
25-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
26-
.ReturnsAsync(
24+
var _userManageService = Substitute.For<IUserManageService>();
25+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
26+
.Returns(
2727
new Response<Unit>()
2828
{
2929
Result = new Unit()
3030
});
31-
return _userManageService.Object;
31+
return _userManageService;
3232
}
3333

3434
[Theory]

Tests/Client/Tests/Actions/GetForecastActionTests.cs

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
2-
using Moq;
2+
3+
using NSubstitute;
34

45
using OpenMeteoRemoteApi.Interfaces;
56
using OpenMeteoRemoteApi.Models;
@@ -37,18 +38,16 @@ public async Task GetForecastActionTests_SuccessForecastNow()
3738
{
3839
var expectedResult = MessageType.ForecastNow;
3940

40-
var _userManageService = new Mock<IUserManageService>();
41-
_userManageService.Setup(
42-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
43-
.ReturnsAsync(
41+
var _userManageService = Substitute.For<IUserManageService>();
42+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
43+
.Returns(
4444
new Response<UserViewModel?>()
4545
{
4646
Result = _dummyUser
4747
});
48-
var _weatherApi = new Mock<IWeatherApiWrapper>();
49-
_weatherApi.Setup(
50-
_ => _.GetWeatherAsync(It.IsAny<WeatherRequest>(), It.IsAny<CancellationToken>()))
51-
.ReturnsAsync(
48+
var _weatherApi = Substitute.For<IWeatherApiWrapper>();
49+
_weatherApi.GetWeatherAsync(Arg.Any<WeatherRequest>(), Arg.Any<CancellationToken>())
50+
.Returns(
5251
new Response<WeatherResponse?>()
5352
{
5453
Result = new WeatherResponse()
@@ -62,7 +61,7 @@ public async Task GetForecastActionTests_SuccessForecastNow()
6261
}
6362
});
6463

65-
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService.Object, _weatherApi.Object).Do(
64+
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService, _weatherApi).Do(
6665
new Message()
6766
{
6867
Chat = new Chat()
@@ -88,16 +87,15 @@ public async Task GetForecastActionTests_FailForecastNowWithWringUserId()
8887
{
8988
var expectedResult = ErrorMessageType.NotFound;
9089

91-
var _userManageService = new Mock<IUserManageService>();
92-
_userManageService.Setup(
93-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
94-
.ReturnsAsync(
90+
var _userManageService = Substitute.For<IUserManageService>();
91+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
92+
.Returns(
9593
new Response<UserViewModel?>()
9694
{
9795
Error = ErrorMessageType.NotFound,
9896
});
9997

100-
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService.Object, default!).Do(
98+
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService, default!).Do(
10199
new Message()
102100
{
103101
Chat = new Chat()
@@ -120,25 +118,22 @@ public async Task GetForecastActionTests_FailForecastNowWithWringUserId()
120118
public async Task GetForecastActionTests_FailForecastNowWhenWeatherServiceUnavaliable()
121119
{
122120
var expectedResult = ErrorMessageType.WeatherServiceUnavaliable;
123-
124-
var _userManageService = new Mock<IUserManageService>();
125-
_userManageService.Setup(
126-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
127-
.ReturnsAsync(
121+
var _userManageService = Substitute.For<IUserManageService>();
122+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
123+
.Returns(
128124
new Response<UserViewModel?>()
129125
{
130126
Result = _dummyUser
131127
});
132-
var _weatherApi = new Mock<IWeatherApiWrapper>();
133-
_weatherApi.Setup(
134-
_ => _.GetWeatherAsync(It.IsAny<WeatherRequest>(), It.IsAny<CancellationToken>()))
135-
.ReturnsAsync(
128+
var _weatherApi = Substitute.For<IWeatherApiWrapper>();
129+
_weatherApi.GetWeatherAsync(Arg.Any<WeatherRequest>(), Arg.Any<CancellationToken>())
130+
.Returns(
136131
new Response<WeatherResponse?>()
137132
{
138133
Error = ErrorMessageType.WeatherServiceUnavaliable,
139134
});
140135

141-
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService.Object, _weatherApi.Object).Do(
136+
var got = await new GetForecastAction(ForecastRequest.Now, _userManageService, _weatherApi).Do(
142137
new Message()
143138
{
144139
Chat = new Chat()

Tests/Client/Tests/Actions/RegisterActionTests.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MediatR;
22

3-
using Moq;
3+
4+
using NSubstitute;
45

56
using System;
67
using System.Collections.Generic;
@@ -26,16 +27,14 @@ public class RegisterActionTests
2627
[Fact]
2728
public async Task RegisterActionTests_Success()
2829
{
29-
var _userManageService = new Mock<IUserManageService>();
30-
_userManageService.Setup(
31-
_ => _.HandleAsync(It.IsAny<RegisterUserCommand>(), CancellationToken.None))
32-
.ReturnsAsync(
30+
var _userManageService = Substitute.For<IUserManageService>();
31+
_userManageService.HandleAsync(Arg.Any<RegisterUserCommand>(), Arg.Any<CancellationToken>())
32+
.Returns(
3333
new Response<Unit>()
3434
{
3535
Result = new Unit()
3636
});
37-
_userManageService.Verify();
38-
var got = await new RegisterAction(_userManageService.Object).Do(
37+
var got = await new RegisterAction(_userManageService).Do(
3938
new Message()
4039
{
4140
Chat = new Chat() { Id = UserStateContextFactory.UsersID.Values.Max() + 1 },

Tests/Client/Tests/Actions/RemoveAlarmActionTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
2-
using Moq;
2+
3+
using NSubstitute;
34

45
using System;
56
using System.Collections.Generic;
@@ -21,15 +22,14 @@ public class RemoveAlarmActionTests
2122
{
2223
private IUserManageService CreateMockService()
2324
{
24-
var _userManageService = new Mock<IUserManageService>();
25-
_userManageService.Setup(
26-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
27-
.ReturnsAsync(
25+
var _userManageService = Substitute.For<IUserManageService>();
26+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
27+
.Returns(
2828
new Response<Unit>()
2929
{
3030
Result = new Unit()
3131
});
32-
return _userManageService.Object;
32+
return _userManageService;
3333
}
3434

3535
[Fact]

Tests/Client/Tests/Actions/SetAlarmActionTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
2-
using Moq;
2+
3+
using NSubstitute;
34

45
using System;
56
using System.Collections.Generic;
@@ -21,15 +22,14 @@ public class SetAlarmActionTests
2122
{
2223
private IUserManageService CreateMockService()
2324
{
24-
var _userManageService = new Mock<IUserManageService>();
25-
_userManageService.Setup(
26-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
27-
.ReturnsAsync(
25+
var _userManageService = Substitute.For<IUserManageService>();
26+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
27+
.Returns(
2828
new Response<Unit>()
2929
{
3030
Result = new Unit()
3131
});
32-
return _userManageService.Object;
32+
return _userManageService;
3333
}
3434

3535
[Theory]

Tests/Client/Tests/Actions/UpdateActionTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
2-
using Moq;
2+
3+
using NSubstitute;
34

45
using System;
56
using System.Collections.Generic;
@@ -23,15 +24,14 @@ public class UpdateActionTests
2324
[Fact]
2425
public async Task UpdateActionTests_Success()
2526
{
26-
var _userManageService = new Mock<IUserManageService>();
27-
_userManageService.Setup(
28-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
29-
.ReturnsAsync(
27+
var _userManageService = Substitute.For<IUserManageService>();
28+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
29+
.Returns(
3030
new Response<Unit>()
3131
{
3232
Result = new Unit()
3333
});
34-
var got = await new UpdateAction(_userManageService.Object).Do(
34+
var got = await new UpdateAction(_userManageService).Do(
3535
new Message()
3636
{
3737
Chat = new Chat() { Id = UserStateContextFactory.UsersID.Values.Max() + 1 },

Tests/Client/Tests/States/WaitingForLocationTests.cs

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using MediatR;
22

3-
using Moq;
3+
using NSubstitute;
44

55
using OpenMeteoRemoteApi.Interfaces;
66

@@ -63,22 +63,20 @@ public async Task WaitingForLocationTests_SuccessRegister()
6363

6464
var userManageServiceSetupBehavior = () =>
6565
{
66-
var _userManageService = new Mock<IUserManageService>();
67-
_userManageService.Setup(
68-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
69-
.ReturnsAsync(
66+
var _userManageService = Substitute.For<IUserManageService>();
67+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
68+
.Returns(
7069
new Response<UserViewModel?>()
7170
{
7271
Error = Core.Domain.Models.Enums.ErrorMessageType.NotFound
7372
});
74-
_userManageService.Setup(
75-
_ => _.HandleAsync(It.IsAny<RegisterUserCommand>(), It.IsAny<CancellationToken>()))
76-
.ReturnsAsync(
73+
_userManageService.HandleAsync(Arg.Any<RegisterUserCommand>(), Arg.Any<CancellationToken>())
74+
.Returns(
7775
new Response<Unit>()
7876
{
7977
Result = new Unit()
8078
});
81-
return _userManageService.Object;
79+
return _userManageService;
8280
};
8381
var requestedObjects = SetupRequestedObjects(userManageServiceSetupBehavior, StateType.WaitingForLocation);
8482
var response = await requestedObjects.State.Handle(requestedObjects.Context, _requestMessage);
@@ -96,15 +94,14 @@ public async Task WaitingForLocationTests_FailRegisterWithError()
9694

9795
var userManageServiceSetupBehavior = () =>
9896
{
99-
var _userManageService = new Mock<IUserManageService>();
100-
_userManageService.Setup(
101-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
102-
.ReturnsAsync(
97+
var _userManageService = Substitute.For<IUserManageService>();
98+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
99+
.Returns(
103100
new Response<UserViewModel?>()
104101
{
105102
Error = Core.Domain.Models.Enums.ErrorMessageType.InternalServerError
106103
});
107-
return _userManageService.Object;
104+
return _userManageService;
108105
};
109106

110107
var requestedObjects = SetupRequestedObjects(userManageServiceSetupBehavior, StateType.WaitingForLocation);
@@ -122,15 +119,14 @@ public async Task WaitingForLocationTests_FailRegisterLocationIsNull()
122119

123120
var userManageServiceSetupBehavior = () =>
124121
{
125-
var _userManageService = new Mock<IUserManageService>();
126-
_userManageService.Setup(
127-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
128-
.ReturnsAsync(
122+
var _userManageService = Substitute.For<IUserManageService>();
123+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
124+
.Returns(
129125
new Response<UserViewModel?>()
130126
{
131127
Error = Core.Domain.Models.Enums.ErrorMessageType.InternalServerError
132128
});
133-
return _userManageService.Object;
129+
return _userManageService;
134130
};
135131

136132
var requestedObjects = SetupRequestedObjects(userManageServiceSetupBehavior, StateType.WaitingForLocation);
@@ -155,22 +151,20 @@ public async Task WaitingForLocationTests_UpdateIfUserExists()
155151

156152
var userManageServiceSetupBehavior = () =>
157153
{
158-
var _userManageService = new Mock<IUserManageService>();
159-
_userManageService.Setup(
160-
_ => _.HandleAsync(It.IsAny<GetUserQuery>(), It.IsAny<CancellationToken>()))
161-
.ReturnsAsync(
154+
var _userManageService = Substitute.For<IUserManageService>();
155+
_userManageService.HandleAsync(Arg.Any<GetUserQuery>(), Arg.Any<CancellationToken>())
156+
.Returns(
162157
new Response<UserViewModel?>()
163158
{
164159
Result = new UserViewModel() { Id = 1 }
165160
});
166-
_userManageService.Setup(
167-
_ => _.HandleAsync(It.IsAny<UpdateUserCommand>(), It.IsAny<CancellationToken>()))
168-
.ReturnsAsync(
161+
_userManageService.HandleAsync(Arg.Any<UpdateUserCommand>(), Arg.Any<CancellationToken>())
162+
.Returns(
169163
new Response<Unit>()
170164
{
171165
Result = new Unit()
172166
});
173-
return _userManageService.Object;
167+
return _userManageService;
174168
};
175169

176170
var requestedObjects = SetupRequestedObjects(userManageServiceSetupBehavior, StateType.WaitingForLocation);

0 commit comments

Comments
 (0)