0

I have http Azure function and I'm writing unit test of that function. I'm getting below error

Message: Test method Functions.Tests.CacheFunctionTests.CacheRefreshFunction threw exception: System.InvalidOperationException: The request does not have an associated configuration object or the provided configuration was null.

UnitTest

[TestMethod]
    public async Task CacheRefreshFunction()
    {
        // Arrange
        mockLog.Setup(x => x.Info(It.IsAny<string>()));
        mockService.Setup(x => x.RefreshCache()).Returns(Task.FromResult(0));
        HttpRequestMessage httpRequestMessage = new HttpRequestMessage() {
            RequestUri = new System.Uri("http://localhost:0895/CacheRefreshFunction"),
            Method = HttpMethod.Get                
        };            
        // Act
        await Functions.CacheRefreshFunction.Run(httpRequestMessage, mockRuleService.Object, mockLog.Object);

        // Assert
        Assert.IsTrue(true);
    }

I guess, I'm not passing some essential value in "HttpRequestMessage" property. Any idea?

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73
  • guess you are missing configuration, like the error tells you... https://stackoverflow.com/questions/44447232/error-while-executing-test-if-using-createresponse-extention-method-to-return-a/44447349 – Jocke May 03 '19 at 13:44

1 Answers1

0

System.InvalidOperationException: The request does not have an associated configuration object or the provided configuration was null.

As Jocke said, when testing the request out side of a httpserver you need to give the request a HttpConfiguration.

To fix this we need to add a HttpConfiguration object through the Properties dictionary as below:

Request = new HttpRequestMessage()
{
    Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } },
    RequestUri = new System.Uri("http://localhost:0895/RuleCacheRefreshFunction"),
    Method = HttpMethod.Get  
}

I added Microsoft.AspNet.WebApi NuGet package to add System.Web.Http.HttpConfiguration

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73
Joey Cai
  • 18,968
  • 1
  • 20
  • 30