lundi 23 novembre 2015

Windsor use registered mock instance when open generic interface is resolved

I use Windsor in this context: http://ift.tt/1T9zyOC

The problem occures when I want to test the DomainEventDispatcher class.

These are my classes:

// Handler Interface
interface IHandler<TEvent> : where TEvent : IDomainEvent {
    void Handle(TEvent eventObj);
}

// Handler Implementation
class MyHandler: IHandler<MyEvent>
{
    void Handle(MyEvent myEvent) 
    {
        // handle
    }
}

Windsor registration in TestInitialize:

 this.container.Register(Classes.FromThisAssembly()
                              .BasedOn(typeof(IHandler<>)) 
                              .WithService.Base());

My test:

 [TestMethod]
 public void TestDispatcherCallsHandler()
 {
     var handler = new Mock<MyHandler>();
     this.container.Register(Component.For<MyHandler>()
                              .Instance(handler.Object));

     DomainEventDispatcher.Raise(new MyEvent());

     handler.Verify(h => h.Handle(It.IsAny<MyEvent>()));
 }

When container.ResolveAll<IHandler<MyEvent>>() is called inside my DomainEventDispatcher.Raise method, the container creates a new instance of TestHandler instead of using the mocked instance.

How can I make Windsor use the registered instance?

Aucun commentaire:

Enregistrer un commentaire