unit-testingcastle-windsorrhinorhino-commonsautomocking

Auto mocking container for Windsor and Rhino


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.


Solution

  • 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));
            }
        }