vendredi 29 janvier 2016

Register a Moq mock object as type for a Unity container

Since my sut class has a constructor with a IUnityContainer as parameter I like to setup a new Unity container im my unit test and pass it to the sut. I like to use Moq here because I also have to veryfy for certain method calls. What I've tried here is

public interface IFoo
    {
        void Bar();
    }

    class Program
    {
        static void Main(string[] args)
        {
            var fooMock = new Mock<IFoo>();

            fooMock.Setup(x => x.Bar()).Callback(() => Console.WriteLine("Hello"));

            IUnityContainer container = new UnityContainer();
            container.RegisterType(typeof (IFoo), fooMock.Object.GetType());

            var sut = container.Resolve<IFoo>();
            sut.Bar();

            Console.Read();
        }
    }

But this results in an ResulutionFailedException. Any ideas what I can do or what might be an alternative to this problem?

Aucun commentaire:

Enregistrer un commentaire