Skip to content

MockRepository.Verify() also verifies mocks created outside of the repo #1018

@matkoliptak

Description

@matkoliptak

Since 4.14, calling MockRepository.VerifyAll() also verifies mocks created outside of the repo but returned from a mocked method. In 4.13.1, the following Test passes, in 4.14 if fails because the IProduct.GetString() was not called. I never wanted to verify that IProduct.GetString() was called, this is why I created the IProduct mock by new Mock rather than repo.Create. I believe this might be a bug.

Expected behavior: the Test below passed.
Actual behavior: the TEst below fails with the following output:


Moq.MockException : The mock repository failed verification due to the following:

   Mock<TestClosure.IFactory:1>:
   This mock failed verification due to the following:
   
      TestClosure.IFactory m => m.Create():
      
         Mock<TestClosure.IProduct:1>:
         This mock failed verification due to the following:
         
            TestClosure.IProduct m => m.GetString():
            This setup was not matched.
   v Moq.MockFactory.VerifyMocks(Action`1 verifyAction)
   v Moq.MockFactory.VerifyAll()
   v MoqBug.TestClosure.Test() 
    public class TestClosure
    {
        public interface IFactory
        {
            IProduct Create();
        }

        public interface IProduct
        {
            string GetString();
        }

        public class FactoryCaller
        {
            private readonly IFactory _factory;

            public FactoryCaller(IFactory factory)
            {
                _factory = factory;
            }

            public void CallFactory()
            {
                _factory.Create();
            }
        }

        [Test]
        public void Test()
        {
            var repo = new MockRepository(MockBehavior.Loose);
            var factoryMock = repo.Create<IFactory>();
            
            var productMock = new Mock<IProduct>();                 // created out of the mock repo
            productMock.Setup(m => m.GetString()).Returns("hey");   // not really expected to be called, setting up "just in case"
            
            factoryMock.Setup(m => m.Create()).Returns(productMock.Object);

            var sut = new FactoryCaller(factoryMock.Object);
            sut.CallFactory();

            repo.VerifyAll();
        }
    }

Back this issue
Back this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions