XUnit模拟自定义身份管理器和存储,无法实例化以下类的代理:API.Identity.Managers.MyUserManager

如何解决XUnit模拟自定义身份管理器和存储,无法实例化以下类的代理:API.Identity.Managers.MyUserManager

我正在使用Moq来模拟我的AuthenticationController,初始化XUnit测试时会发生错误, 问题出在在线测试类的构造函数上:

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

在此行上,尝试模拟对象时,出现Castle.DynamicProxy.InvalidProxyconstructorargumentsException类型的异常。

和messege和stacktrace在这里

Message: 
Castle.DynamicProxy.InvalidProxyconstructorargumentsException : Can not instantiate proxy of class: API.Identity.Managers.MyUserManager`1[[API.Identity.Models.AppUser,API,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]].
Could not find a constructor that would match given arguments:
Castle.Proxies.IMyUserStoreProxy

Stack Trace: 
proxygenerator.CreateClassproxyInstance(Type proxyType,List`1 proxyArguments,Type classtoProxy,Object[] constructorarguments)
proxygenerator.CreateClassproxy(Type classtoProxy,Type[] additionalInterfacesToProxy,proxygenerationoptions options,Object[] constructorarguments,IInterceptor[] interceptors)
CastleProxyFactory.CreateProxy(Type mockType,IInterceptor interceptor,Type[] interfaces,Object[] arguments)
Mock`1.InitializeInstance()
Mock`1.OnGetobject()
Mock.get_Object()
Mock`1.get_Object()
AuthenticateControllerTests.ctor() line 47

这是测试班

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

我的自定义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 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;

解决方法

在模拟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)

但是您只提供一个IMyUserStore参数:

new Mock<MyUserManager<AppUser>>(_myUserStore.Object);

您需要提供其余的参数;或使用其他构造函数(并再次提供参数);或者如果您可以为MyUserManager定义一个接口,还是更好,请对其进行模拟。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?