首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要模拟的AutoMock CreateInstance类型必须是接口、委托或非密封的非静态类

要模拟的AutoMock CreateInstance类型必须是接口、委托或非密封的非静态类
EN

Stack Overflow用户
提问于 2020-12-01 04:56:23
回答 1查看 1.3K关注 0票数 0

当我用Moq.AutoMock.Automocker模拟"autoMocker.CreateInstance();“时,我得到了下面的通用消息。HeliumController类的构造函数有几个接口。我如何才能找到哪个特定的接口或构造函数无法模拟?

代码语言:javascript
复制
{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NotSupportedException: Type to mock must be an interface, a delegate, or a non-sealed, non-static class.
   at Moq.Guard.IsMockable(Type type)
   at Moq.Mock`1..ctor(MockBehavior behavior, Object[] args)
   at Moq.Mock`1..ctor(MockBehavior behavior)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Moq.AutoMock.Resolvers.MockResolver.Resolve(MockResolutionContext context)
   at Moq.AutoMock.AutoMocker.Resolve(Type serviceType, Object initialValue)
   at Moq.AutoMock.AutoMocker.Resolve(Type serviceType)
   at Moq.AutoMock.AutoMocker.Get(Type serviceType)
   at Moq.AutoMock.AutoMocker.<CreateArguments>b__58_0(ParameterInfo x)
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Moq.AutoMock.AutoMocker.CreateArguments(Type type, BindingFlags bindingFlags)
   at Moq.AutoMock.AutoMocker.CreateInstance(Type type, Boolean enablePrivate)
   at Moq.AutoMock.AutoMocker.CreateInstance[T](Boolean enablePrivate)
   at Moq.AutoMock.AutoMocker.CreateInstance[T]()
   at Helium.Api.Tests.API.v2.HeliumControllerSearchTests..ctor()
EN

回答 1

Stack Overflow用户

发布于 2020-12-01 23:18:57

不幸的是,这将是一些试验和错误,检查构造函数采用的类型,然后查看是否有任何类型不符合要求-我会首先开始并查看您正在使用的密封类,因为这可能是原因。接口、非密封的、非静态的和委托--这些都可以被模仿。

我确实注意到异常中没有任何行号。我猜可能是在“发布”模式下进行测试,因此符号不可用。通常,"Debug“模式下的异常堆栈具有发生异常的实际行号--在"Debug”模式下编译和运行测试以查看实际行可能会有所帮助。这可能会缩小范围。

Here's where the not-mockable exception is thrown in Moq.看起来他们可以很容易地将实际的类型名称添加到异常中,但这并没有发生。如果我是你,I'd file an issue with the Moq folks并要求他们更新以在异常中包含类型名称。如果他们对此表示同意,那么提交给他们并使产品变得更好,同时解决您的问题似乎是一个非常容易的拉动请求。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65081070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档