C# 如何在abp框架中使用nSubstitute模拟依赖关系,

C# 如何在abp框架中使用nSubstitute模拟依赖关系,,c#,testing,nsubstitute,abp,C#,Testing,Nsubstitute,Abp,在Abp框架中,我试图为我的FamilyAppService类编写一个测试(见下文)。我需要在FamilyAppService的构造函数中模拟一个IAutho实例。我尝试模拟IAutho,然后将其添加到FamilyAppService的新实例中(而不是使用GetRequiredService()),但ObjectMapper出现了“System.ArgumentNullException”错误 家庭服务类 public class FamilyAppService : KmAppSer

在Abp框架中,我试图为我的FamilyAppService类编写一个测试(见下文)。我需要在FamilyAppService的构造函数中模拟一个IAutho实例。我尝试模拟IAutho,然后将其添加到FamilyAppService的新实例中(而不是使用GetRequiredService()),但ObjectMapper出现了“System.ArgumentNullException”错误

家庭服务类

     public class FamilyAppService : KmAppService, IFamilyAppService { 
        private readonly IAutho autho;

        public FamilyAppService( 
            IAutho autho) {
 
            this.autho = autho;
        } 

        public virtual async Task SendRequest(SendRequestInput input) {
             var family = ObjectMapper.Map<FamilyDto, Family>(input.Family);
             // code ...
             await autho.FindUserIdByEmail(family.Email); 
        }
    }

公共类FamilyAppService:KmAppService,IFamilyAppService{
私有只读IAutho autho;
公共家庭服务(
(作者){
this.autho=autho;
} 
公共虚拟异步任务SendRequest(SendRequestInput){
var family=ObjectMapper.Map(input.family);
//代码。。。
等待autho.finduseridbymail(family.Email);
}
}
Autho类。 我需要使用nSubstitute用模拟类替换IAutho依赖

public class Autho : IAutho, ITransientDependency { 

        public Autho( ) {
           
        } 

        public virtual async Task<User> FindUserIdByEmail(string input) { 
            // Don’t  want this code touched in test 
            // Code ...
        }
 
    }
公共类Autho:IAutho,ITransientDependency{
公共作者(){
} 
公共虚拟异步任务FindUserIdByEmail(字符串输入){
//不希望在测试中触碰此代码
//代码。。。
}
}
我目前的测试

  [Fact]
        public async Task ShouldSendEmail() {
  
            var autho = Substitute.For<IAutho>();
            autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);  
    
            var familyAppService = new FamilyAppService(autho);
            // var familyAppService = GetRequiredService<IFamilyAppService>();

            // setup input code here..
            // get ObjectMapper error here
            await familyAppService.SendRequest(input);
 
            // Assert  
        }

[事实]
公共异步任务应发送电子邮件(){
var autho=替换.For();
autho.FindUserIdByEmail(Arg.Any())。返回((用户)null);
var familyAppService=新的familyAppService(autho);
//var familyAppService=GetRequiredService();
//此处设置输入代码。。
//在此处获取ObjectMapper错误
等待familyAppService.SendRequest(输入);
//断言
}

github abp回购协议的一位成员给了我正确的答案。您需要重写测试类上的AfterAddApplication方法,并将替换项/mock添加到services.AddSingletone

例如

        protected override void AfterAddApplication(IServiceCollection services) {
 
            var autho = Substitute.For<IAutho>();
            autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);

            services.AddSingleton(autho);
        }
受保护的override void AfterAddApplication(IServiceCollection服务){
var autho=替换.For();
autho.FindUserIdByEmail(Arg.Any())。返回((用户)null);
服务。AddSingleton(autho);
}