RajOpteamix / moq

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

function call with array as input parameter could not pass verify. #348

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Sample code is like this, TestArrayVerify fails.

    public interface ITest
    {
        void TestVerifyArray(Array a);
        void TestVerifyInt(int i);
    }

    internal class MyTest : ITest
    {
        public void TestVerifyArray(Array a)
        {

        }

        public void TestVerifyInt(int i)
        {

        }
    }

    internal class Test2
    {
        private ITest myTest;

        public Test2(ITest myTest)
        {
            this.myTest = myTest;
        }

        public void CallTestVerifyArray(bool b)
        {
            Array valueArray = new object[1];
            valueArray.SetValue(null, 0);
            myTest.TestVerifyArray(valueArray);
            if (b)
            {
                valueArray.SetValue("No", 0);
                myTest.TestVerifyArray(valueArray);
            }
        }

        public void CallTestVerifyInt(bool b)
        {
            int i = 0;
            myTest.TestVerifyInt(i);
            if (b)
            {
                i = 1;
                myTest.TestVerifyInt(i);
            }
        }

    }
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {

        }

        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }      

        [TestMethod]
        public void TestArrayVerify()
        {
            Mock<ITest> mock = new Mock<ITest>();
            Test2 test = new Test2(mock.Object);
            Array valueArray = new object[1];

            valueArray.SetValue(null, 0);
            test.CallTestVerifyArray(true);
            mock.Verify(fw => fw.TestVerify(valueArray));

            valueArray.SetValue("No", 0);
            mock.Verify(fw => fw.TestVerify(valueArray),Times.Once());

        }

        [TestMethod]
        public void TestIntVerify()
        {
            Mock<ITest> mock = new Mock<ITest>();
            Test2 test = new Test2(mock.Object);
            int i = 0;
            test.CallTestVerifyInt(true);
            mock.Verify(fw => fw.TestVerifyInt(i));

            i = 1;
            mock.Verify(fw => fw.TestVerifyInt(i));

        }
    }

What is the expected output? What do you see instead?

I hope the first test (TestArrayVerify) can be passed but it failed at the 
first verification, because the TestVerifyArray is called twice in code, and 
seems the array hold by moq is overwritten with last value. so when it verify 
the value set previously, there is no match calls. I mean i can only get the 
second verify passed. But if the parameter is basic type such as int. the test 
(TestIntVerify) will be passed without any problem. I think this is a bug. or 
do i misuse moq?

What version of the product are you using? On what operating system?
4.0.10827.0

Please provide any additional information below.

Original issue reported on code.google.com by yuzi801...@gmail.com on 27 Jul 2012 at 8:48