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?
You already have an instance of object IFoo fooMock
, so you need to register using RegisterInstance method:
container.RegisterInstance<IFoo>(fooMock.Object);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With