FluentAssertions extensions are not open for extension #582

Closed
opened 2025-12-29 15:27:34 +01:00 by adam · 2 comments
Owner

Originally created by @chrischu on GitHub (Mar 4, 2024).

Originally assigned to: @StefH on GitHub.

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.

Originally created by @chrischu on GitHub (Mar 4, 2024). Originally assigned to: @StefH on GitHub. **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`.
adam added the feature label 2025-12-29 15:27:34 +01:00
adam closed this issue 2025-12-29 15:27:34 +01:00
Author
Owner

@StefH commented on GitHub (Mar 9, 2024):

@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.

@StefH commented on GitHub (Mar 9, 2024): @chrischu I've updated the code for WireMockAssertions so that you now can create an extension method like: ``` c# 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.
Author
Owner

@chrischu commented on GitHub (Mar 12, 2024):

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

@chrischu commented on GitHub (Mar 12, 2024): Oh sorry I just saw the comment, yes this looks perfect, thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#582