Windsor 和 Rhino 的自动模拟容器

发布于 2024-11-17 00:16:13 字数 543 浏览 6 评论 0原文

我想用 Windsor 进行自动模拟,这样我就可以做类似的事情

  _controller = _autoMockingContainer.Create<MyControllerWithLoadsOfDepdencies>();

There was an Windsor auto mocking container in Ayende's< /一> <一href="http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx" rel="nofollow noreferrer">Rhino 库。但这似乎不再被维护,因此依赖项有点旧(它使用的是 Castle Windsor 2,但我们需要引用 2.5),因此导致 dll hell。

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

I am want to do automocking with Windsor so that I can do something like

  _controller = _autoMockingContainer.Create<MyControllerWithLoadsOfDepdencies>();

There used to be a Windsor auto mocking container in Ayende's Rhino libraries. But that doesn't seem to be maintained any more, so the dependencies are a bit old (it's using Castle Windsor 2, but we need 2.5 to be referenced), therefore causing dll hell.

Are there any viable alternatives? I tried pulling out the relevant classes from rhino testing, but it's much more involved that I can handle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

面如桃花 2024-11-24 00:16:13

感谢 @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));
        }
    }

Thanks to @mookid8000's link and help from a colleague, I created this......which seems to do the trick.

 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));
        }
    }
℉服软 2024-11-24 00:16:13

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

通过注册一个使用 Rhino Mocks 而不是 NSubstitute 生成模拟实例的 ILazyComponentLoader,用您想要的功能扩展 Windsor 应该相当容易。

更新:我最近在我的博客上展示了 Windsor 如何使用 Rhino 模拟实现自动模拟。

Check out how Windsor can be made into an auto-mocking container using NSubstitute here.

It should be fairly easy to extend Windsor with your desired functionality by registering an ILazyComponentLoader that uses Rhino Mocks to generate mock instances instead of NSubstitute.

Update: I recently showed how Windsor can implement auto-mocking with Rhino mocks on my blog.

娇纵 2024-11-24 00:16:13

Moq Contrib 有一个 Windsor + Moq 的自动模拟容器。似乎是最新的。显然,您必须使用 Moq 而不是 Rhino.Mocks。

Moq Contrib has an automocking container for Windsor + Moq. It seems to be up to date. Obviously, you'll have to use Moq instead of Rhino.Mocks.

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