Search code examples
c#moqioc-container

How to mock container.Resolve<Type>()


I have something like this:

public class HomeController
{
   public ActionResult Index()
   {
      var x = Container.Resolve<IOrganisationService>();
   }
}

When unit testing, I get a null reference exception when the container tries to resolve.

Anybody knows how to mock Container.Resolve<Type>() ?


Solution

  • I did it, it's like this:

    //code inside the setup method
     var c = new Moq.Mock<IWindsorContainer>();
     var l = new Moq.Mock<ILookupService>();
     l.Setup(o => o.GetItems(It.IsAny<String>())).Returns(new List<LookupItem>());
     c.Setup(o => o.Resolve<ILookupService>()).Returns(l.Object);
     LocatorConfigurator.SetContainer(c.Object);