Moq 和 NUnit 的运行时通用约束错误
我正在使用 NUnit 和 Moq 来测试一个使用通用 WCF 服务客户端包装器的类 我写了,我遇到了一个我无法弄清楚的错误。我有以下接口:
public interface IService
{
void Call();
}
...由以下服务客户端实现:
public class ServiceClient : ClientBase<IService>, IService
{
public void Call()
{
}
}
...并由此类包装,并具有以下通用约束:
public class ServiceClientWrapper<TClient, TService>
where TClient : ClientBase<TService>, TService
where TService : class
{
public virtual TService CreateServiceClient()
{
return (TService)Activator.CreateInstance<TClient>();
}
}
为了使其可测试,我有一个可以模拟的包装器工厂。包装工厂接口是这样的:
public interface IServiceClientWrapperFactory
{
ServiceClientWrapper<TClient, TService>
CreateServiceClientWrapper<TClient, TService>()
where TClient : ClientBase<TService>, TService
where TService : class;
}
我使用以下代码测试此设置:
// A mock IService to return from my mock service wrapper:
var mockService = new Mock<IService>();
// A mock client wrapper to return from my mock wrapper factory:
var mockClientWrapper =
new Mock<ServiceClientWrapper<ServiceClient, IService>>();
mockClientWrapper
.Setup(mcw => mcw.CreateServiceClient())
.Returns(mockService.Object);
// A mock wrapper factory to inject into a client object:
var mockClientWrapperFactory = new Mock<IServiceClientWrapperFactory>();
mockClientWrapperFactory
.Setup(mcwf => mcwf.CreateServiceClientWrapper<ServiceClient, IService>())
.Returns(mockClientWrapper.Object);
// Get the mock client wrapper from the mock wrapper factory - boom!
mockClientWrapperFactory.Object
.CreateServiceClientWrapper<ServiceClient, IService>();
错误是:
GenericArguments[0], 'TService', 上 'ServiceClientWrapper`2[TClient,TService]' 违反类型约束 参数“TClient”。
无论我在哪里声明它们,约束都是相同的,它编译和运行得很好,如果我实现 IServiceClientWrapperFactory 并在没有起订量的情况下运行它,则不会抛出错误......有什么想法吗?
I'm using NUnit and Moq to test a class which uses a generic WCF service client wrapper I wrote, and I've run into an error I can't figure out. I have the following interface:
public interface IService
{
void Call();
}
...implemented by the following service client:
public class ServiceClient : ClientBase<IService>, IService
{
public void Call()
{
}
}
...and wrapped by this class with the following generic constraints:
public class ServiceClientWrapper<TClient, TService>
where TClient : ClientBase<TService>, TService
where TService : class
{
public virtual TService CreateServiceClient()
{
return (TService)Activator.CreateInstance<TClient>();
}
}
To make it testable I have a wrapper factory which I can mock. The wrapper factory interface is this:
public interface IServiceClientWrapperFactory
{
ServiceClientWrapper<TClient, TService>
CreateServiceClientWrapper<TClient, TService>()
where TClient : ClientBase<TService>, TService
where TService : class;
}
I test this set up using this code:
// A mock IService to return from my mock service wrapper:
var mockService = new Mock<IService>();
// A mock client wrapper to return from my mock wrapper factory:
var mockClientWrapper =
new Mock<ServiceClientWrapper<ServiceClient, IService>>();
mockClientWrapper
.Setup(mcw => mcw.CreateServiceClient())
.Returns(mockService.Object);
// A mock wrapper factory to inject into a client object:
var mockClientWrapperFactory = new Mock<IServiceClientWrapperFactory>();
mockClientWrapperFactory
.Setup(mcwf => mcwf.CreateServiceClientWrapper<ServiceClient, IService>())
.Returns(mockClientWrapper.Object);
// Get the mock client wrapper from the mock wrapper factory - boom!
mockClientWrapperFactory.Object
.CreateServiceClientWrapper<ServiceClient, IService>();
The error is:
GenericArguments[0], 'TService', on
'ServiceClientWrapper`2[TClient,TService]'
violates the constraint of type
parameter 'TClient'.
The constraints are the same wherever I've stated them, it compiles and runs just fine, the error isn't thrown if I implement IServiceClientWrapperFactory
and run it without Moq... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我稍微玩了一下代码,得到了相同的结果。据我所知,您的定义没有任何问题。
在实现具有各种通用组合的模拟时,这似乎可能是 Moq 中的一个错误对他们的约束。
我尝试删除各种约束(并根据需要注释掉或修改代码)。看来如果我从
where TClient : ... , TService
删除TService
,那么我就不会收到此错误。过去存在多个类似的错误似乎进一步证实了这一点:
仅供参考,我用来重现您问题的版本是:Moq.4.0.10827,NET40 (不是 NET40-RequiresCastle)
I played with the code a bit, and I get the same result. As far as I can tell, there isn't anything wrong with your definitions.
It seems that it might be a bug in Moq, when implementing mocks that have various combinations of generic constraints on them.
I tried removing various constraints (and commenting out or modifying code, as required). It seems if I remove
TService
fromwhere TClient : ... , TService
, then I don't get this error.This seems further corroborated by the fact that there have been multiple similar bugs in the past:
FYI, the version I used to repro your problem was: Moq.4.0.10827, NET40 (not NET40-RequiresCastle)