温莎和犀牛的自动模拟容器

问题描述 投票:4回答:3

我想和温莎一起做自动锁定,以便我可以做类似的事情

  _controller = _autoMockingContainer.Create<MyControllerWithLoadsOfDepdencies>();

Ayende's Rhino图书馆曾经有一个温莎自动模拟容器。但这似乎不再维持,所以依赖关系有点旧(它使用Castle Windsor 2,但我们需要引用2.5),因此导致dll地狱。

有没有可行的替代方案?我尝试从rhino测试中提取出相关的类,但是我可以处理的更多内容。

unit-testing castle-windsor rhino rhino-commons automocking
3个回答
6
投票

感谢@ mookid8000的链接以及同事的帮助,我创造了这个......这似乎就是诀窍。

 public abstract class TestBase
    {
        static readonly WindsorContainer _mockWindsorContainer;

        static TestBase()
        {
            _mockWindsorContainer = new WindsorContainer();
            _mockWindsorContainer.Register(Component.For<LazyComponentAutoMocker>());
        }

        protected static T MockOf<T>() where T : class
        {
            return _mockWindsorContainer.Resolve<T>();
        }

        protected static T Create<T>()
        {
            _mockWindsorContainer.Register(Component.For<T>());
            return _mockWindsorContainer.Resolve<T>();
        }

    }

    public class LazyComponentAutoMocker : ILazyComponentLoader
    {
        public IRegistration Load(string key, Type service, IDictionary arguments)
        {
            return Component.For(service).Instance(MockRepository.GenerateStub(service));
        }
    }

5
投票

了解如何使用NSubstitute here将Windsor制成自动模拟容器。

通过注册使用Rhino Mocks来生成模拟实例而非NSubstitute的ILazyComponentLoader,可以相当容易地扩展Windsor所需的功能。

更新:我最近展示了Windsor如何使用Rhino mocks on my blog实现自动模拟。


1
投票

Moq Contrib有一个用于Windsor + Moq的自动锁定容器。它似乎是最新的。显然,你必须使用Moq而不是Rhino.Mocks。

© www.soinside.com 2019 - 2024. All rights reserved.