Autofac.ILifetimeScope:无法模拟或解析我的服务类

时间:2018-08-15 16:14:51

标签: c# unit-testing mocking autofac teardown

我已经在我的应用程序中为服务层创建了一个测试项目,并且一切正常,但是我需要模拟一个服务类,该服务类存在于其他文件夹或名称空间中,但解决方案相同。因此,我尝试按照以下步骤模拟服务类(IEmailService),但是在调用“ ResolveServiceInstance”函数时,它会转到“ TearDown”(属性)方法来处理Autofac.ILifetimeScope的容器/对象。我不知道我哪里做错了,请帮我解决这个问题。

我的用于模拟服务类的“设置”方法

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();
} 

我的ResolveServiceInstance函数,

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

当它返回container.Resolve方法时,它显示以下错误消息,因此我无法注册或模拟其他名称空间类。我在这里做错了什么?

 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 个答案:

没有答案