gpt4 book ai didi

c# - 模拟 SignInManager

转载 作者:行者123 更新时间:2023-12-01 22:06:18 24 4
gpt4 key购买 nike

使用 Moq 和 xUnit 进行单元测试的新手。我正在尝试模拟在 Controller 构造函数中使用的 SignInManager 来构建单元测试。我可以找到的 SignInManager 构造函数的文档说它接受 UserManagerAuthenticationManager 对象:https://msdn.microsoft.com/en-us/library/mt173769(v=vs.108).aspx#M:Microsoft.AspNet.Identity.Owin.SignInManager `2。

当我尝试模拟 Controller 时,收到一条错误消息,指出它无法实例化 SignInManagerAuthenticationManager 类的代理。

错误:

"Message: Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: Microsoft.AspNetCore.Identity.SignInManager1[[Models.AppUser, , Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Could not find a constructor that would match given arguments: Castle.Proxies.UserManager`1Proxy Castle.Proxies.AuthenticationManagerProxy"

单元测试:

public void Can_Send_Password_Reset_Email()
{
//Arrange
//create mock services
Mock<IEmailService> mockEmailService = new Mock<IEmailService>();
Mock<ILessonRepository> mockRepo = new Mock<ILessonRepository>();
Mock<UserManager<AppUser>> mockUsrMgr = GetMockUserManager();
var mockSignInMgr = GetMockSignInManager();
Mock<UserValidator<AppUser>> mockUsrVal = new Mock<UserValidator<AppUser>>();
Mock<PasswordValidator<AppUser>> mockPwdVal = new Mock<PasswordValidator<AppUser>>();
Mock<PasswordHasher<AppUser>> mockPwdHshr = new Mock<PasswordHasher<AppUser>>();
Mock<ForgotPasswordModel> model = new Mock<ForgotPasswordModel>();
model.Object.Email = "joe@example.com";

var user = new AppUser();
var token = mockUsrMgr.Object.GeneratePasswordResetTokenAsync(user).Result;

//create mock temporary data, needed for controller message
Mock<ITempDataDictionary> tempData = new Mock<ITempDataDictionary>();
//create the controller
//ERROR ON THIS LINE
AccountController controller = new AccountController(mockUsrMgr.Object, mockSignInMgr.Object, mockUsrVal.Object, mockPwdVal.Object, mockPwdHshr.Object, mockEmailService.Object)
{
TempData = tempData.Object
};

//Act
//the controller should call the email action method
controller.PasswordResetEmail(model.Object);

//Assert
//verify that the email service method was called one time
mockEmailService.Verify(m => m.PasswordResetMessage(user, "Test Email"), Times.Once());
}

SignInManager 模拟函数:

//create a mock SignInManager class
private Mock<SignInManager<AppUser>> GetMockSignInManager()
{
var mockUsrMgr = GetMockUserManager();
var mockAuthMgr = new Mock<AuthenticationManager>();
return new Mock<SignInManager<AppUser>>(mockUsrMgr.Object, mockAuthMgr.Object);
}

GetMockUserManager() 在其他单元测试中工作正常,似乎不是问题。

最佳答案

这就是我的做法,希望对你有帮助。

public class FakeSignInManager : SignInManager<ApplicationUser>
{
public FakeSignInManager()
: base(new FakeUserManager(),
new Mock<IHttpContextAccessor>().Object,
new Mock<IUserClaimsPrincipalFactory<ApplicationUser>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<ILogger<SignInManager<ApplicationUser>>>().Object,
new Mock<IAuthenticationSchemeProvider>().Object)
{ }
}



public class FakeUserManager : UserManager<ApplicationUser>
{
public FakeUserManager()
: base(new Mock<IUserStore<ApplicationUser>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<ApplicationUser>>().Object,
new IUserValidator<ApplicationUser>[0],
new IPasswordValidator<ApplicationUser>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<ApplicationUser>>>().Object)
{ }

public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
{
return Task.FromResult(IdentityResult.Success);
}

public override Task<IdentityResult> AddToRoleAsync(ApplicationUser user, string role)
{
return Task.FromResult(IdentityResult.Success);
}

public override Task<string> GenerateEmailConfirmationTokenAsync(ApplicationUser user)
{
return Task.FromResult(Guid.NewGuid().ToString());
}

}

关于c# - 模拟 SignInManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48189741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com