I need to register my Autofac container with specific interface, for this case I want to resolved mock.
How can I do it?
I've tried:
var AppContainer = ApplicationContainer.GetApplicationContainer();
var cb = new ContainerBuilder();
cb.RegisterType<Mock<IStudyLoader>>().As<IStudyLoader>().SingleInstance();
cb.Update(AppContainer);
I don't want to change my code to resolve something else than IStudyLoader
, but Mock<IStudyLoader>
is not substitutable for IStudyLoader
; e.g Mock<IStudyLoader>.Object
is substitutable for IStudyLoader
, but I cant register Mock<IStudyLoader>.Object
because it not a type.
Correct me please; I have the feeling that I am missing something.
(I have another restriction, I can't use other container than ApplicationContainer.GetApplicationContainer()
)
(I think it's better to manually inject the dependency when testing, but I don't want to change the production code this time.)
I found the solution, Yes it is possible!
var AppContainer = ApplicationContainer.GetApplicationContainer();
var studyLoaderMock = new Mock<IStudyLoader>().Object;
cb.RegisterInstance(studyLoaderMock).As<IStudyLoader>();
cb.Update(AppContainer);
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