Skip to content

Commit 38e24f4

Browse files
authored
Merge pull request #370 from amirkaws/replace-moq-with-nsubstitute
2 parents ee1f06f + 3f93097 commit 38e24f4

File tree

41 files changed

+3318
-3849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3318
-3849
lines changed

examples/Idempotency/test/HelloWorld.Test/FunctionTest.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Net;
19-
using System.Net.Http;
20-
using System.Text.Json;
21-
using System.Threading;
2217
using System.Threading.Tasks;
2318
using Amazon.DynamoDBv2;
24-
using Amazon.DynamoDBv2.DataModel;
25-
using Amazon.DynamoDBv2.Model;
2619
using Xunit;
2720
using Amazon.Lambda.APIGatewayEvents;
2821
using Amazon.Lambda.TestUtilities;
29-
using DotNet.Testcontainers.Builders;
30-
using DotNet.Testcontainers.Containers;
31-
using Moq;
32-
using Moq.Protected;
3322
using Xunit.Abstractions;
3423

3524
namespace HelloWorld.Tests

examples/Idempotency/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
10-
<PackageReference Include="Moq" Version="4.18.4" />
11-
<PackageReference Include="xunit" Version="2.4.2" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
10+
<PackageReference Include="xunit" Version="2.5.0" />
1211
<PackageReference Include="Testcontainers" Version="3.3.0" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1413
<PrivateAssets>all</PrivateAssets>
1514
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1615
</PackageReference>

examples/Logging/src/HelloWorld/HelloWorld.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
9-
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
9+
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
1212
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
1313
</ItemGroup>
1414
</Project>

examples/Logging/test/HelloWorld.Test/FunctionTest.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
using Xunit;
2525
using Amazon.Lambda.APIGatewayEvents;
2626
using Amazon.Lambda.TestUtilities;
27-
using Moq;
28-
using Moq.Protected;
27+
using NSubstitute;
2928
using Xunit.Abstractions;
3029

3130
namespace HelloWorld.Tests
@@ -39,6 +38,20 @@ public FunctionTest(ITestOutputHelper testOutputHelper)
3938
_testOutputHelper = testOutputHelper;
4039
}
4140

41+
private class MockHttpMessageHandler : HttpMessageHandler
42+
{
43+
private readonly HttpResponseMessage _responseMessage;
44+
public MockHttpMessageHandler(HttpResponseMessage responseMessage)
45+
{
46+
_responseMessage = responseMessage;
47+
}
48+
49+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
50+
{
51+
return await Task.FromResult(_responseMessage);
52+
}
53+
}
54+
4255
[Fact]
4356
public async Task TestHelloWorldFunctionHandler()
4457
{
@@ -47,21 +60,12 @@ public async Task TestHelloWorldFunctionHandler()
4760
var accountId = Guid.NewGuid().ToString("D");
4861
var location = "192.158. 1.38";
4962

50-
var dynamoDbContext = new Mock<IDynamoDBContext>();
51-
var handlerMock = new Mock<HttpMessageHandler>();
52-
handlerMock
53-
.Protected()
54-
.Setup<Task<HttpResponseMessage>>(
55-
"SendAsync",
56-
ItExpr.IsAny<HttpRequestMessage>(),
57-
ItExpr.IsAny<CancellationToken>()
58-
)
59-
.ReturnsAsync(new HttpResponseMessage
60-
{
61-
StatusCode = HttpStatusCode.OK,
62-
Content = new StringContent(location)
63-
})
64-
.Verifiable();
63+
var dynamoDbContext = Substitute.For<IDynamoDBContext>();
64+
var handlerMock = new MockHttpMessageHandler(new HttpResponseMessage
65+
{
66+
StatusCode = HttpStatusCode.OK,
67+
Content = new StringContent(location)
68+
});
6569

6670
var request = new APIGatewayProxyRequest
6771
{
@@ -94,7 +98,7 @@ public async Task TestHelloWorldFunctionHandler()
9498
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
9599
};
96100

97-
var function = new Function(dynamoDbContext.Object, new HttpClient(handlerMock.Object));
101+
var function = new Function(dynamoDbContext, new HttpClient(handlerMock));
98102
var response = await function.FunctionHandler(request, context);
99103

100104
_testOutputHelper.WriteLine("Lambda Response: \n" + response.Body);

examples/Logging/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
99
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
11-
<PackageReference Include="Moq" Version="4.18.3" />
12-
<PackageReference Include="xunit" Version="2.4.2" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
11+
<PackageReference Include="NSubstitute" Version="5.0.0" />
12+
<PackageReference Include="xunit" Version="2.5.0" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>

examples/Metrics/src/HelloWorld/HelloWorld.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
9-
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
9+
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.2.0" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.2" />
1313
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
1414
</ItemGroup>
1515
</Project>

examples/Metrics/test/HelloWorld.Test/FunctionTest.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
using Xunit;
2525
using Amazon.Lambda.APIGatewayEvents;
2626
using Amazon.Lambda.TestUtilities;
27-
using Moq;
28-
using Moq.Protected;
27+
using NSubstitute;
2928
using Xunit.Abstractions;
3029

3130
namespace HelloWorld.Tests
@@ -39,6 +38,20 @@ public FunctionTest(ITestOutputHelper testOutputHelper)
3938
_testOutputHelper = testOutputHelper;
4039
}
4140

41+
private class MockHttpMessageHandler : HttpMessageHandler
42+
{
43+
private readonly HttpResponseMessage _responseMessage;
44+
public MockHttpMessageHandler(HttpResponseMessage responseMessage)
45+
{
46+
_responseMessage = responseMessage;
47+
}
48+
49+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
50+
{
51+
return await Task.FromResult(_responseMessage);
52+
}
53+
}
54+
4255
[Fact]
4356
public async Task TestHelloWorldFunctionHandler()
4457
{
@@ -47,21 +60,12 @@ public async Task TestHelloWorldFunctionHandler()
4760
var location = "192.158. 1.38";
4861
Environment.SetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE","AWSLambdaPowertools");
4962

50-
var dynamoDbContext = new Mock<IDynamoDBContext>();
51-
var handlerMock = new Mock<HttpMessageHandler>();
52-
handlerMock
53-
.Protected()
54-
.Setup<Task<HttpResponseMessage>>(
55-
"SendAsync",
56-
ItExpr.IsAny<HttpRequestMessage>(),
57-
ItExpr.IsAny<CancellationToken>()
58-
)
59-
.ReturnsAsync(new HttpResponseMessage
60-
{
61-
StatusCode = HttpStatusCode.OK,
62-
Content = new StringContent(location)
63-
})
64-
.Verifiable();
63+
var dynamoDbContext = Substitute.For<IDynamoDBContext>();
64+
var handlerMock = new MockHttpMessageHandler(new HttpResponseMessage
65+
{
66+
StatusCode = HttpStatusCode.OK,
67+
Content = new StringContent(location)
68+
});
6569

6670
var request = new APIGatewayProxyRequest
6771
{
@@ -94,7 +98,7 @@ public async Task TestHelloWorldFunctionHandler()
9498
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
9599
};
96100

97-
var function = new Function(dynamoDbContext.Object, new HttpClient(handlerMock.Object));
101+
var function = new Function(dynamoDbContext, new HttpClient(handlerMock));
98102
var response = await function.FunctionHandler(request, context);
99103

100104
_testOutputHelper.WriteLine("Lambda Response: \n" + response.Body);

examples/Metrics/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
99
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
11-
<PackageReference Include="Moq" Version="4.18.3" />
12-
<PackageReference Include="xunit" Version="2.4.2" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
11+
<PackageReference Include="NSubstitute" Version="5.0.0" />
12+
<PackageReference Include="xunit" Version="2.5.0" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>

examples/Parameters/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
9-
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
9+
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
1111
<PackageReference Include="AWS.Lambda.Powertools.Parameters" Version="0.0.2-preview" />
1212
</ItemGroup>

examples/Parameters/test/HelloWorld.Test/FunctionTest.cs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using Amazon.Lambda.APIGatewayEvents;
2020
using Amazon.Lambda.TestUtilities;
2121
using Xunit;
22-
using Moq;
22+
using NSubstitute;
2323
using System.Text.Json;
2424

2525
namespace HelloWorld.Tests
@@ -32,7 +32,7 @@ public async Task TestHelloWorldFunctionHandler()
3232
// Arrange
3333
var requestId = Guid.NewGuid().ToString("D");
3434
var accountId = Guid.NewGuid().ToString("D");
35-
35+
3636
var request = new APIGatewayProxyRequest
3737
{
3838
RequestContext = new APIGatewayProxyRequest.ProxyRequestContext
@@ -41,28 +41,26 @@ public async Task TestHelloWorldFunctionHandler()
4141
AccountId = accountId
4242
}
4343
};
44-
44+
4545
var context = new TestLambdaContext()
4646
{
4747
FunctionName = Guid.NewGuid().ToString("D"),
4848
FunctionVersion = "1",
4949
MemoryLimitInMB = 215,
5050
AwsRequestId = requestId
5151
};
52-
53-
var helper = new Mock<IParameterLookupHelper>();
54-
52+
53+
var helper = Substitute.For<IParameterLookupHelper>();
54+
5555
var singleSsmRecord = new ParameterLookupRecord
5656
{
5757
Provider = ParameterProviderType.SsmProvider,
5858
Method = ParameterLookupMethod.Get,
5959
Key = Guid.NewGuid().ToString("D"),
6060
Value = Guid.NewGuid().ToString("D")
6161
};
62-
helper.Setup(c =>
63-
c.GetSingleParameterWithSsmProvider()
64-
).ReturnsAsync(singleSsmRecord);
65-
62+
helper.GetSingleParameterWithSsmProvider().Returns(singleSsmRecord);
63+
6664
var multipleSsmRecord = new ParameterLookupRecord
6765
{
6866
Provider = ParameterProviderType.SsmProvider,
@@ -74,9 +72,7 @@ public async Task TestHelloWorldFunctionHandler()
7472
{ Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D") }
7573
}
7674
};
77-
helper.Setup(c =>
78-
c.GetMultipleParametersWithSsmProvider()
79-
).ReturnsAsync(multipleSsmRecord);
75+
helper.GetMultipleParametersWithSsmProvider().Returns(multipleSsmRecord);
8076

8177
var singleSecretRecord = new ParameterLookupRecord
8278
{
@@ -89,21 +85,17 @@ public async Task TestHelloWorldFunctionHandler()
8985
Password = Guid.NewGuid().ToString("D")
9086
}
9187
};
92-
helper.Setup(c =>
93-
c.GetSingleSecretWithSecretsProvider()
94-
).ReturnsAsync(singleSecretRecord);
95-
88+
helper.GetSingleSecretWithSecretsProvider().Returns(singleSecretRecord);
89+
9690
var singleDynamoDbRecord = new ParameterLookupRecord
9791
{
9892
Provider = ParameterProviderType.DynamoDBProvider,
9993
Method = ParameterLookupMethod.Get,
10094
Key = Guid.NewGuid().ToString("D"),
10195
Value = Guid.NewGuid().ToString("D")
10296
};
103-
helper.Setup(c =>
104-
c.GetSingleParameterWithDynamoDBProvider()
105-
).ReturnsAsync(singleDynamoDbRecord);
106-
97+
helper.GetSingleParameterWithDynamoDBProvider().Returns(singleDynamoDbRecord);
98+
10799
var multipleDynamoDbRecord = new ParameterLookupRecord
108100
{
109101
Provider = ParameterProviderType.DynamoDBProvider,
@@ -115,10 +107,8 @@ public async Task TestHelloWorldFunctionHandler()
115107
{ Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D") }
116108
}
117109
};
118-
helper.Setup(c =>
119-
c.GetMultipleParametersWithDynamoDBProvider()
120-
).ReturnsAsync(multipleDynamoDbRecord);
121-
110+
helper.GetMultipleParametersWithDynamoDBProvider().Returns(multipleDynamoDbRecord);
111+
122112
var body = new Dictionary<string, object>
123113
{
124114
{ "RequestId", requestId },
@@ -134,7 +124,7 @@ public async Task TestHelloWorldFunctionHandler()
134124
}
135125
}
136126
};
137-
127+
138128
var expectedResponse = new APIGatewayProxyResponse
139129
{
140130
Body = JsonSerializer.Serialize(body),
@@ -143,15 +133,15 @@ public async Task TestHelloWorldFunctionHandler()
143133
};
144134

145135
// Act
146-
var function = new Function(helper.Object);
136+
var function = new Function(helper);
147137
var response = await function.FunctionHandler(request, context).ConfigureAwait(false);
148138

149139
// Assert
150-
helper.Verify(v => v.GetSingleParameterWithSsmProvider(), Times.Once);
151-
helper.Verify(v => v.GetMultipleParametersWithSsmProvider(), Times.Once);
152-
helper.Verify(v => v.GetSingleSecretWithSecretsProvider(), Times.Once);
153-
helper.Verify(v => v.GetSingleParameterWithDynamoDBProvider(), Times.Once);
154-
helper.Verify(v => v.GetMultipleParametersWithDynamoDBProvider(), Times.Once);
140+
await helper.Received(1).GetSingleParameterWithSsmProvider();
141+
await helper.Received(1).GetMultipleParametersWithSsmProvider();
142+
await helper.Received(1).GetSingleSecretWithSecretsProvider();
143+
await helper.Received(1).GetSingleParameterWithDynamoDBProvider();
144+
await helper.Received(1).GetMultipleParametersWithDynamoDBProvider();
155145
Assert.Equal(expectedResponse.Body, response.Body);
156146
Assert.Equal(expectedResponse.Headers, response.Headers);
157147
Assert.Equal(expectedResponse.StatusCode, response.StatusCode);

examples/Parameters/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0-preview.23280.1" />
10-
<PackageReference Include="Moq" Version="4.18.4" />
11-
<PackageReference Include="xunit" Version="2.4.2" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
10+
<PackageReference Include="NSubstitute" Version="5.0.0" />
11+
<PackageReference Include="xunit" Version="2.5.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>

0 commit comments

Comments
 (0)