Export to GitHub

moq - issue #320

Mocking Indexer using Strict Behaviour


Posted on Aug 1, 2011 by Happy Dog

What steps will reproduce the problem? 1. Create a strict mock of an object 2. SetupSet on an object's indexer 3. Try to set a value in indexer

What is the expected output? What do you see instead? I expect the callback that I setup to be called -- rather I am told that "All invocations on the mock must have a corresponding setup."

What version of the product are you using? On what operating system? I'm using 4.0.1

Please provide any additional information below.

Code example

var retk,retv; var mock = new Mock<MyClass>(Behaviour.Strict); mock.SetupSet(x=>x[It.IsAny<Key>() = It.IsAny<Value>()).Callback<Key,Value>((k,v)=> {retk = k; retv = v;});

Comment #1

Posted on Aug 1, 2011 by Happy Dog

Comment deleted

Comment #2

Posted on Aug 1, 2011 by Happy Dog

Comment deleted

Comment #3

Posted on Aug 1, 2011 by Happy Dog

Comment deleted

Comment #4

Posted on Aug 1, 2011 by Quick Monkey

I don't understand the error report.

mock.SetupSet(x=>x[It.IsAny() = It.IsAny())

doesn't even compile. I'm not sure what you mean by "generic It.IsAny" either.

Comment #5

Posted on Aug 1, 2011 by Happy Dog

Comment deleted

Comment #6

Posted on Aug 1, 2011 by Quick Monkey

The problem is that in C# you cannot use open generics anywhere :P.

Can't fix the compiler in Moq ;)

Comment #7

Posted on Aug 1, 2011 by Happy Dog

Comment deleted

Comment #8

Posted on Aug 1, 2011 by Quick Monkey

If you don't provide code that compiles at least, I can hardly repro or see what the problem is. It must NOT be your production code, by definition. Just a clean, self-contained, failing repro.

Proper bug reporting is a pre-requisite for getting a fix for it, in almost any project I used or worked with.

Comment #9

Posted on Aug 1, 2011 by Happy Dog

using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq;

namespace TestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var mockMyClass = new Mock(); Key retKey = default(Key); Value retVal = default(Value); mockMyClass.SetupSet(x => x[It.IsAny()] = It.IsAny()).Callback( (k, v) => { retKey = k; retVal = v; });

        var testKey = new Key();
        var testVal = new Value();
        mockMyClass.Object[testKey] =  testVal;

        Assert.AreEqual(testVal, retVal);

    }



}

public class MyClass
{
    private Dictionary<Key, Value> _dict = new Dictionary<Key, Value>();

    public virtual Value this[Key k]
    {
        get { return _dict[k]; }
        set { _dict[k] = value; }

    }
}

public class Key
{

}

public class Value
{

}

}

Comment #10

Posted on Aug 1, 2011 by Quick Monkey

Got it.

The following works:

    [TestMethod]
    public void TestMethod1()
    {
        var mockMyClass = new Mock<MyClass>(MockBehavior.Strict);
        Key retKey = default(Key);
        Value retVal = default(Value);
        var testKey = new Key();
        var testVal = new Value();

        mockMyClass.SetupSet(x => x[testKey] = testVal).Callback<Key, Value>(
            (k, v) =>
            {
                retKey = k;
                retVal = v;
            });

        mockMyClass.Object[testKey] = testVal;

        Assert.AreEqual(testVal, retVal);

    }

Status: Accepted

Labels:
Type-Defect Priority-Medium Milestone-Release4.5