ABP 框架测试中的“无法解析参数”错误

Posted

技术标签:

【中文标题】ABP 框架测试中的“无法解析参数”错误【英文标题】:"Cannot resolve parameter" error in ABP Framework Test 【发布时间】:2021-02-25 14:47:01 【问题描述】:

我正在尝试编写测试代码:

public class SupplierAppServiceTests : ABCApplicationTestBase

    private readonly ISupplierAppService _supplierAppService;
    private readonly IWmsCompanyRepository _fakeRepo;

    public SupplierAppServiceTests()
    
        _fakeRepo = Substitute.For<IWmsCompanyRepository>();
        _supplierAppService = GetRequiredService<ISupplierAppService>();
    

    [Fact]
    public async Task Should_Be_Generate_Code()
    
        // Arrange
        _fakeRepo.CreateAsync(Arg.Any<WmsCompany>())
            .Returns(Task.CompletedTask);
        ...
    

但我收到此错误:

Autofac.Core.DependencyResolutionException : An exception was thrown while activating MyCompany.ABC.Application.CompanySuppliers.SupplierAppService.
    ---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyCompany.ABC.Application.CompanySuppliers.SupplierAppService' can be invoked with the available services and parameters:
    Cannot resolve parameter 'MyCompany.ABC.Domain.Wms.Companies.IWmsCompanyRepository wmsCompanyRepository' of constructor 'Void .ctor(MyCompany.ABC.Domain.Companies.SupplierManager, MyCompany.ABC.Domain.Companies.ISupplierRepository, MyCompany.ABC.Domain.Wms.Companies.IWmsCompanyRepository)'.

实现IWmsCompanyRepositoryWmsCompanyRepository类(最后一个参数)向第三方的网络服务发出请求。

SupplierAppService:

public class SupplierAppService : ABCAppService, ISupplierAppService

    public SupplierAppService(SupplierManager supplierManager, ISupplierRepository supplierRepository, IWmsCompanyRepository wmsCompanyRepository)
    
        this.supplierManager = supplierManager;
        this.supplierRepository = supplierRepository;
        this.WmsCompanyRepository = wmsCompanyRepository;
    

    ...

IWmsCompanyRepository:

public interface IWmsCompanyRepository

     Task CreateAsync(WmsCompany company);

我在SupplierAppServiceTests 类的构造函数中更改了_supplierAppService 赋值

来自:

_supplierAppService = GetRequiredService<ISupplierAppService>();

到:

_supplierAppService = new SupplierAppService(_supplierManager, _supplierRepository, _fakeRepo);

SupplierAppService 的方法使用了ObjectMapper 属性,这次我收到了这个错误:

ObjectMapper -> System.ArgumentNullException : Value cannot be null. (Parameter 'provider') objectmapper

如何正确地将IWmsCompanyRepository 注入到SupplierAppService 以用于测试用例?

【问题讨论】:

【参考方案1】:

将您的 _fakeRepo 单例添加到 services 以便可以为 SupplierAppService 解析。

public SupplierAppServiceTests()

    // _fakeRepo = Substitute.For<IWmsCompanyRepository>();
    _supplierAppService = GetRequiredService<ISupplierAppService>();


protected override void AfterAddApplication(IServiceCollection services)

    _fakeRepo = Substitute.For<IWmsCompanyRepository>();
    services.AddSingleton(_fakeRepo);

【讨论】:

【参考方案2】:

我找到了解决方法, 我更改了 SupplierAppService 类:

将默认值IWmsCompanyRepository wmsCompanyRepository = null添加到ctor参数 将 WmsCompanyRepository 从字段更改为公共属性 将 WmsCompanyRepository 属性添加到 ISupplierAppService
public class SupplierAppService : ABCAppService , ISupplierAppService

   public IWmsCompanyRepository WmsCompanyRepository  get; set; 
   public SupplierAppService(SupplierManager supplierManager, ISupplierRepository supplierRepository,IWmsCompanyRepository wmsCompanyRepository = null)
   
      this.supplierManager = supplierManager;
      this.supplierRepository = supplierRepository;
      this.WmsCompanyRepository = wmsCompanyRepository;
   
 ...
 

并更改了 SupplierAppServiceTests ctor:

public SupplierAppServiceTests()

    _fakeRepo = Substitute.For<IWmsCompanyRepository>();
    _supplierAppService = GetRequiredService<ISupplierAppService>();
    _supplierAppService.WmsCompanyRepository = _fakeRepo;

【讨论】:

以上是关于ABP 框架测试中的“无法解析参数”错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual Studio 2015 中调试时无法解析参数名称类型 ISession

解析ABP框架中的事务处理和工作单元,ABP事务处理

Bash:由“文本在找到匹配引用之前结束”引起的“无法解析参数”[重复]

Express post请求无法解析参数的原因

如何在使用 SQLite 作为内存数据库的 ABP 框架上集成测试接受 SQL 参数的方法?

[Architect] Abp 框架原理解析 UnitOfWork