I have a class (MyData
) with several properties (A
, B
, C
). The class does not have an overridden ToString
and it cannot have one for other reasons, but it has a method ToDebugString()
that is also configured as “debugger display” using.
[DebuggerDisplay(nameof(ToDebugString))]
public class MyData {
public string A {get;set;}
public string B {get;set;}
public string C {get;set;}
public string ToDebugString() => $"{A}-{B}-{C}";
}
I have an assertion for a list of such objects, like this:
var items = new MyData[] { ... };
items.Should().Contain(i => i.A == "foo");
When it fails, it shows an error message like:
Expected items
{
MyProject.MyData
{
A = "a1",
B = "b1",
C = "c1"
}
} to have an item matching (i.A == "foo"). (0.2s)
That is pretty verbose especially if you have many items in the list.
When I define a ToString
method, it is used by FluentAssertions and the output is much more condensed, but I cannot do that for the class.
My question is how could I configure FluentAssertions to use my ToDebugString
method to log the objects or is there a way to generally use the method that is configured as DebuggerDisplay
?