I am trying to implement some unit/integration testing to an Azure v3 HTTP trigger and am running into some issues with the process.
I previously wrote some testing for an API controller that was using syntax like so:
TransactionResponse fakeResponse = new TransactionResponse { APIResponse = "Okay" };
var fakeSbus = A.Fake<IServiceBusPlatform>();
var fakeTransaction = A.Fake<ITransaction>();
A.CallTo(() => fakeTransaction.CreateTransaction(tran)).Returns(Task.FromResult(fakeResponse.APIResponse));
var controller = new SubmitController(fakeSbus, fakeTransaction);
var result = await controller._transaction.CreateTransaction(tran);
Assert.Equal(fakeResponse.APIResponse, result.ToString());
This worked fine because it was just testing a dbcontext and then asserting the return.
What I'm trying to do with the Azure function is evaluate the trigger based on the OkObjectResult but inside the function I have a few method calls and then logic that is dependant on the result of the method call. (returning a list, using a property of ListMember[0] to declare a var). Like so:
var BatchResults = await _dbService.RetrieveBatch(session);
var CompanyID = BatchResults[0].CompanyID;
Update: For a little more clarity here is the whole testing function:
var fakeBatch = new List<RetrievedBatch>();
fakeBatch.Add(new RetrievedBatch
{
CompanyID = new Guid(),
});
var testResponse = new OkObjectResult("OKAY");
var request = A.Fake<HttpRequest>();
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(fakeTransaction));
MemoryStream stream = new MemoryStream(byteArray);
request.Body = stream;
var logger = A.Fake<ILogger>();
var fakeService = A.Fake<IDbService>();
A.CallTo(() => fakeService.RetrieveBatch(fakeTransaction)).Returns(fakeBatch);
var test = fakeService.RetrieveBatch(fakeTransaction);
var SM = new SessionManager(fakeService);
var response = await SM.Run(request, logger);
Oh and the method/type inside the interface is like this:
Task<List<RetrievedBatch>> RetrieveBatch(Transaction session);
The error is occurring inside SM.Run(x,y). After a bit of debugging I can see that the A.CallTo is returning the correct results inside of the testing function but when the Azure function is being run the value is not carried over, which is the code breaking issue.
Any help would be appreciated as I've spent far too much time trying to work this out and all the examples out there are basic and unfortunately not very helpful in this scenario.