Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FluentAssertions extensions are not open for extension #1075

Closed
chrischu opened this issue Mar 4, 2024 · 2 comments · Fixed by #1082
Closed

FluentAssertions extensions are not open for extension #1075

chrischu opened this issue Mar 4, 2024 · 2 comments · Fixed by #1082
Assignees
Labels

Comments

@chrischu
Copy link

chrischu commented Mar 4, 2024

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
Copy link
Collaborator

StefH commented 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.

@chrischu
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants