RajOpteamix / moq

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

Order of calls to Mock Setup affects VerifyAll() success rate #377

Open GoogleCodeExporter opened 9 years ago

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

1. Using the following in MSTest:

public interface IBase { }

public interface IInterface : IBase { }

public interface IWorker
{
    void Work<TClass>() where TClass : class;
}

public static class SomeClass
{
    public static void DoAllWork(IWorker worker)
    {
        worker.Work<IInterface>();
        worker.Work<IBase>();
    }
}

[TestMethod]
public void SampleClass_DoAllWork_MockFailsOnVerifyAll()
{
    var workerMock = new Mock<IWorker>(MockBehavior.Strict);
    // this mock setup order DOESN'T work
    workerMock.Setup(x => x.Work<IInterface>());
    workerMock.Setup(x => x.Work<IBase>());
    SomeClass.DoAllWork(workerMock.Object);

    // throws:
    // The following setups were not matched:
    // IWorker x => x.Work<IInterface>()
    workerMock.VerifyAll();
}

[TestMethod]
public void SampleClass_DoAllWork_MockSucceeds()
{
    var workerMock = new Mock<IWorker>(MockBehavior.Strict);
    // this mock setup order works
    workerMock.Setup(x => x.Work<IBase>());
    workerMock.Setup(x => x.Work<IInterface>());

    SomeClass.DoAllWork(workerMock.Object);

    workerMock.VerifyAll();
}

2. Run each of the tests - one will fail, one will succeed.

What is the expected output? What do you see instead?
I expect that both tests will pass. I am seeing failure on one test during 
VerifyAll as I suspect that Moq is unexpectedly registering two calls to one of 
the Setups and none to the other. This is most likely caused by Moq 
successfully matching my interface declarations by the base interface, which is 
correct EXCEPT for where there is a better match provided. Although the second 
test works as desired, I believe that this is an unintentional coincidence.

What version of the product are you using? On what operating system?
Windows 7 x64
VS Pro 2012 - 11.0.61030.00 Update 4
Moq.4.2.1402.2112

Original issue reported on code.google.com by goodh...@gmail.com on 26 Jul 2014 at 7:42