在MVC项目中测试UnityControllerFactory时模拟IUnityContainer的问题
我在测试统一控制器工厂时遇到问题。我正在清理统一容器并断言在其上调用了一个方法。虽然我得到了无效的强制转换期望,但无法真正看出我的代码有什么问题。
这是正在测试的类:
public class UnityControllerFactory : IControllerFactory
{
private readonly IUnityContainer _container;
public UnityControllerFactory(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
_container = container;
}
public IController CreateController(RequestContext requestContext, string controllerName)
{
return _container.Resolve<IController>(controllerName);
}
public void ReleaseController(IController controller)
{
_container.Teardown(controller);
}
}
这是测试:
[Test]
public void CreateControllerCallsResolveOnContainerWithCorrectArgument()
{
const string controllerName = "WhateverController";
var containerMock = MockRepository.GenerateMock<IUnityContainer>();
var controllerFactory = new UnityControllerFactory(containerMock);
controllerFactory.CreateController(null, controllerName);
containerMock.AssertWasCalled(x => x.Resolve<IController>(controllerName));
}
这是错误消息:
System.InvalidCastException :无法将类型为“ObjectProxy42336d85f7bb400f9590892348d1d597”的对象转换为类型“System.Web.Mvc.IController”
I am having trouble testing a unity controller factory. I am mcoking out the unity container and asserting that a method was called on it. I am getting invalid cast expection though but cannot really see what is wrong with my code.
Here is the class under test:
public class UnityControllerFactory : IControllerFactory
{
private readonly IUnityContainer _container;
public UnityControllerFactory(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
_container = container;
}
public IController CreateController(RequestContext requestContext, string controllerName)
{
return _container.Resolve<IController>(controllerName);
}
public void ReleaseController(IController controller)
{
_container.Teardown(controller);
}
}
and here is the test:
[Test]
public void CreateControllerCallsResolveOnContainerWithCorrectArgument()
{
const string controllerName = "WhateverController";
var containerMock = MockRepository.GenerateMock<IUnityContainer>();
var controllerFactory = new UnityControllerFactory(containerMock);
controllerFactory.CreateController(null, controllerName);
containerMock.AssertWasCalled(x => x.Resolve<IController>(controllerName));
}
and here is the error message:
System.InvalidCastException : Unable to cast object of type 'ObjectProxy42336d85f7bb400f9590892348d1d597' to type 'System.Web.Mvc.IController'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通用
Resolve(string)
方法是扩展方法,无法模拟。您在这里有两种选择:Resolve(Type, string)
方法或IUnityContainer
,以便您要使用的泛型方法是接口方法,而不是扩展方法。The generic
Resolve<T>(string)
method is an extension method and cannot be mocked. You have two choices here:Resolve(Type, string)
method orIUnityContainer
with your own interface so that the generic methods you want to use are interface methods, not extension methods.为什么不直接使用 IUnityContainer 的实际实现并在其中注册模拟/存根呢?
Why don't you just use a
IUnityContainer
's real implementation and register mock/stubs inside it?