Moq - 验证 returns 为零,即使我可以看到正在调用的方法

Moq - verify returns zero even though I can see the methods being called

我正在尝试验证两个方法是否仅被调用一次,但模拟一直告诉我它们被调用的实际次数为零:

Moq.MockException: 
Expected invocation on the mock exactly 1 times, but was 0 times: x => x.Map(.OrderType)
No setups configured.
No invocations performed.
   at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
   at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
   at Moq.Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
   at Moq.Mock`1.Verify[TResult](Expression`1 expression, Times times)
   at Ylp.Import.Sandwell.Tests.Factories.SandwellOperatingHoursFactoryTests.WhenInputValid.CallsRestrictionTypeMapper()

代码:

class SandwellOperatingHoursFactoryTestBase
    {
        protected OperatingHoursCms OperatingHoursCms;
        protected virtual Mock<IMap<string, RuleTimeCms>> RuleTimeMapperMock => new Mock<IMap<string, RuleTimeCms>>();
        protected virtual Mock<IMap<string, UkRestrictionTypes.Entry>> RestrictionTypeMapperMock => new Mock<IMap<string, UkRestrictionTypes.Entry>>();

        protected virtual string OrderType => null;
        protected virtual string TimesOfEnforcement => null;

        protected RuleTimeCms ReturnedRuleTimeCms => null;
        protected UkRestrictionTypes.Entry ReturnedRestrictionTypeEntry => null;

    }

    internal class SandwellOperatingHoursFactoryCreateTestBase: SandwellOperatingHoursFactoryTestBase
    {
        [TestInitialize]
        public void Initialize()
        {
            RestrictionTypeMapperMock.Setup(x => x.Map(OrderType)).Returns(ReturnedRestrictionTypeEntry);
            RuleTimeMapperMock.Setup(x => x.Map(TimesOfEnforcement)).Returns(ReturnedRuleTimeCms);


            OperatingHoursCms = new SandwellOperatingHoursFactory(RuleTimeMapperMock.Object,RestrictionTypeMapperMock.Object).Create(OrderType,TimesOfEnforcement);
        }
    }

    [TestClass]
    internal class WhenInputValid: SandwellOperatingHoursFactoryCreateTestBase
    {
        [TestMethod]
        public void CallsRuleTimeMapper()
        {
            RuleTimeMapperMock.Verify(x=>x.Map(TimesOfEnforcement),Times.Once);
        }

        [TestMethod]
        public void CallsRestrictionTypeMapper()
        {
            RestrictionTypeMapperMock.Verify(x => x.Map(OrderType), Times.Exactly(1));
        }
    }

    class SandwellOperatingHoursFactory
    {
        private IMap<string, RuleTimeCms> _ruleTimeMapper;
        private IMap<string, UkRestrictionTypes.Entry> _restrictionTypeMapper;
        internal SandwellOperatingHoursFactory(
            IMap<string, RuleTimeCms> ruleTimeMapper, 
            IMap<string, UkRestrictionTypes.Entry> restrictionTypeMapper)
        {
            _ruleTimeMapper = ruleTimeMapper;
            _restrictionTypeMapper = restrictionTypeMapper;
        }

        public OperatingHoursCms Create(string orderType, string timesOfEnforcement)
        {
            var ruleTime = _ruleTimeMapper.Map(timesOfEnforcement);
            var restrictionType = _restrictionTypeMapper.Map(orderType);

            return null;
        }
    }

可能您用于设置的值与您实际调用它的值不匹配。

尝试替换

 RestrictionTypeMapperMock.Setup(x => x.Map(OrderType)).Returns(ReturnedRestrictionTypeEntry);

 RestrictionTypeMapperMock.Setup(x => x.Map(It.IsAny<string>())).Returns(ReturnedRestrictionTypeEntry);

你有:

protected virtual Mock<IMap<string, UkRestrictionTypes.Entry>> RestrictionTypeMapperMock
  => new Mock<IMap<string, UkRestrictionTypes.Entry>>();

箭头 => 属性 get 访问器有新的 C# 6 语法吗?

请问你的属性return一个newMock<>每次get 存取器运行?

如果我的问题的答案是 "yes",您将 Setup 一个 Mock<> 的实例,然后在 另一个 [=] 上得到 .Object 33=] 实例(您使用非严格的模拟,因此即使没有在第二个实例上进行任何设置,也可以调用 Map 之类的方法)。最后,在 Mock<> 的第三个 实例上,一个从未被使用过的实例,你调用 Verify 来查看是否使用了方法。