I just wrote a small Unit Test in which I used a StringBuilder().
var stringBuilder = new StringBuilder();
stringBuilder.Append("Foo");
Assert.AreEqual(stringBuilder, "Foo");
This Test will fail.
Expected: <Foo>
But was: "Foo"
But if I change the Assert to
Assert.AreEqual(stringBuilder.ToString(), "Foo");
the test will pass.
So, what is the difference between the implicit call and the explicit call of the ToString() method? Or/And what are these Brackets (<>) standing for?
In your first example, you are testing if your
StringBuilderinstance is equal to the string, which will fail.In your second one, you are testing if the result of the call to
ToString()(which is a string) is equal to the other string.The bracktes (<>) are NUnits way to indicate that it got an non-string object, but to display the message, NUnit calls
ToString()on that object.So
<Foo>is an object that returnsFooon a call toToString(), whereas"Foo"is just aStringFoo.MSTest would show you a different message, which would be more clear: