Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XUnit模拟自定义标识管理器和存储,无法实例化类API.Identity.Managers.MyUserManager的代理_C#_Asp.net Core_Moq_Identity_Xunit - Fatal编程技术网

C# XUnit模拟自定义标识管理器和存储,无法实例化类API.Identity.Managers.MyUserManager的代理

C# XUnit模拟自定义标识管理器和存储,无法实例化类API.Identity.Managers.MyUserManager的代理,c#,asp.net-core,moq,identity,xunit,C#,Asp.net Core,Moq,Identity,Xunit,我使用Moq模拟AuthenticationController,初始化XUnit测试时出错, 问题在于在线测试类的构造函数: _myUserManager = new Mock<MyUserManager<AppUser>>(_myUserStore.Object); 这是考试班 public class AuthenticateControllerTests { private AuthenticateController _controller;

我使用Moq模拟AuthenticationController,初始化XUnit测试时出错, 问题在于在线测试类的构造函数:

 _myUserManager = new Mock<MyUserManager<AppUser>>(_myUserStore.Object);
这是考试班

public class AuthenticateControllerTests {

    private AuthenticateController _controller;

    private Mock<ILoggerSevice> _loggerSevice;
    private Mock<IJwtAuthService> _jwtAuthService;
    private Mock<IEmailService> _emailService;
    private Mock<IEmailGateway> _emailGateway;
    private Mock<IValidationHelper> _validationHelper;

    private Mock<IMyUserStore> _myUserStore;
    private Mock<MyUserManager<AppUser>> _myUserManager;
    private Mock<IMyCustomerUserStore> _myCustomerUserStore;
    private Mock<MyCustomerUserManager<AppCustomerUser>> _myUserCustomerManager;

    private Mock<SignInManager<AppUser>> _signInManager;
    private Mock<SignInManager<AppCustomerUser>> _signInCustomerManager;

    public AuthenticateControllerTests() {
        //set up authenticate controller
        _loggerSevice = new Mock<ILoggerSevice>();
        _jwtAuthService = new Mock<IJwtAuthService>();
        _emailService = new Mock<IEmailService>();
        _emailGateway = new Mock<IEmailGateway>();
        _validationHelper = new Mock<IValidationHelper>();


        _myUserStore = new Mock<IMyUserStore>();
        _myUserManager = new Mock<MyUserManager<AppUser>>(_myUserStore.Object);
        _signInManager = new Mock<SignInManager<AppUser>>(_myUserManager.Object);

        _myCustomerUserStore = new Mock<IMyCustomerUserStore>();
        _myUserCustomerManager = new Mock<MyCustomerUserManager<AppCustomerUser>>(_myCustomerUserStore.Object);
        _signInCustomerManager = new Mock<SignInManager<AppCustomerUser>>(_myUserCustomerManager.Object);

        _controller = new AuthenticateController(_loggerSevice.Object,
                                                _jwtAuthService.Object,
                                                _myUserManager.Object,
                                                _myUserCustomerManager.Object,
                                                _signInManager.Object,
                                                _signInCustomerManager.Object,
                                                _emailService.Object,
                                                _validationHelper.Object,
                                                _emailGateway.Object);
    }
公共类AuthenticateControllerTests{
专用认证控制器(U控制器),;
私人模拟停车场;
私人模拟服务;
私人模拟电子邮件服务;
专用模拟电子邮件网关;
私有模拟验证助手;
私有Mock_myUserStore;
私有Mock_myUserManager;
私人模拟myCustomerUserStore;
私有Mock_myUserCustomerManager;
私人模拟签名管理员;
私人模拟客户经理;
公共AuthenticateControllerTests(){
//设置验证控制器
_loggerSevice=newmock();
_jwtAuthService=newmock();
_emailService=newmock();
_emailGateway=new Mock();
_validationHelper=newmock();
_myUserStore=new Mock();
_myUserManager=newmock(_myUserStore.Object);
_signInManager=new Mock(_myUserManager.Object);
_myCustomerUserStore=new Mock();
_myUserCustomerManager=new Mock(_myCustomerUserStore.Object);
_signInCustomerManager=新建模拟(_myUserCustomerManager.Object);
_控制器=新的AuthenticateController(_loggerSevice.Object,
_jwtAuthService.Object,
_myUserManager.Object,
_myUserCustomerManager.Object,
_signInManager.Object,
_signInCustomerManager.Object,
_emailService.Object,
_validationHelper.Object,
_emailGateway.Object);
}
我的自定义UserManager构造函数如下所示:

    public MyUserManager(IMyUserStore myUserStore, 
                         IOptions<IdentityOptions> optionsAccessor, 
                         IPasswordHasher<AppUser> passwordHasher, 
                         IEnumerable<IUserValidator<AppUser>> userValidators, 
                         IEnumerable<IPasswordValidator<AppUser>> passwordValidators, 
                         ILookupNormalizer keyNormalizer, 
                         IdentityErrorDescriber errors,
                         IServiceProvider services, 
                         ILogger<UserManager<AppUser>> logger) : 
                    base(myUserStore, optionsAccessor,
                         passwordHasher, userValidators, passwordValidators,
                         keyNormalizer, 
                         errors, services, logger) {

        _myUserStore = myUserStore;
    }
public MyUserManager(IMyUserStore myUserStore,
IOPS选项访问器,
IPasswordHasher密码hasher,
IEnumerable用户验证程序,
IEnumerable密码验证器,
ILookupNormalizer键规格化器,
IdentityErrorDescriptber错误,
IServiceProvider服务,
ILogger记录器):
基本(myUserStore、OptionAccessor、,
passwordHasher、userValidators、passwordValidators、,
键归一化器,
错误、服务、记录器){
_myUserStore=myUserStore;
}
自定义用户存储:

public interface IMyUserStore : IUserStore<AppUser>, IUserEmailStore<AppUser>, 
                                IUserPhoneNumberStore<AppUser>,
                                IUserTwoFactorStore<AppUser>, IUserPasswordStore<AppUser>, 
                                IUserRoleStore<AppUser> {

public class MyUserStore : IMyUserStore {

    private readonly IUserGateway _userGateway;
    private readonly IRoleGateway _roleGateway;
公共接口IMyUserStore:IUserStore、IUserEmailStore、,
IUserPhoneNumberStore,
iUserTwoFactoryStore、IUserPasswordStore、,
IUserRoleStore{
公共类MyUserStore:IMyUserStore{
专用只读IUserGateway\u userGateway;
专用只读IRoleGateway_roleGateway;

模拟实现(如
MyUserManager
)时,Moq需要一个可访问的构造函数来创建模拟,并且您需要提供所述构造函数的参数

所以你的构造函数是

 public MyUserManager(IMyUserStore myUserStore, 
                         IOptions<IdentityOptions> optionsAccessor, 
                         IPasswordHasher<AppUser> passwordHasher, 
                         IEnumerable<IUserValidator<AppUser>> userValidators, 
                         IEnumerable<IPasswordValidator<AppUser>> passwordValidators, 
                         ILookupNormalizer keyNormalizer, 
                         IdentityErrorDescriber errors,
                         IServiceProvider services, 
                         ILogger<UserManager<AppUser>> logger)
您需要提供其余的参数;或者使用不同的构造函数(并再次为提供参数);或者如果您可以为
MyUserManager
定义一个接口,则更好,模拟它

 public MyUserManager(IMyUserStore myUserStore, 
                         IOptions<IdentityOptions> optionsAccessor, 
                         IPasswordHasher<AppUser> passwordHasher, 
                         IEnumerable<IUserValidator<AppUser>> userValidators, 
                         IEnumerable<IPasswordValidator<AppUser>> passwordValidators, 
                         ILookupNormalizer keyNormalizer, 
                         IdentityErrorDescriber errors,
                         IServiceProvider services, 
                         ILogger<UserManager<AppUser>> logger)
new Mock<MyUserManager<AppUser>>(_myUserStore.Object);