4

Is there any possibility to override default messages of FluentAssertions. Sometimes I just want my custom message to be print as the result of failed test. So far I haven't found any solution for this but maybe I missed something.

Example:

myOrderedList.SequenceEqual(desiredOrderedList)
    .Should().BeTrue("Elements are not in correct order.\r\n" +
        $"Error in JSON file: {fileName}.\r\n" +
        $"Required order of elements is {string.Join(", ", desiredOrderedList)}");

Result message is:

Expected boolean to be true because Elements are not in correct order.
Error in JSON file: MyTestData.json.
Required order of elements is str1, str2, str3, but found False.

But I would like to have simply this

Elements are not in correct order.
Error in JSON file: MyTestData.json.
Required order of elements is str1, str2, str3.

EDIT: Tried this

public static class BooleanAssertionsExtensions
{
    public static BooleanAssertions Should(this bool? instance)
    {
        return new BooleanAssertions(instance);
    }
}
public class BooleanAssertions
{
    public BooleanAssertions(bool? instance)
    {
        Subject = instance;
    }

    public bool? Subject { get; private set; }

    public AndConstraint<BooleanAssertions> BeTrueCustom(string because = "", params object[] becauseArgs)
    {
        Execute.Assertion
            .ForCondition(Subject == true)
            .BecauseOf(because, becauseArgs)
            .FailWith("{reason}");

        return new AndConstraint<BooleanAssertions>(this);
    }
}

but when trying to do .Should().BeTrueCustom(... I get this error: 'BooleanAssertions' does not contain a definition for 'BeTrueCustom' and no accessible extension method 'BeTrueCustom' accepting a first argument of type 'BooleanAssertions' could be found (are you missing a using directive or an assembly reference?

I'm probably little bit slow but don't understand what am I doing wrong or how could I create new extension extending or overriding default behavior (default message). Pretty sad that FA doesn't support such fundamental thing.

Gondil
  • 787
  • 3
  • 9
  • 28
  • What about `myOrderedList.Should().BeEquivalentTo(desiredOrderedList, config => config.WithStrictOrdering())`? – Fabio Dec 09 '19 at 09:56
  • @Fabio Still not solve problem with message. I wanted to print only my custom message as other tools do. Dennis probably know that it is not possible unless I create my own implementation - he created FluentAssertions. However it is not really easy and for now I don't even know how to create my own impl. BeEquivalentTo will print `Expected collection {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: item} to be equivalent to {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: other}, but it misses {SomeProperty: 2, OtherProperty: other}.` – Gondil Dec 09 '19 at 10:11
  • @Fabio didn't noticed that usage of config. It is nice. However with using also custom message it is total mess and that's why I don't like the way FA uses these custom messages. You need to strictly follow the context of `Excepted something {because message} but found smthing other.` No ability to meaningfully add some important message because it is inserted in that FA message context. – Gondil Dec 09 '19 at 10:45

1 Answers1

0

Unless you write your own extensions, you can't.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • 1
    Hm what does it actually mean? I'm a bit confused. Don't want to make new extension, just want to override current functionality. For me sufficient would be to get rid of whole content in every .FailWith() except {reason}. Or better, add simple boolean OnlyCustomMessage=false as parameter which if will be true when only {reason} will be in FailWith() – Gondil Dec 06 '19 at 10:52
  • please check EDIT in my question, don't know what am I doing wrong – Gondil Dec 09 '19 at 09:40
  • The compiler may choose a different extension method based on overload resolution rules. Check the definition of the `Should()` method that the compiler chooses to understand what happens. – Dennis Doomen Dec 12 '19 at 06:10