2424using Xunit ;
2525using Amazon . Lambda . APIGatewayEvents ;
2626using Amazon . Lambda . TestUtilities ;
27- using Moq ;
28- using Moq . Protected ;
27+ using NSubstitute ;
2928using Xunit . Abstractions ;
3029
3130namespace 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 ) ;
0 commit comments