Пример #1
0
        public Mock(MockBehavior behavior) : base(behavior)
        {
            // setup a default context so that .GetProperty() extension on IBaseMessage mock can be called for property without setup
            _contextMock = new Context.Mock <IBaseMessageContext>(behavior);
            base.Setup(msg => msg.Context).Returns(_contextMock.Object);

            SetupProperty(msg => msg.BodyPart.ContentType);
            // hook GetOriginalDataStream() onto BodyPart.Data, so that it does not fail when BodyPart has a Data stream
            SetupProperty(msg => msg.BodyPart.Data);
            base.Setup(msg => msg.BodyPart.GetOriginalDataStream()).Returns(() => Object.BodyPart.Data);
        }
        public void MoqBugWhereRecursiveMockingOverwritesExplicitSetupIsAscertained()
        {
            var message = new Mock <IBaseMessage> {
                DefaultValue = DefaultValue.Empty
            };
            var context = new Context.Mock <IBaseMessageContext> {
                DefaultValue = DefaultValue.Empty
            };

            message.Setup(m => m.Context).Returns(context.Object);
            message.Object.Context.Should().BeSameAs(context.Object);

            message.Setup(m => m.Context.CountProperties).Returns(0);
            message.Object.Context.Should().BeSameAs(context.Object, "Moq bug has resurfaced as explicit setup being overwritten by recursive mocking feature.");
        }
Пример #3
0
        public Mock(MockBehavior behavior) : base(behavior)
        {
            // setup a default context so that .GetProperty() extension on IBaseMessage mock can be called for property without setup

            // HACK Don't setup a new Context.Mock<> of our own as it would be overwritten by Moq recursive mocking
            // feature. Hence we perform a mock setup that will kick the recursive mocking to instantiate a context mock
            // that will be wrapped and delegated to by our own Context.Mock<>. This must be considered as a bug in Moq.
            base.Setup(msg => msg.Context.AddPredicate(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <object>()));
            // ReSharper disable once VirtualMemberCallInConstructor
            _contextMock = new Context.Mock <IBaseMessageContext>(behavior, Get(Object.Context));

            // the following two lines should have been used instead
            //_contextMock = new Context.Mock<IBaseMessageContext>(behavior);
            //base.Setup(msg => msg.Context).Returns(_contextMock.Object);

            // hook GetOriginalDataStream() onto BodyPart.Data, so that it does not fail when BodyPart has a Data stream
            base.Setup(msg => msg.BodyPart.GetOriginalDataStream()).Returns(() => Object.BodyPart.Data);
        }