chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello,
I'm trying to set up a fake IContainer. Here's what I have:

var container = Isolate.Fake.Instance<IContainer>();
var program = Isolate.Fake.Instance<IProgram>();
               
Isolate.WhenCalled(() => container.Resolve<IProgram>()).WillReturn(program);


(IProgram is an interface in my code).

When I try to run this code, I get an Autofac exception: "The requested service MyApp.IProgram has not been registered."

How could this exception be thrown though? I'm not actually calling container.Resolve<IProgram>(), right? I'm just setting it up to return a fake IProgram.

Sorry for the ultra-newbie question...
asked by spearson (600 points)

2 Answers

0 votes
Hi,

Faking Autofac directly is not supported because of an Isolator limitation. Is it possible for you to register a fake instance on Autofac and have Autofac return it instead of faking Autofac's behavior?

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Part of the benefit of using an IoC container is generally that you shouldn't need to fake it. You just register your fakes into a real container and use a real container during testing.

In your case, I'd recommend looking at something more like this:

var program = Isolate.Fake.Instance<IProgram>();
var builder = new ContainerBuilder();
builder.RegisterInstance(program).As<IProgram>();
var container = builder.Build();

Now use the container for real. When you call container.Resolve<IProgram>() it will return your fake.
answered by tillig (6.7k points)
...