Rihab Kasim
Rihab Kasim

Reputation: 37

How to Mock FunctionContext in Azure Isolated worker Model Function .Net 8

I have an Azure function in Isolated worker model type with .Net 8 version . I want to mock the function context for Unit testing.

Below code of Azure Function is given

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public class Function1
    {

        [Function("Function1")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,FunctionContext context)
        {
            var logger = context.GetLogger<Function1>();
            logger.LogInformation("C# HTTP trigger function processed a request.");
            return new OkObjectResult("Welcome to Azure Functions!");
        }
    }
}

I'm getting error when I tried to create FunctionContext object in unit test since it is an abstract class.

Upvotes: 0

Views: 1128

Answers (1)

Dasari Kamali
Dasari Kamali

Reputation: 3649

I tried the below sample unit test code to mock the FunctionContext in the Azure function HTTP trigger Isolated model in .Net 8.

Function1Tests.cs :

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Moq;

namespace FunctionApp1.Tests
{
    public static class FunctionContextExtensions
    {
        public static ILogger GetLogger<T>(this FunctionContext context)
        {
            return context.InstanceServices.GetService(typeof(ILogger<T>)) as ILogger<T>;
        }
    }
    public class Function1Tests
    {
        [Fact]
        public void Run_ShouldReturnWelcomeMessage()
        {
            var mockLogger = new Mock<ILogger<Function1>>();
            var mockFunctionContext = new Mock<FunctionContext>();
            mockFunctionContext.Setup(x => x.InstanceServices.GetService(typeof(ILogger<Function1>))).Returns(mockLogger.Object);
            var function = new Function1();
            var mockHttpRequest = new Mock<HttpRequest>();
            mockHttpRequest.Setup(req => req.Method).Returns("GET");
            var result = function.Run(mockHttpRequest.Object, mockFunctionContext.Object) as OkObjectResult;
            Assert.NotNull(result);
            Assert.Equal("Welcome to Azure Functions!", result.Value);
            mockLogger.Verify(
                x => x.Log(
                    LogLevel.Information,
                    It.IsAny<EventId>(),
                    It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("C# HTTP trigger function processed a request.")),
                    null,
                    (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()
                ),
                Times.Once
            );
        }
    }
}

Project structure :

enter image description here

The test cases were passed using the function context in Unit testing as shown below.

enter image description here

Upvotes: 1

Related Questions