Assert.AreEqual(...) 给了我一个 System.FormatException

发布于 2024-12-08 19:57:44 字数 762 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-12-15 19:57:44

在相等测试之前进行测试以确保对象计算器不为空。

在这种特殊情况下,以字符串格式出现这种错误。

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.

捂风挽笑 2024-12-15 19:57:44

尝试不使用格式化字符串,看看是否仍然失败。我今天刚刚遇到同样的问题,试图对结构进行断言,这样做可以阻止我的断言抛出格式异常。格式化字符串似乎有问题。 (我正在使用 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'.");

我不喜欢构建这样的字符串,但这是我可以让测试正常运行的唯一方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文