fluentassertions.analyzers icon indicating copy to clipboard operation
fluentassertions.analyzers copied to clipboard

Create a refactoring for asserting exceptions properties

Open Meir017 opened this issue 7 years ago • 3 comments

for example:

public class MyCustomException : Exception
{
    public string MyCustomProperty { get; }
    
    public MyCustomException(string myCustomProperty)
    {
        MyCustomProperty = MyCustomProperty;
    }
}

when hovering over the property MyCustomProperty I would like to have a suggestion to generate an assertion for that property.

The generated assertion will something like this:

public static class MyCustomExceptionExtensions
{
    public static ExceptionAssertions<TException> WithMyCustomProperty<TException>(this ExceptionAssertions<TException> assertions,
        string expectedMyCustomProperty, string because = "", params object[] becauseArgs) where TException : MyCustomException
    {
        Execute.Assertion
            .ForCondition(assertions.And.MyCustomProperty.Equals(expectedMyCustomProperty))
            .BecauseOf(because, becauseArgs)
            .FailWith("Expected exception with MyCustomProperty {0}{reason}, but found {1}.", assertions.And.MyCustomProperty, expectedMyCustomProperty);
        return assertions;
    }
}

Meir017 avatar Jun 21 '18 16:06 Meir017

@jnyrup @dennisdoomen does this make sense?

Meir017 avatar Jun 21 '18 16:06 Meir017

Sure.

dennisdoomen avatar Jun 21 '18 17:06 dennisdoomen

I don't really have any gut feelings about this.

jnyrup avatar Jun 24 '18 10:06 jnyrup