Autofac.ILifetimeScope: Not able to Mock or resolve my service class

1.3k Views Asked by At

I have created a test project for service layer in my application and everything is working fine, but I need to Mock one of my service class which exists in some other folder or namespace but the same solution. So I tried like following to Mock the service class (IEmailService), but while calling the 'ResolveServiceInstance' function, it goes to 'TearDown'(attribute) method to dispose the container/object of Autofac.ILifetimeScope. I don't know where I did go wrong, please help me with this.

My 'SetUp' Method for Mock the service class,

public ILifetimeScope Container { get; private set; }

[SetUp]
public void SetUpTest()
{
    MockPermissionService = new Mock<IPermissionService>();
    MockPermissionServiceProvider = new Mock<IPermissionServiceProvider>();
    MockPlatformEmailService = new Mock<Platform.Services.IEmailService>();
    Container = ServiceTestsSetUp.GlobalContainer.BeginLifetimeScope(builder =>
    {
        builder.Register(c => MockPermissionService.Object).As<IPermissionService>();
        builder.Register(c => MockPermissionServiceProvider.Object).As<IPermissionServiceProvider>();
        PreServiceResolve(builder);
    });

    Service = ResolveServiceInstance();
    AddStaticData();
} 

My ResolveServiceInstance function,

private TService ResolveServiceInstance()
{
   return Container.Resolve<TService>();
}

When it returns the container.Resolve method it shows the below error message, so I am not able to register or mock the other namespace class. What I did wrong here?

 DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'InEight.Platform.Services.EmailService' can be invoked with the available services and parameters:
 Cannot resolve parameter 'InEight.Platform.TenantInfo tenant' of constructor 'Void .ctor(InEight.Platform.TenantInfo, InEight.Platform.Site.Services.Contexts.ICoreDataContext, InEight.Platform.Logs.IAppLog)'.    
0

There are 0 best solutions below