0

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)'.    
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Md Aslam
  • 1,228
  • 8
  • 27
  • 61
  • Unfortunately it looks like you need to provide a bit more information. Please update the question (not comments!) with info like... What is `TService` when you call `ResolveServiceInstance`? Why are you mocking the `IEmailService` but not registering it into the lifetime scope? When do you dispose of the lifetime scope? Is `AddStaticData()` anything to do with `static` fields that might interfere? What is `PreServiceResolve()`? Is there anything you can _remove_ from this to simplify the repro? Things that don't have to do with the error you're seeing? – Travis Illig Aug 15 '18 at 20:27
  • Sorry Travis, I am not able to edit my question, but I can explain those thing here. TService is nothing, We are passing our service class object as generic. public virtual new TService Service { ( get { return (TService)base.Service; }). }. And then static data used to mocking the model object. And PreServiceResolve is (protected internal Action PreServiceResolve = (scope) => { };) – Md Aslam Aug 16 '18 at 12:54
  • `TService` has to be something. When this calls `Container.Resolve` it's super important that you know what type `TService` is at that time - that's the thing that's throwing the exception. It appears it is `InEight.Platform.Services.EmailService` and that is not being registered in the container - hence my additional question, why is it mocking the object but not registering it? – Travis Illig Aug 16 '18 at 14:18
  • @Travis. I am new to unit testing part, So I don'nt know how to register the EmailService class before mocking that. I thought , I have registered. Can you help me to register the class with some sample ? – Md Aslam Aug 17 '18 at 06:32
  • You have registration examples right there in your code. You're registering the mock permission service. But I don't know if registering the email service will fix it because there isn't enough info here, as I've mentioned. I can't help you more without the answers I've requested, sorry. – Travis Illig Aug 17 '18 at 12:12

0 Answers0