RajOpteamix / moq

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

Mocking Indexer using Strict Behaviour #320

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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;});

Original issue reported on code.google.com by Greg.Ne...@gmail.com on 1 Aug 2011 at 4:41

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I don't understand the error report.

mock.SetupSet(x=>x[It.IsAny<Key>() = It.IsAny<Value>())

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

Original comment by dan...@cazzulino.com on 1 Aug 2011 at 7:04

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The problem is that in C# you cannot use open generics anywhere :P.

Can't fix the compiler in Moq ;)

Original comment by dan...@cazzulino.com on 1 Aug 2011 at 7:13

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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.

Original comment by dan...@cazzulino.com on 1 Aug 2011 at 7:28

GoogleCodeExporter commented 9 years ago
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<MyClass>();
            Key retKey = default(Key);
            Value retVal =  default(Value);
            mockMyClass.SetupSet(x => x[It.IsAny<Key>()] = It.IsAny<Value>()).Callback<Key, Value>(
                (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
    {

    }
}

Original comment by Greg.Ne...@gmail.com on 1 Aug 2011 at 7:40

GoogleCodeExporter commented 9 years ago
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);

        }

Original comment by dan...@cazzulino.com on 1 Aug 2011 at 8:32