4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dan
  • 29,100
  • 43
  • 148
  • 207

3 Answers3

6

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));
        }
    }
Dan
  • 29,100
  • 43
  • 148
  • 207
5

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.

mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • So the only way to do it is to use a different mocking framework? – Dan Jun 29 '11 at 10:57
  • 1
    No, I think you must have misunderstood me - you are using Windsor, right? The example shows how any mocking framework could be used easily with Windsor to allow for automatically injecting mocks. – mookid8000 Jun 29 '11 at 12:12
  • 1
    Fantastic answer. The guy who wrote the blog post didn't feel it was worth the time but it is fantastic even though it is so simple. Thanks for sharing. – Bronumski Dec 05 '11 at 16:19
1

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.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • So the only way to do it is to use a different mocking framework? – Dan Jun 29 '11 at 10:59
  • @Dan: not at all, it's just that Moq Contrib is maintained, unlike Rhino's automocking container. You could also get Rhino's code and update it. – Mauricio Scheffer Jun 29 '11 at 12:33
  • Yeah I did try to upgrade Castle in Rihno.Testing, it was more involved that I thought. – Dan Jun 29 '11 at 15:30