1

I have a legacy class something along the lines of this:

Public Class MyOverloadExample
    Public Overridable ReadOnly Property SomeDescription() As String
            Get
                Return "No Parameter"
            End Get
        End Property
    Public ReadOnly Property SomeDescription(ByVal p1 As Integer) As String
            Get
                Return "Used Parameter"
            End Get
        End Property
End Class

I want to SetupGet on the first SomeDescription Property with no parameters. I tried this:

Dim mock = New Mock(Of MyOverloadExample)
mock.SetupGet(Function(i) i.SomeDescription()).Returns("Mocked the No Parameter Property")

But it fails with this exception:

threw exception: 
System.Reflection.AmbiguousMatchException: Ambiguous match found.
    at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
   at System.Type.GetProperty(String name, BindingFlags bindingAttr)
   at Moq.ExpressionExtensions.ToPropertyInfo(LambdaExpression expression) in C:\projects\moq4\src\Moq\ExpressionExtensions.cs:line 47
   at Moq.Mock.SetupGetPexProtected[T,TProperty](Mock`1 mock, Expression`1 expression, Condition condition) in C:\projects\moq4\src\Moq\Mock.cs:line 511
   at Moq.PexProtector.Invoke[T1,T2,T3,TResult](Func`4 function, T1 arg1, T2 arg2, T3 arg3) in C:\projects\moq4\src\Moq\PexProtector.cs:line 38
   at Moq.Mock.SetupGet[T,TProperty](Mock`1 mock, Expression`1 expression, Condition condition) in C:\projects\moq4\src\Moq\Mock.cs:line 496
   at Moq.Mock`1.SetupGet[TProperty](Expression`1 expression) in C:\projects\moq4\src\Moq\Mock.Generic.cs:line 332

I tried without the empty () but get the same exception:

mock.SetupGet(Function(i) i.SomeDescription).Returns("Mocked the No Parameter Property")

Is there some lambda/moq magic I'm missing or is this just not possible with Moq?

  • 1
    C# does not have parametrized properties, VB.NET does. As mentioned in https://stackoverflow.com/q/798570/11683, this was first raised [in 2008](https://stackoverflow.com/questions/798570/experiences-using-moq-with-vb-net#comment613284_798668) and apparently is still not fixed. – GSerg Jan 11 '20 at 17:24
  • 1
    That issue in 2008 was for a Property setter with parameters. I haven't tried Mocking a setter with parameters yet so not sure if it is fixed now or not. Mocking Property Getters work except in this one case with overloaded one with and one without parameters. I'll look at logging an issue with the google code people. – Darren J. McLeod Jan 11 '20 at 17:52
  • @DarrenJ.McLeod Does the same happen with just `Setup`? ie: `mock.Setup(Function(i) i.SomeDescription()).Returns("Mocked the No Parameter Property")` – Nkosi Jan 11 '20 at 20:47
  • @Nkosi The same happens with Setup. – Darren J. McLeod Jan 13 '20 at 23:48

0 Answers0