Skip to content

Version 4 not work well with WebApplicationFactory #174

Open
@raphaelm22

Description

@raphaelm22

The Verify.MicrosoftLogging does not work well after updating to version 4 when it's used WebApplicationFactory together.

The Initialization was created like the README.
Bellow the WebApplicationFactory implementation:

internal class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder) => ConfigureVerifyLoggingV4(builder);
    static void ConfigureVerifyLoggingV4(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services => services.AddLogging(options =>
        {
            options.ClearProviders();
            Recording.Start();
            var verifyLoggerProvider = new RecordingProvider();
            options.AddProvider(verifyLoggerProvider);
        }));
    }
}

And, the test like this:

public class WeatherforecastTests
{
    readonly CustomWebApplicationFactory _applicationFactory = new();

    [Fact]
    public async Task Test1()
    {
        // Arrange
        var client = _applicationFactory.CreateClient();
        // Act
        var result = await client.GetAsync("/weatherforecast");
        // Assert
        await Verify(new { result.StatusCode });
    }
}

When run, the exception below is thrown:

System.Exception: 'Recording.Start must be called before Recording.Add'
System.Exception
  HResult=0x80131500
  Message=Recording.Start must be called before Recording.Add
  Source=Verify
  StackTrace:
   at VerifyTests.Recording.CurrentState(String caller) in /_/src/Verify/Recording/Recording.cs:line 70

image

This is the project that was created to try to diagnose this issues:
issues-vrflog.zip

If you go back to version 3.4.0 and change the ConfigureWebHost method, it will work fine.

protected override void ConfigureWebHost(IWebHostBuilder builder) => ConfigureVerifyLoggingV3(builder);

static void ConfigureVerifyLoggingV3(IWebHostBuilder builder)
{
    var verifyLoggerProvider = LoggerRecording.Start();

    builder.ConfigureTestServices(services => services.AddLogging(options =>
    {
        options.ClearProviders();
        options.AddProvider(verifyLoggerProvider);
    }));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions