如何在Moq中使用Unity.RegisterType? [英] How to use Unity.RegisterType with Moq?

查看:81
本文介绍了如何在Moq中使用Unity.RegisterType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个统一的运行代码.

I have a running code with unity.

现在,我想使用Moq对ASP-MVC进行单元测试. 在global.asax的代码中,我有以下内容:

Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following:

IUnityContainer container = new UnityContainer();
container.RegisterType<IFoo, Foo>(new InjectionConstructor("xxx"));

现在我用Moq编写了测试代码:

Now I wrote testcode with Moq:

IUnityContainer container = new UnityContainer();
var mockFoo = new Mock<IFoo>();

container.RegisterType<IFoo, mockFoo) >(new InjectionConstructor("xxx"));

但这不起作用.

错误: 类型'Moq.Mock'不能用作通用类型或方法中的类型参数'TTo' 'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType ... 没有从'Moq.Mock'到'IFoo'的隐式引用转换

Error: The type 'Moq.Mock' cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType... There is no implicit reference conversion from 'Moq.Mock' to 'IFoo'

推荐答案

您正在尝试注册模拟对象,而不是模拟对象.

You are trying to register the mock-object, not the mocked-object.

var mock = new Mock<IFoo>();
container.RegisterInstance<IFoo>(mock.Object);

这篇关于如何在Moq中使用Unity.RegisterType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆