Moq and accessing called parameters

Posted by lozzar on Stack Overflow See other posts from Stack Overflow or by lozzar
Published on 2010-01-14T11:32:34Z Indexed on 2010/04/13 8:12 UTC
Read the original article Hit count: 523

I've just started to implement unit tests (using xUnit and Moq) on an already established project of mine. The project extensively uses dependency injection via the unity container.

I have two services A and B. Service A is the one being tested in this case. Service A calls B and gives it a delegate to an internal function. This 'callback' is used to notify A when a message has been received that it must handle.

Hence A calls (where b is an instance of service B):

b.RegisterHandler(Guid id, Action<byte[]> messageHandler);

In order to test service A, I need to be able to call messageHandler, as this is the only way it currently accepts messages.

Can this be done using Moq? ie. Can I mock service B, such that when RegisterHandler is called, the value of messageHandler is passed out to my test?

Or do I need to redesign this? Are there any design patterns I should be using in this case? Does anyone know of any good resources on this kind of design?

© Stack Overflow or respective owner

Related posts about oo

Related posts about moq