ameniameni / moq

Automatically exported from code.google.com/p/moq
Other
0 stars 0 forks source link

Error calling SetupAllProperties for Mock<IDataErrorInfo> #205

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
 [TestFixture]
public class FooTests
{
        [Test]
        public void TestCase()
        {
            var mock = new Mock<IDataErrorInfo>();

            mock.SetupAllProperties();
        }
}

What is the expected output? 
no errors

What do you see instead?

TestCase 'Tests.NewFolder1.FooTests.TestCase'
failed: System.Reflection.TargetInvocationException : Exception has been
thrown by the target of an invocation.
  ----> System.IndexOutOfRangeException : Index was outside the bounds of
the array.
    at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[]
arguments, SignatureStruct& sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
    at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[]
arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean skipVisibilityChecks)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at Moq.Mock.<>c__DisplayClass27.<SetupAllProperties>b__23()
    at Moq.PexProtector.Invoke(Action action)
    at Moq.Mock.SetupAllProperties(Mock mock)
    at Moq.Mock`1.SetupAllProperties()
    NewFolder1\Tests.cs(321,0): at Tests.NewFolder1.FooTests.TestCase()
    --IndexOutOfRangeException
    at Moq.MethodCall..ctor(Mock mock, Expression originalExpression,
MethodInfo method, Expression[] arguments)
    at Moq.MethodCallReturn`2..ctor(Mock mock, Expression originalExpression,
MethodInfo method, Expression[] arguments)
    at Moq.Mock.<>c__DisplayClass15`2.<SetupGet>b__14()
    at Moq.PexProtector.Invoke[T](Func`1 function)
    at Moq.Mock.SetupGet[T1,TProperty](Mock mock, Expression`1 expression)
    at Moq.Mock`1.SetupGet[TProperty](Expression`1 expression)

0 passed, 1 failed, 0 skipped, took 1.55 seconds (NUnit 2.5.2).

What version of the product are you using? 
3.1.0.0

On what operating system?
win 7 x64

Original issue reported on code.google.com by cliftonf...@gmail.com on 27 Aug 2009 at 11:34

GoogleCodeExporter commented 8 years ago

Original comment by kzu.net on 25 Oct 2009 at 4:11

GoogleCodeExporter commented 8 years ago
This still happens with 4.0.812.4. The cause seems to be that IDataErrorInfo 
defines 
an indexer, which is implemented as a property whose get/set methods take a 
parameter.

I would propose modifying Moq.Extensions.CanOverrideGet() to read

    return getter != null && getter.IsVirtual && !getter.IsFinal && 
(getter.GetParameters() == null || getter.GetParameters().Count() == 0);

I've also written this regression test which fails without this modification 
and 
passes with this modification:

        #region #205

        public class _205
        {
            public interface IIndexer
            {
                string this[string param] { get; }
            }

            [Fact]
            public void Test()
            {
                var mock = new Mock<IIndexer>();
                Assert.DoesNotThrow(() => mock.SetupAllProperties());
            }
        }

        #endregion

Original comment by david.black.co.at@gmail.com on 11 Feb 2010 at 11:19

GoogleCodeExporter commented 8 years ago

Original comment by kzu.net on 11 Feb 2010 at 3:34

GoogleCodeExporter commented 8 years ago
Indexers excluded from SetupAllProperties

Original comment by marian...@gmail.com on 20 Feb 2010 at 11:51

GoogleCodeExporter commented 8 years ago
Issue 225 has been merged into this issue.

Original comment by marian...@gmail.com on 13 May 2010 at 5:13