Mocking-无法实例化属性的代理类

本文关键字:代理 属性 实例化 Mocking- | 更新日期: 2023-09-27 18:28:05

在我的测试中,这里是我的代码:

[SetUp]
public void Initialise()
{
    mockOwinManager = new Mock<IOwinManager<ApplicationUser, ApplicationRole>>();
    mockSearch = new Mock<ISearch<ApplicationUser>>();
    mockMail = new Mock<IRpdbMail>();
    mockUserStore = new Mock<IUserStore<ApplicationUser>>();
    mockOwinManager.Setup(x => x.UserManager).Returns(() => new AppUserManager(mockUserStore.Object));
    sut = new UsersController(mockOwinManager.Object, mockSearch.Object, mockMail.Object);
}

然后是测试本身:

[Test]
public void WhenPut_IfUserIsNullReturnInternalServerError()
{
    //Arrange
    mockOwinManager.Setup(x => x.UserManager.FindByIdAsync(It.IsAny<string>())).Returns(() => null);
    //Act
    var response = sut.Put(new AppPersonUpdate());
    //Assert
    Assert.AreEqual(response.Result.StatusCode, HttpStatusCode.InternalServerError);
}

但我的排列行出现以下错误:

无法实例化类的代理:Microsoft.AspNet.Identity.UserManager`1[[SWDB.BusinessLayer.Identity.ApplicationUser,SWDB.BusinessLayer,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]。找不到无参数构造函数。

为什么会这样,因为在安装程序中,我已经将mockOwinManager的UserManager属性设置为我希望它已经返回的属性?

Mocking-无法实例化属性的代理类

首先创建一个UserManager的mock对象。然后设置它的虚拟方法FindByIdAsync(假定属性UserManager的类型是类AppUserManager,并且假设这个类实现IAppUserManager)。

var yourMockOfUserManager = new Mock<IAppUserManager>();
yourMockOfUserManage.Setup(x=>x.FindByIdAsync(It.IsAny<string>())).Returns(() => null);

最后是

mockOwinManager.Setup(x => x.UserManager).Returns(() => yourMockOfUserManager.Object);

UserManager类在构造函数中接受IUserStore<ApplicationUser>,该构造函数用于访问FindByIdAsync

当我需要测试需要UserManager的类时,我对IUserStore进行mock并设置其FindByIdAsync方法,然后创建UserManager的实例并将mock提供到其参数中。这是一个类构造函数,需要UserManager:

internal class UserManagerWrapper
{
    private readonly IEditUserResponceDataModelProvider _editUserResponceDataModelProvider;
    private readonly UserManager<User> _userManager;
    private readonly IUserModelFactory _userModelFactory;
    public UserManagerWrapper(UserManager<User> userManager,
        IUserModelFactory userModelFactory,
        IEditUserResponceDataModelProvider editUserResponceDataModelProvider)
    {
        _userManager = userManager;
        _userModelFactory = userModelFactory;
        _editUserResponceDataModelProvider = editUserResponceDataModelProvider;
    }
    public async Task<IUserModel> FindByIdAsync(string id)
    {      
        var user = await _userManager.FindByIdAsync(id);
        return _userModelFactory.Create(user.Id, user.Email, user.Year, user.UserName);
    }
}

这是我对UserManagerWrapper:的测试

private Mock<IUserStore<User>> UserStoreMock { get; set; }
[SetUp]
public void SetUp()
{
    UserStoreMock = new Mock<IUserStore<User>>();
    UserStoreMock.Setup(x => x.FindByIdAsync(It.IsAny<string>(), CancellationToken.None))
        .Returns(Task.FromResult(new User() {Year = 2020}));
}
[TestCase(2020)]
public async Task ValidateUserYear(int year)
{
    var userManager = new UserManager<User>(UserStoreMock.Object, null, null, null, null, null, null, null, null);
    var userManagerWrapper = new UserManagerWrapper(userManager, new UserModelFactory(), null);
    var findByIdAsync = await userManagerWrapper.FindByIdAsync("1");
    Assert.AreEqual(findByIdAsync.Year, year);
}`: