Reputation: 5075
I am working on xUnit for.NET CORE Azure Function that have HttpTrigger
. I have managed to mock HttpTrigger that expect to receive data in the body but struggling with Query String. I have made the generic function outside the test class so that it can be used by other Azure Functions tests.
I need help to create mock for HttpRequest that accept query string. I believe need mock that Setup type of IQueryCollection
[FunctionName("MyFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "DELETE")] HttpRequest req,
[ServiceBus("MyServiceBus", Connection = "MyServiceBusConn")] IAsyncCollector<Message> servicebusMessage)
{
string sessionId = string.Empty;
var DateTimeNow = DateTime.Now;
sessionId = req.Query["sessions"]; //Mock to return this??
}
public Mock<HttpRequest> CreateMockRequest(object body)
{
var memoryStream = new MemoryStream();
var writer = new StreamWriter(memoryStream);
var json = JsonConvert.SerializeObject(body);
writer.Write(json);
writer.Flush();
memoryStream.Position = 0;
var mockRequest = new Mock<HttpRequest>();
mockRequest.Setup(x => x.Body).Returns(memoryStream);
mockRequest.Setup(x => x.ContentType).Returns("application/json");
return mockRequest;
}
Need help in following method
public Mock<HttpRequest> CreateQueryMockRequest(object body)
{
var memoryStream = new MemoryStream();
var writer = new StreamWriter(memoryStream);
var json = JsonConvert.SerializeObject(body);
var mockRequest = new Mock<HttpRequest>();
mockRequest.Setup(x => x.Query).Returns(json); // This doesn't work??
mockRequest.Setup(x => x.ContentType).Returns("application/json");
return mockRequest;
}
[Fact]
public void Function_ShouldReturn_XYZ()
{
//Arrange
var providerSessionId = RingGoExemptionTestData.GetProviderSession(); //GetProviderSession() implementation below
Mock<HttpRequest> mockHttpRequest = httpResquestFactory.CreateQueryMockRequest(providerSessionId); // this is where I am trying to use method define above
}
public static RingGoSession GetProviderSession()
{
var ringGoSession = new RingGoSession
{
RingGoRef = "232d3f"
};
return ringGoSession;
}
Upvotes: 0
Views: 1882
Reputation: 5075
got the answer;
'Generic Method to deal with Query String`
public Mock<HttpRequest> CreateMockHttpRequest(Dictionary<string, StringValues> query)
{
var context = new DefaultHttpContext();
var request = context.Request;
request.Query = new QueryCollection(query);
var mockRequest = new Mock<HttpRequest>();
mockRequest.Setup(x => x.Query).Returns(request.Query);
return mockRequest;
}
This is how to create Mock by passing Query
var query = new Dictionary<string, StringValues>();
query.TryAdd("myKey", MyKeyValue);
Mock<HttpRequest> mockHttpRequest = httpResquestFactory.CreateMockHttpRequest(query);
Upvotes: 2
Reputation: 10849
You can create a new instance of QueryCollection
(read this) and setup the mock.
var mockDict = new Dictionary<string, StringValues>
{
{ "key1", "some value" },
{ "sessions", "random session string"}
};
mockRequest.Setup(x => x.Query).Returns(new QueryCollection(mockDict));
Upvotes: 1