在MVC项目中测试UnityControllerFactory时模拟IUnityContainer的问题

发布于 2024-10-04 03:50:53 字数 1513 浏览 2 评论 0原文

我在测试统一控制器工厂时遇到问题。我正在清理统一容器并断言在其上调用了一个方法。虽然我得到了无效的强制转换期望,但无法真正看出我的代码有什么问题。

这是正在测试的类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-10-11 03:50:53

通用 Resolve(string) 方法是扩展方法,无法模拟。您在这里有两种选择:

  1. 使用非泛型 Resolve(Type, string) 方法或
  2. 用您自己的接口包装 IUnityContainer ,以便您要使用的泛型方法是接口方法,而不是扩展方法。

The generic Resolve<T>(string) method is an extension method and cannot be mocked. You have two choices here:

  1. Use a non-generic Resolve(Type, string) method or
  2. Wrap IUnityContainer with your own interface so that the generic methods you want to use are interface methods, not extension methods.
凝望流年 2024-10-11 03:50:53

为什么不直接使用 IUnityContainer 的实际实现并在其中注册模拟/存根呢?

Why don't you just use a IUnityContainer's real implementation and register mock/stubs inside it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文