Assert.AreEqual(...) 给了我一个 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);
这两行都抛出:
测试方法 MyTesting.FooTest 抛出异常: System.FormatException:输入字符串的格式不正确。
System.Text.StringBuilder.AppendFormat(IFormatProvider 提供程序, 字符串格式,Object[] args)
System.String.Format(IFormatProvider 提供者,字符串格式,Object[] args)
MyTesting.FooTest() 中 C:\ TFS \ Scratchpad \ MyLibrary \单元测试\ FooTest.cs:第195行
奇怪的是,如果我的单元测试失败,我只会得到一个异常,当它通过时,我不会得到这个异常。不过,我并不期望出现异常,相反,它应该由于断言而失败,而不是因为单元测试本身引发了异常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在相等测试之前进行测试以确保对象计算器不为空。
在这种特殊情况下,以字符串格式出现这种错误。
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.
尝试不使用格式化字符串,看看是否仍然失败。我今天刚刚遇到同样的问题,试图对结构进行断言,这样做可以阻止我的断言抛出格式异常。格式化字符串似乎有问题。 (我正在使用 ms test)
我不喜欢构建这样的字符串,但这是我可以让测试正常运行的唯一方法。
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)
I don't like building strings like this but it was the only way I could get my test to run properly.