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.
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));
}
}
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.
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.
精彩评论