模拟 - 无法实例化 属性 的代理 class?

Mocking - cannot instantiate proxy class of property?

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

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

但是我的排列线抛出以下错误:

Can not instantiate proxy of class: Microsoft.AspNet.Identity.UserManager`1[[SWDB.BusinessLayer.Identity.ApplicationUser, SWDB.BusinessLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Could not find a parameterless constructor.

为什么会这样,因为在设置中我已经将我的 mockOwinManager 的 UserManager 属性 设置为我想要的 return?

先创建一个UserManager的模拟对象。然后设置它的虚方法FindByIdAsync(假设属性UserManager的类型是classAppUserManager,假设这个class实现了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 class 在构造函数中接受 IUserStore<ApplicationUser>,用于访问 FindByIdAsync 和 etc

当我需要测试需要 UserManager 的 class 时,我会模拟 IUserStore 并设置它的 FindByIdAsync 方法,然后我创建 FindByIdAsync 的实例=12=] 并将我的模拟提供给它的参数。 这是一个 class 构造函数,需要 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);
}`: