michael
michael

Reputation: 15302

Assert.AreEqual(...) gives me an System.FormatException

Assert.AreEqual(expected, actual, "The value returned for {0}'s Foo method should be 'Bar'.",
            typeof(Calculator));

Assert.AreEqual(expected, actual, "The value returned for {0}'s Foo method should be 'Bar'.",
            typeof(Calculator).Name);

Both of these lines throw a:

Test method MyTesting.FooTest threw exception: System.FormatException: Input string was not in a correct format.


System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
System.String.Format(IFormatProvider provider, String format, Object[] args)
MyTesting.FooTest() in C:\TFS\Scratchpad\MyLibrary\Unit Testing\FooTest.cs: line 195

The strange thing is I only get an exception if my Unit Test fails, when it passes I don't get this exception. I'm not expecting an exception though, instead it should have Failed due to the assertion, not because the unit test itself threw an exception.

Upvotes: 0

Views: 876

Answers (2)

Kyle
Kyle

Reputation: 3300

Try to not use a formatted string and see if it still fails. I just ran into this same issue today trying to do an assert on structs, and doing this stopped my assert from throwing a format exception. It seems to have problems with formatted strings. (I'm using ms test)

Assert.AreEqual(expected, actual,"The value returned for " + typeof (Calculator) + "'s Foo method should be 'Bar'.");
Assert.AreEqual(expected, actual, "The value returned for " + typeof(Calculator) + "'s Foo method should be 'Bar'.");

I don't like building strings like this but it was the only way I could get my test to run properly.

Upvotes: 0

Mike Verrier
Mike Verrier

Reputation: 484

Make a test to be sure that your object Calculator is not null before your equality test.

This kind of error will occur in this particular case in a string format.

Upvotes: 1

Related Questions