如何模拟一个方法并返回由其回调设置的结果?



我正在使用下面的代码模拟UserManager

我遇到的问题是oResult仍然为空。但我无法在回调之外给它一个值,因为密码只能在回调内部使用。我不能只返回一个预定的值(例如IdentityResult.Success(,因为结果必须在运行时生成。

这三个问题是相似的,但它们并没有完全涵盖这个问题:

  1. 验证最小起订量单元测试方法返回值(这是最接近的(
  2. 嘲笑使用Moq框架的方法;t返回预期结果
  3. Moq设置方法返回值

所有这些与我的情况之间的区别在于,在我的情况下,方法所需的输入值仅在回调中可用。

我正在使用它来测试我的控制器的行为。

如何为模拟方法(本例中为CreateAsync()(执行回调并返回其结果?


Protected Function UserManagerMock(Of TUser As Db.User, TCity as Db.City)(Users As List(Of TUser)) As Mock(Of UserManager)
Dim oManagerMock As Mock(Of UserManager)
Dim oStoreMock As Mock(Of IUserStore(Of TUser))
Dim oCallback As Action(Of TUser, String)
Dim oManager As UserManager
Dim oResult As IdentityResult
Dim oSetup As Expression(Of Func(Of UserManager, Task(Of IdentityResult)))
oStoreMock = New Mock(Of IUserStore(Of TUser))
oManagerMock = New Mock(Of UserManager)(oUserStoreMock.Object)
oManager = oUserManagerMock.Object
oCallback = Sub(User, Password, City)
oResult = oManager.PasswordValidator.ValidateAsync(Password).Result
If oResult Is IdentityResult.Success Then
User.PasswordHash = oManager.PasswordHasher.HashPassword(Password)
Users.Add(User)
End If
End Sub
oManager.PasswordValidator = New PasswordValidator
oManager.UserValidator = New UserValidator(Of TUser)(oManager)
oSetup = Function(Manager) Manager.CreateAsync(It.IsAny(Of Db.User), It.IsAny(Of String), It.IsAny(Of Db.City))
oUserManagerMock.Setup(oSetup).ReturnsAsync(oResult).Callback(oCallback)
Return oManagerMock
End Function

这可以通过完全取消回调而使用来实现

Func(Of ..., Task(Of IdentityResult))

在设置的CCD_ 5调用中。

示例:

Protected Function GetUserManager(Users As List(Of Db.User)) As UserManager
Return Me.GetUserManagerMock(Of Db.User, Db.City)(Users).Object
End Function
Private Function GetUserManagerMock(Of TUser As Db.User, TCity As Db.City)(Users As List(Of TUser)) As Mock(Of UserManager)
Dim oManagerMock As Mock(Of UserManager)
Dim oStoreMock As Mock(Of IUserStore(Of TUser))
Dim oManager As UserManager
Dim oReturn As Func(Of TUser, String, TCity, Task(Of IdentityResult))
Dim oResult As IdentityResult
Dim oSetup As Expression(Of Func(Of UserManager, Task(Of IdentityResult)))
oStoreMock = New Mock(Of IUserStore(Of TUser))
oManagerMock = New Mock(Of UserManager)(oUserStoreMock.Object)
oManager = oUserManagerMock.Object
oReturn = Async Function(User, Password, City)
If City.IsNothing Then
oResult = IdentityResult.Failed($"{NameOf(City)} is required.")
Else
oResult = Await oManager.UserValidator.ValidateAsync(User)
If oResult Is IdentityResult.Success Then
oResult = Await oManager.PasswordValidator.ValidateAsync(Password)
If oResult Is IdentityResult.Success Then
User.PasswordHash = oManager.PasswordHasher.HashPassword(Password)
Users.Add(User)
User.CityId = City.Id
User.City = City
City.Users.Add(User)
End If
End If
End If
Return oResult
End Function
oSetup = Function(Manager) Manager.CreateAsync(It.IsAny(Of Db.User), It.IsAny(Of String), It.IsAny(Of Db.City))
oManagerMock.Setup(oSetup).Returns(oReturn)
Return oManagerMock
End Function

这样使用:

<Fact>
Public Async Function CreateUser() As Task
Dim oUserManager As UserManager
Dim oResult As IdentityResult
Dim sPassword As String
Dim oUser as Db.User
Dim oCity as Db.City

sPassword = "P@ssw0rd!"
oUser = New Db.User With {.FirstName = "User", .LastName = "Name", .UserName = "user.name", .Email = "username@domain.com"}
oCity = New Db.City With {.Id = 1, .Name = "BigCity", .Code = "L69CNV5"}
oUserManager = Me.GetUserManager(Me.Users)
oResult = Await oUserManager.CreateAsync(oUser, sPassword, oCity)
Assert.True(oResult.Succeeded)
End Function

来源于此。再次感谢stakx

相关内容

  • 没有找到相关文章

最新更新