2

I need to mark my substitutes in the Output window because now I have this:

Expected: not collection containing <Castle.Proxies.IFormProxy>
But was:  < <Castle.Proxies.IFormProxy>, <Castle.Proxies.IFormProxy> >

I want this:

Expected: not collection containing <Bad>
But was:  < <Good>, <Bad> >

ToString() is the obvious way but it doesn't work (How to substitute Object.ToString using NSubstitute?)

Community
  • 1
  • 1
Brains
  • 594
  • 1
  • 6
  • 18

1 Answers1

1

This is an old question but here is what I did when I had the issue:

In my interface I added the ToString()

public interface IRowDetail
{
    ...
    string ToString();
}

The substitutes are created to return on my ToString:

var fake = Substitute.For<IRowDetail>();
string message = $"{x} - {y}";

fake.ToString().Returns(message); // this works because it's IRowDetail.ToString()

return fake;

In the test I created a custom message:

Assert.AreEqual(expectedList, orderedList, "Lists differ: " + Diff(expectedList, orderedList.ToList()));

...

private string Diff(List<IRowDetail> expectedList, List<IRowDetail> orderedList)
{
    StringBuilder buffer = new StringBuilder();

    for (int i = 0; i < expectedList.Count; i++)
    {
        var o1 = ((IRowDetail)expectedList[i]).ToString(); // this is calling IRowDetail.ToString() which we configured
        var o2 = ((IRowDetail)orderedList[i]).ToString();

        if(!o1.Equals(o2))
            buffer.AppendLine($"Expected {o1} but was {o2} at index {i}.");
    }

    return buffer.ToString();
}

Here is the output:

Lists differ: Expected 1 - 55 but was 1 - 38 at index 1.
Expected 1 - 38 but was 1 - 55 at index 2.
Expected 4 - 44 but was 4 - 39 at index 10.
Expected 4 - 39 but was 4 - 44 at index 11.

Maybe this works for you too. Cheers.

Pedro Magueija
  • 131
  • 1
  • 8