在我的测试中,这是我的代码:
[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属性设置为我希望它返回的内容?
由于
答案 0 :(得分:3)
首先创建UserManager
的模拟对象。然后设置其虚拟方法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);
答案 1 :(得分:1)
UserManager
类在构造函数中接受IUserStore<ApplicationUser>
,该构造函数用于获取对FindByIdAsync
等的访问权限
当我需要测试需要UserManager
的类时,我会为IUserStore
进行模拟并设置其FindByIdAsync
方法,然后创建UserManager
的实例并提供模拟其参数。
这是一个类构造器,需要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);
}`: