Avanade / UnitTestEx

UnitTestEx provides .NET testing extensions to the most popular testing frameworks (MSTest, NUnit and Xunit) specifically to improve the testing experience with ASP.NET controller, and Azure Function, execution including underlying HttpClientFactory mocking.
MIT License
18 stars 4 forks source link

Using Flurl with UnitTestEx #52

Closed karpikpl closed 1 year ago

karpikpl commented 1 year ago

Flurl can be used with TestServer - see this stack overflow answer - https://stackoverflow.com/questions/50155702/can-i-use-flurlclient-with-asp-net-core-testserver but UnitTestEx does not expose TestServer via an API, would it be possible to allow it?

chullybun commented 1 year ago

Published NuGet package v2.1.2.

karpikpl commented 1 year ago

The fix worked. I was able to use Flurl and have a valid unit test:

    public async Task HealthCheckEndpoint_Should_BeExposed_And_Return503_When_CheckFails()
    {
        // Arrange
        // fake & record all http calls in the test subject
        using (var httpTest = new HttpTest()) {
            httpTest.RespondWith("server error", 500);

            using var test = CreateApiTester<SampleApp.Startup>();
            test.Http().TestServer.PreserveExecutionContext = true;

            // Act
            var result = await test.Http()
                .RunAsync(HttpMethod.Get, "/health");

            // Assert
            System.Text.Json.JsonElement response = result.GetValue<dynamic>();
            var status = response.GetProperty("healthReport").GetProperty("status").GetString();
            status.Should().Be("Unhealthy", because: "the health check was mocked to fail");
            httpTest.ShouldHaveMadeACall();
            httpTest.ShouldHaveCalled("https://www.mock-url.com/*")
                .WithVerb(HttpMethod.Head);
        }
    }