首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XUnit模拟自定义标识管理器和存储区,无法实例化API.Identity.Managers.MyUserManager类的代理

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

Stack Overflow用户
提问于 2020-09-24 20:08:25
回答 1查看 78关注 0票数 2

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

代码语言:javascript
复制
 _myUserManager = new Mock<MyUserManager<AppUser>>(_myUserStore.Object);

在这一行,当试图模拟对象时,类型为Castle.DynamicProxy.InvalidProxyConstructorArgumentsException,的异常。

消息和堆栈跟踪在这里:

代码语言:javascript
复制
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

下面是测试类

代码语言:javascript
复制
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构造函数如下所示:

代码语言:javascript
复制
    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;
    }

自定义用户存储:

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2020-09-25 16:30:57

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

所以你的构造函数是

代码语言:javascript
复制
 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参数:

代码语言:javascript
复制
new Mock<MyUserManager<AppUser>>(_myUserStore.Object);

您将需要提供其余的参数;或者使用不同的构造函数(并再次为其提供参数);或者,如果您可以为MyUserManager定义一个接口,则更好的做法是模拟该接口。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64046123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档