Moq - 验证返回零,即使我可以看到被调用的方法

时间:2015-10-12 17:37:26

标签: c# moq

我正在尝试验证两个方法只被调用一次,但我一直被模拟告知它们被调用的实际时间为零:

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;
        }
    }

2 个答案:

答案 0 :(得分:1)

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

尝试替换

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

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

答案 1 :(得分:1)

你有:

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

该箭头=>是否有属性get访问者的新C#6语法?

每次Mock<>访问者运行时,您的媒体资源会返回 get吗?

如果我的问题的答案为“是”,您将Setup Mock<>的一个实例,然后在另一个实例上获取.Object(您使用非严格的模拟,所以即使没有在第二个实例上进行任何设置,也可以调用Map之类的方法。最后,在Mock<>第三个​​实例(从未使用过的实例)上,调用Verify来查看是否使用了方法。