WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 207 forks source link

FluentAssertions extensions are not open for extension #1075

Closed chrischu closed 6 months ago

chrischu commented 7 months ago

Is your feature request related to a problem? Please describe. Currently the FluentAssertion extensions offer a pretty small feature set, which would not be a problem by itself, but the fact that they are not very open for extension makes this really problematic.

Describe the solution you'd like I would like to for example access all the filtered requests on the WireMockAssertions object. That way I could write my own extension methods for it to for example assert that a request was using a specific path (currently only Url & AbsoluteUrl exist).

Describe alternatives you've considered As far as I can tell the only alternative is using reflection to access the private members of WireMockAssertions.

StefH commented 6 months ago

@chrischu I've updated the code for WireMockAssertions so that you now can create an extension method like:

using System;
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
using WireMock.FluentAssertions;

namespace WireMock.Net.Tests.FluentAssertions;

public static class WireMockAssertionsExtensions
{
    [CustomAssertion]
    public static AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl2(this WireMockAssertions assertions,
        string absoluteUrl, string because = "", params object[] becauseArgs)
    {
        var (filter, condition) = assertions.BuildFilterAndCondition(request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase));

        Execute.Assertion
            .BecauseOf(because, becauseArgs)
            .Given(() => assertions.RequestMessages)
            .ForCondition(requests => assertions.CallsCount == 0 || requests.Any())
            .FailWith(
                "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
                absoluteUrl
            )
            .Then
            .ForCondition(condition)
            .FailWith(
                "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but didn't find it among the calls to {1}.",
                _ => absoluteUrl,
                requests => requests.Select(request => request.AbsoluteUrl)
            );

        assertions.FilterRequestMessages(filter);

        return new AndWhichConstraint<WireMockAssertions, string>(assertions, absoluteUrl);
    }
}

Note that the RequestMessages, BuildFilterAndCondition and FilterRequestMessages are made public so these can be used in the extension methods.

Would this fix your issue?


And if you have any useful default fliuent extensions, you can also add these to this project using a PR.

chrischu commented 6 months ago

Oh sorry I just saw the comment, yes this looks perfect, thank you!