Skip to content

Commit

Permalink
Isssue unit test devlooped#1129
Browse files Browse the repository at this point in the history
  • Loading branch information
Mujdat Dinc committed Jan 15, 2021
1 parent 424fe31 commit 09ce4f9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/Moq/Interception/InterceptionAspects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ public static bool Handle(Invocation invocation, Mock mock)
{
return false;
}

MethodInfo invocationMethod = invocation.Method;
if (invocationMethod.IsPropertyAccessor())
{
string propertyNameToSearch = invocationMethod.Name.Substring(AccessorPrefixLength);
PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch);
PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch, new Type[0]);

if (property == null)
{
Expand Down Expand Up @@ -258,7 +258,7 @@ public static bool Handle(Invocation invocation, Mock mock)

private static object CreateInitialPropertyValue(Mock mock, MethodInfo getter)
{
object initialValue = mock.GetDefaultValue(getter, out Mock innerMock,
object initialValue = mock.GetDefaultValue(getter, out Mock innerMock,
useAlternateProvider: mock.AutoSetupPropertiesDefaultValueProvider);

if (innerMock != null)
Expand Down
114 changes: 66 additions & 48 deletions tests/Moq.Tests.VisualBasic/IssueReports.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,90 @@ Imports Xunit

Public Class IssueReports

Public Class Issue278
Public Class Issue278

<Fact()>
Public Sub SetupsForPropertiesWithMultipleArgsDoNotOverwriteEachOther()
Dim mock As New Mock(Of ISimpleInterface)()
<Fact()>
Public Sub SetupsForPropertiesWithMultipleArgsDoNotOverwriteEachOther()
Dim mock As New Mock(Of ISimpleInterface)()

mock.Setup(Function(m) m.PropertyWithMultipleArgs(1, 1)).Returns(1)
mock.Setup(Function(m) m.PropertyWithMultipleArgs(1, 2)).Returns(2)
mock.Setup(Function(m) m.PropertyWithMultipleArgs(1, 1)).Returns(1)
mock.Setup(Function(m) m.PropertyWithMultipleArgs(1, 2)).Returns(2)

Assert.Equal(1, mock.Object.PropertyWithMultipleArgs(1, 1))
Assert.Equal(2, mock.Object.PropertyWithMultipleArgs(1, 2))
Assert.Equal(1, mock.Object.PropertyWithMultipleArgs(1, 1))
Assert.Equal(2, mock.Object.PropertyWithMultipleArgs(1, 2))

End Sub
End Sub

Public Interface ISimpleInterface
Public Interface ISimpleInterface

ReadOnly Property PropertyWithMultipleArgs(setting As Integer, setting2 As Integer) As Integer
ReadOnly Property PropertyWithMultipleArgs(setting As Integer, setting2 As Integer) As Integer

End Interface
End Class
End Interface
End Class

Public Class Issue1067
Public Class Issue1067

<Fact>
Public Sub Test_NonGeneric()
Dim userManagerMock = New Mock(Of IUserManager)()
Setup_NonGeneric(userManagerMock, 42)
<Fact>
Public Sub Test_NonGeneric()
Dim userManagerMock = New Mock(Of IUserManager)()
Setup_NonGeneric(userManagerMock, 42)

Dim user As New User()
userManagerMock.Object.Create(user)
Dim user As New User()
userManagerMock.Object.Create(user)

Assert.Equal(42, user.Id)
End Sub
Assert.Equal(42, user.Id)
End Sub

<Fact>
Public Sub Test_Generic()
Dim userManagerMock = New Mock(Of IUserManager)()
Setup_Generic(Of User)(userManagerMock, 42)
<Fact>
Public Sub Test_Generic()
Dim userManagerMock = New Mock(Of IUserManager)()
Setup_Generic(Of User)(userManagerMock, 42)

Dim user As New User()
userManagerMock.Object.Create(user)
Dim user As New User()
userManagerMock.Object.Create(user)

Assert.Equal(42, user.Id)
End Sub
Assert.Equal(42, user.Id)
End Sub

Public Class User
Property Id As Integer
End Class
Public Class User
Property Id As Integer
End Class

Public Interface IUserManager
Sub Create(User As User)
End Interface
Public Interface IUserManager
Sub Create(User As User)
End Interface

Protected Sub Setup_NonGeneric(userManagerMock As Mock(Of IUserManager), expectedId As Integer)
userManagerMock.Setup(Sub(manager) manager.Create(It.IsAny(Of User))).Callback(Sub(user) user.Id = expectedId)
End Sub
Protected Sub Setup_NonGeneric(userManagerMock As Mock(Of IUserManager), expectedId As Integer)
userManagerMock.Setup(Sub(manager) manager.Create(It.IsAny(Of User))).Callback(Sub(user) user.Id = expectedId)
End Sub

Protected Sub Setup_Generic(Of TUser As User)(userManagerMock As Mock(Of IUserManager), expectedId As Integer)
userManagerMock.Setup(Sub(manager) manager.Create(It.IsAny(Of TUser))).Callback(Sub(user) user.Id = expectedId)
' ^
' The use of generics will cause the VB.NET compiler to wrap the `It.IsAny<>` call with two `Convert` nodes.
' The inner conversion will convert to `Object`, and the outer conversion will convert to `User` (i.e. the type that
' `TUser` is constrained to). `MatcherFactory` needs to be able to recognize the `It.IsAny<>` matcher even if it
' is doubly wrapped!
End Sub
Protected Sub Setup_Generic(Of TUser As User)(userManagerMock As Mock(Of IUserManager), expectedId As Integer)
userManagerMock.Setup(Sub(manager) manager.Create(It.IsAny(Of TUser))).Callback(Sub(user) user.Id = expectedId)
' ^
' The use of generics will cause the VB.NET compiler to wrap the `It.IsAny<>` call with two `Convert` nodes.
' The inner conversion will convert to `Object`, and the outer conversion will convert to `User` (i.e. the type that
' `TUser` is constrained to). `MatcherFactory` needs to be able to recognize the `It.IsAny<>` matcher even if it
' is doubly wrapped!
End Sub

End Class
End Class

Public Class Issue1129

<Fact>
Public Sub Test()
Dim failClassMock = New Mock(Of IndexerInterface)()

failClassMock.SetupAllProperties()

Assert.False(failClassMock.Object.Value)
End Sub
Interface IndexerInterface
ReadOnly Property SystemDefault() As Boolean
Property Value() As Boolean
Property Value(ByVal OverrideLevel As Integer) As Boolean
Property Value(ByVal OverrideLevel As Integer, ByVal OverrideID As String) As Boolean
End Interface
End Class

End Class

0 comments on commit 09ce4f9

Please sign in to comment.