Error while executing test, if using CreateResponse extention method to return Azure Function HttpResonseMessage

后端 未结 2 1975
予麋鹿
予麋鹿 2020-12-06 04:35

My Azure Function code is like below

public static class MyHttpTriggerFunction
{       
    public static async Task Run([HttpTrig         


        
相关标签:
2条回答
  • 2020-12-06 04:50

    This is not specific to Azure Functions, but in order to execute this test outside of the context of an actual HTTP request you need to make sure you create an HttpConfiguartion instance, configure it as required (e.g. add any formatters you may need) and call SetConfiguration on the HttpRequestMessage instance with that object.

    Example:

    var configuration = new HttpConfiguration();
    var request = new HttpRequestMessage();
    request.SetConfiguration(configuration);
    
    0 讨论(0)
  • 2020-12-06 04:55

    The Error message is telling you the problem.

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

    When testing the request out side of a httpserver you need to give the request a HttpConfiguration.

    // Arrange.
    var configuration = new HttpConfiguration();
    var request = new System.Net.Http.HttpRequestMessage();
    request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
    
    //...other code
    
    0 讨论(0)
提交回复
热议问题