1

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.