混合 NUnit 和 NMock2 匹配器时出现不明确的引用

发布于 2024-10-24 22:32:01 字数 619 浏览 2 评论 0原文

我们使用 NUnit (2.5.9) 和 NMock2 进行单元测试和模拟。然而,两者都有紧密对应的匹配器语法。当我

using NUnit.Framework;
using NMock2;

这样做时,稍后在以下 NMock2 代码中:

Expect.Once.On(database).Method("Create").
  With(Has.Property("id", Is.EqualTo("012345678901")));

还有一个 NUnit 断言:

Assert.That(someValue, Is.EqualTo(54321));

然后 VS (2008) 会抱怨 'Is' 是 'NUnit.Framework.Is' 和 'NMock2.Is' 之间的不明确引用< /em> (与“有”相同)。

有什么办法解决这个问题吗?无论如何,这两个匹配器似乎都有相似的功能。当然,使用完整的命名空间为每个匹配器类添加前缀是可行的,但它使测试的可读性明显降低。

谷歌搜索这个问题根本没有找到任何匹配,所以我的内心感觉是我正在做一些非常愚蠢的事情。

We're using NUnit (2.5.9) and NMock2 for unit testing and mocking. Both, however, have a matcher syntax that closely corresponds. When I do

using NUnit.Framework;
using NMock2;

And later on the following NMock2 code:

Expect.Once.On(database).Method("Create").
  With(Has.Property("id", Is.EqualTo("012345678901")));

But also an NUnit assertion:

Assert.That(someValue, Is.EqualTo(54321));

Then VS (2008) will complain that 'Is' is an ambiguous reference between 'NUnit.Framework.Is' and 'NMock2.Is' (and same for 'Has').

Is there any way around this? It seems that both matchers have similar functionality anyway. Prefixing each matcher class with the full namespace of course works, but it makes tests significantly less readable.

Google searches for this issue have found no match at all, so my underbelly feeling is that i'm doing something very stupid.

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

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

发布评论

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

评论(3

眼角的笑意。 2024-10-31 22:32:01

您可以避免使用 Nunit 流利语法。

Assert.AreEqual(54321, someValue);

然后结合使用别名。

using NMock2;
using Is = NMock2.Is;
using Has = NMock2.Has;

这将强制您的应用程序采用这些类的 NMock 版本。如果您想访问 NUnit,则必须提供包含名称空间的全名。

保留两者的另一个选择是使用以下命名空间声明和别名。

using NUnit.Framework;
using Is = NUnit.Framework.Is;
using NMock2;
using WithValue = NMock2.Is;
using Has = NMock2.Has;

现在你可以使用两个流畅的接口,但是代码如下

Expect.Once.On(database).Method("Create").
  With(Has.Property("id", WithValue.EqualTo("012345678901")));

Assert.That(someValue, Is.EqualTo(54321));

You can avoid using the Nunit fluent syntax.

Assert.AreEqual(54321, someValue);

And then use aliases in conjunction.

using NMock2;
using Is = NMock2.Is;
using Has = NMock2.Has;

This will force your application to take the NMock versions of these classes. If you wanted to get to the NUnit ones, you would have to give a full name including namespace.

Another option to preserve both would be to use the following namespace declarations and aliases.

using NUnit.Framework;
using Is = NUnit.Framework.Is;
using NMock2;
using WithValue = NMock2.Is;
using Has = NMock2.Has;

Now you can use both fluent interfaces, but the code reads

Expect.Once.On(database).Method("Create").
  With(Has.Property("id", WithValue.EqualTo("012345678901")));

Assert.That(someValue, Is.EqualTo(54321));
怎樣才叫好 2024-10-31 22:32:01

有趣的是,NUnit 还公开了一个名为 Iz 的小类,您可以使用它来避免与 NMock2 的命名冲突。因此,例如,

Assert.That(someValue, Is.EqualTo(54321))

您可以写“

Assert.That(someValue, Iz.EqualTo(54321))

可能不是最干净的,但它确实有效”;)

Interestingly enough NUnit also exposes a little class called Iz that you can use to avoid this naming collision with NMock2. So for example instead of:

Assert.That(someValue, Is.EqualTo(54321))

you can write

Assert.That(someValue, Iz.EqualTo(54321))

May not be the cleanest, but it doez work ;)

缘字诀 2024-10-31 22:32:01

如果需要使用约束,请使用命名空间别名。
例如。

    using N = NUnit.Framework;
    using M = NMock2;

然后

Assert.That(someValue, Is.EqualTo(54321))

would become

Assert.That(someValue, N.Is.EqualTo(54321))

Use Namespace aliases if you need to use constraints.
Eg.

    using N = NUnit.Framework;
    using M = NMock2;

Then

Assert.That(someValue, Is.EqualTo(54321))

would become

Assert.That(someValue, N.Is.EqualTo(54321))

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