混合 NUnit 和 NMock2 匹配器时出现不明确的引用
我们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以避免使用 Nunit 流利语法。
然后结合使用别名。
这将强制您的应用程序采用这些类的 NMock 版本。如果您想访问 NUnit,则必须提供包含名称空间的全名。
保留两者的另一个选择是使用以下命名空间声明和别名。
现在你可以使用两个流畅的接口,但是代码如下
You can avoid using the Nunit fluent syntax.
And then use aliases in conjunction.
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.
Now you can use both fluent interfaces, but the code reads
有趣的是,NUnit 还公开了一个名为 Iz 的小类,您可以使用它来避免与 NMock2 的命名冲突。因此,例如,
您可以写“
可能不是最干净的,但它确实有效”;)
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:
you can write
May not be the cleanest, but it doez work ;)
如果需要使用约束,请使用命名空间别名。
例如。
然后
would become
Use Namespace aliases if you need to use constraints.
Eg.
Then
would become