什么是“升级路径”?来自断言.AssertEquals?

发布于 2024-08-05 02:32:17 字数 385 浏览 4 评论 0原文

我继承了一些单元测试代码,该代码给我一个弃用警告,因为它使用“Assertion.AssertEquals”:

警告CS0618:'NUnit.Framework.Assertion'已过时:'使用Assert类代替'

但是,我看不到我应该使用 Assert 类中明显的方法吗?

AssertEquals 采用两个对象和一条消息,可用于在出现故障时报告错误。例如,

        Assertion.AssertEquals(
             "Enqueuing first item should set count to 1",
             1, pq.Count);

Assert 类的等效方法是什么?

I've inherited some unit test code which is giving me a deprecation warning because it uses "Assertion.AssertEquals":

warning CS0618: 'NUnit.Framework.Assertion' is obsolete: 'Use Assert class instead'

However, I can't see the obvious method in the Assert class that I should be using instead?

AssertEquals takes two objects and a message that can be used to report the error if there's a failure. e.g.

        Assertion.AssertEquals(
             "Enqueuing first item should set count to 1",
             1, pq.Count);

What's the equivalent method on the Assert class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

尘曦 2024-08-12 02:32:17

答案 Jon Skeet 提出了所谓的观点“经典”模型,而 John Gietzen 的答案是指“基于约束”的模型。两者都是正确的,并且都提供了在失败的情况下传递消息的可能性。

所以让我总结一下:

“经典”模型

Assert.AreEqual(1, pq.Count,
    "Enqueuing first item should set count to 1");

“基于约束”模型

Assert.That(
    pq.Count,
    Is.EqualTo(1),
    "Enqueuing first item should set count to 1");

我更喜欢后者,因为它读起来更像一个句子。

The answer Jon Skeet presented points to the so called "Classic" model whereas John Gietzen's answer refers to the "Constraint-based" model. Both are right and both do provide the possibility to pass a message for the case of failure.

So let me conclude this:

"Classic" model

Assert.AreEqual(1, pq.Count,
    "Enqueuing first item should set count to 1");

"Constraint-based" model

Assert.That(
    pq.Count,
    Is.EqualTo(1),
    "Enqueuing first item should set count to 1");

I prefer the latter one since it reads more like a sentence.

李不 2024-08-12 02:32:17

这个怎么样:

Assert.AreEqual(1, pq.Count,
                "Enqueuing first item should set count to 1");

How about this:

Assert.AreEqual(1, pq.Count,
                "Enqueuing first item should set count to 1");
拔了角的鹿 2024-08-12 02:32:17
Assert.That(a, Is.EqualTo(b),
    "Enqueuing first item should set count to 1");
Assert.That(a, Is.EqualTo(b),
    "Enqueuing first item should set count to 1");
酷到爆炸 2024-08-12 02:32:17

执行完整的正则表达式替换:

Assertion\.AssertEquals(\(.*\),\(.*\),\(.*\))

应替换为:

Assert.That(\2, \1, \0)

并且 Assertion.Assert(\(.*\),\(.*\))

应替换为:

Assert.That(\2, \1)

Do a full regex replace:

Assertion\.AssertEquals(\(.*\),\(.*\),\(.*\))

should be replaced with:

Assert.That(\2, \1, \0)

And Assertion.Assert(\(.*\),\(.*\))

should be replaced with:

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