Unit Testing Azure function with MsTest ERROR : A serializer is not configured for the worker

56 views Asked by At

I'm trying to unit test an azure function API Endpoint and i have this Exception on WriteAsJsonAsync : enter image description here

Here is my Test :

[TestMethod, TestCategory("CreateOpportunity")]
public void CreateOpportunity_OK()
{
    //// Test parameters
    string testRequest = Crm2CrmTestModels.GetRequest();
    OpportunityCreateResponse testOpportunityCreateResponse = Crm2CrmTestModels.GetOpportunityCreateResponse();

    ServiceCollection serviceCollection = new ServiceCollection();
    serviceCollection.AddScoped<ILoggerFactory, LoggerFactory>();
    ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

    Mock<FunctionContext> context = new Mock<FunctionContext>();
    context.SetupProperty(c => c.InstanceServices, serviceProvider);

    MemoryStream body = new MemoryStream(Encoding.ASCII.GetBytes(testRequest));
    FakeHttpRequestData request = new FakeHttpRequestData(
                    context.Object,
                    new Uri("https://test.com"),
                    body);

    // Setup mocks
    Mock<ICrm2CrmBusiness> mockCrm2CrmBusiness = new(MockBehavior.Strict);
    mockCrm2CrmBusiness.Setup(b => b.Exists(It.IsAny<Opportunity>())).ReturnsAsync(It.IsAny<string>());
    mockCrm2CrmBusiness.Setup(b => b.CreateOpportunity(It.IsAny<Opportunity>())).ReturnsAsync(testOpportunityCreateResponse);

    ICrm2CrmBusiness consentsBusiness = mockCrm2CrmBusiness.Object;

    ILogger<Crm2CrmFunction> logger = new Mock<ILogger<Crm2CrmFunction>>().Object;

    Mock<ICrm2CrmAdapter> mockCrm2CrmAdapter = new(MockBehavior.Strict);
    mockCrm2CrmAdapter.Setup(b => b.ConvertToBusiness(It.IsAny<Crm2Crm.FunctionsApp.Models.Request.Opportunity>())).Returns(It.IsAny<Opportunity>());

    ICrm2CrmAdapter crm2CrmAdapter = mockCrm2CrmAdapter.Object;

    // Execute
    Crm2CrmFunction functions = new(logger, consentsBusiness, crm2CrmAdapter);
    //A serializer is not configured for the worker
    HttpResponseData result = functions.CreateOpportunity(httpRequest: request, context.Object).Result;

    mockCrm2CrmBusiness.Verify(b => b.Exists(It.IsAny<Opportunity>()).Result, Times.Never);
    mockCrm2CrmBusiness.Verify(b => b.CreateOpportunity(It.IsAny<Opportunity>()).Result, Times.Never);

    Assert.IsNotNull(result);
    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

How can i solve my problem ?

Thanks.

I tried to unit test an azure function that contains WriteAsJsonAsync

0

There are 0 answers