I wrote an azure function v1 HttpTrigger that gets client details and transactions from our api service, there is only one parameter which is "frequency" which is optional also, so when the function is triggered it will get the details then get transactions for each retailer details and returns a list of transaction fee for each retailer, I want to write a unit test for my function but I am not able to see a good example for my scenario , Can someone give me an example unit test (with moq if possible) , here is the function codebase example:
[FunctionName("FunctionTest1")]
public static async Task<HttpResponseMessage>
Run([HttpTrigger(AuthorizationLevel.Function)]HttpRequestMessage req, ILogger log) {
log.LogInformation("C# HTTP trigger function processed a request.");
#region Properties
string Frequency = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "frequency", true) == 0).Value;
#endregion
log.LogInformation("Getting client details from MongoDB");
Process of Getting ClientDetails
log.LogInformation("Get and compute Transaction Fee foreach retailers from client details");
Process of Getting And Computing Transactions for each retailers (NOTE Took too much time)
log.LogInformation("Return results response");
return txnList == null
? new HttpResponseMessage(HttpStatusCode.InternalServerError) {
Content = new StringContent(JsonConvert.SerializeObject("Error getting data from server"), Encoding.UTF8, "application/json")
} : new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(JsonConvert.SerializeObject(txnList, Newtonsoft.Json.Formatting.Indented), Encoding.UTF8, "application/json")
};
}
References for unit test I tried: https://learn.microsoft.com/en-us/azure/azure-functions/functions-test-a-function
https://medium.com/@tsuyoshiushio/writing-unit-test-for-azure-durable-functions-80f2af07c65e
What I tried:
[TestMethod]
public async Task Response_Should_Not_Null()
{
var request = new HttpRequestMessage();
var logger = Mock.Of<ILogger>();
var response = await FunctionTest1.Run(request, logger).ConfigureAwait(false);
Assert.IsNotNull(response);
}
Errors I got:
The thread 0x5580 has exited with code 0 (0x0).
The program '[23748] dotnet.exe' has exited with code 0 (0x0).
The program '[23748] dotnet.exe: Program Trace' has exited with code 0 (0x0).
Regards,
Nico