lundi 27 avril 2015

Can I configure Unity to automatically return a fake for interfaces that are not registered?

I'm a bit tired of manually registering fake dependencies in my unit tests... I'm wondering if there is a way to configure Unity in such a way that, if there is no registered implementation for a given type, it automatically creates a fake using FakeItEasy or some other mocking framework.

So instead of writing this:

var container = new UnityContainer();

var fooProvider = A.Fake<IFooProvider>();
container.RegisterInstance(fooProvider);
var barService = A.Fake<IBarService>();
container.RegisterInstance(barService);
var bazManager = A.Fake<IBazManager>();
container.RegisterInstance(bazManager);

var sut = container.Resolve<SystemUnderTest>();

I could just write something like this:

var container = new UnityContainer();
container.AutoFake();
var sut = container.Resolve<SystemUnderTest>();

Is it possible? I was looking for some kind of callback that would be called when Unity tries to resolve a type, but I couldn't find anything. There seems to be plenty of extensibility points, with extensions, strategies, policies, call handlers and so forth, but I don't have a clue where to start...

Aucun commentaire:

Enregistrer un commentaire