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

WithException assertion for comparing logged exception within specific message #27

Open
rafek1241 opened this issue Mar 7, 2022 · 1 comment

Comments

@rafek1241
Copy link

suggestion:

            var data = new Exception("Example error exception");
            _logger.Information(data, "Example error");

            InMemorySink.Instance
                .Should()
                .HaveMessage("Example error with sensitive data")
                .WithException(data) //<-- addition
                .WithException(assertions=> assertions //<-- another variant
                   .WithMessage(data.Message)
                   .WithData("somekey",data["somekey"])
                )
@sandermvanvliet
Copy link
Contributor

Hi @rafek1241

I'm not entirely sure yet on the way this should actually look.
As part of #22 I actually added some extension methods which add HaveErrorMessageWithException<T> as part of the test case.

I'm currently thinking along the lines of adding the following:

private void GivenLogMessaageWithException()
{
    var exception = new Exception("BANG BANG!");
    exception.Data.Add("some", "property");

    _logger.Error(exception, "BANG!");
}

[Fact]
public void GivenLogMessageWithException_NewStyle()
{
    GivenLogMessaageWithException();

    _inMemorySink
        .Should()
        .HaveMessage("BANG!")
        .Appearing()
        .Once()
        .WithException<Exception>() // Asserts that there is an exception and that the type matches the expectation
        .AndMessage("BANG BANG!");
}

[Fact]
public void GivenLogMessageWithException_NewStyleFullMatch()
{
    GivenLogMessaageWithException();

    var exception = new Exception("BANG BANG!");
    exception.Data.Add("some", "property");

    _inMemorySink
        .Should()
        .HaveMessage("BANG!")
        .Appearing()
        .Once()
        .WithException<Exception>()
        .Matching(exception); // This is a shortcut for WithException<Exception>().Subject.Should().BeEquivalentTo(exception)
}

Let me know what you think.

sandermvanvliet added a commit that referenced this issue Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants