public void ChooseAffirmativeExpetationOnPropertySetter()
        {
            var mock = new Mock <IFoo>();

            var when     = true;
            var positive = false;
            var negative = false;

            mock.When(() => when).SetupSet(x => x.Value  = "foo").Callback(() => positive = true);
            mock.When(() => !when).SetupSet(x => x.Value = "foo").Callback(() => negative = true);

            mock.Object.Value = "foo";

            Assert.True(positive);
            Assert.False(negative);

            when              = false;
            positive          = false;
            mock.Object.Value = "foo";

            Assert.False(positive);
            Assert.True(negative);

            when              = true;
            negative          = false;
            mock.Object.Value = "foo";

            Assert.True(positive);
            Assert.False(negative);
        }
        public void ChooseAffirmativeExpetationOnVoidMethod()
        {
            var mock = new Mock <IFoo>();

            var when     = true;
            var positive = false;
            var negative = false;

            mock.When(() => when).Setup(x => x.Bar()).Callback(() => positive  = true);
            mock.When(() => !when).Setup(x => x.Bar()).Callback(() => negative = true);

            mock.Object.Bar();

            Assert.True(positive);
            Assert.False(negative);

            when     = false;
            positive = false;
            mock.Object.Bar();

            Assert.False(positive);
            Assert.True(negative);

            when     = true;
            negative = false;
            mock.Object.Bar();

            Assert.True(positive);
            Assert.False(negative);
        }
        public void ShouldSupportMultipleMethod()
        {
            var target = Mocks.Of <IBaz>().First(x => !x.HasElements("1") && x.HasElements("2"));

            Assert.False(target.HasElements("1"));
            Assert.True(target.HasElements("2"));
        }
Example #4
0
        public void MockObjectsEqualityIsReferenceEquals()
        {
            var mock1 = new Mock <IFoo>();
            var mock2 = new Mock <IFoo>();

            Assert.True(mock1.Object.Equals(mock1.Object));
            Assert.False(mock1.Object.Equals(mock2.Object));
        }
        public void NeverChecksZeroTimes()
        {
            var target = Times.Never();

            Assert.False(target.Verify(-1));
            Assert.True(target.Verify(0));
            Assert.False(target.Verify(1));
            Assert.False(target.Verify(int.MaxValue));
        }
        public void OnceChecksOneTime()
        {
            var target = Times.Once();

            Assert.False(target.Verify(-1));
            Assert.False(target.Verify(0));
            Assert.True(target.Verify(1));
            Assert.False(target.Verify(int.MaxValue));
        }
Example #7
0
        public void DoesNotCallBaseClassVirtualImplementationByDefault()
        {
            var mock = new Mock <FooBase>();

            Assert.False(mock.Object.BaseCalled);
            mock.Object.BaseCall();

            Assert.False(mock.Object.BaseCalled);
        }
Example #8
0
        public void UsesCustomMatcherWithArgument()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(m => m.Do(Between(1, 5, Range.Inclusive))).Returns(true);

            Assert.False(mock.Object.Do(6));
            Assert.True(mock.Object.Do(1));
            Assert.True(mock.Object.Do(5));
        }
        public void AtMostOnceRangesBetweenZeroAndOne()
        {
            var target = Times.AtMostOnce();

            Assert.False(target.Verify(-1));
            Assert.True(target.Verify(0));
            Assert.True(target.Verify(1));
            Assert.False(target.Verify(5));
            Assert.False(target.Verify(int.MaxValue));
        }
Example #10
0
        public void MatchesSameReference()
        {
            var a = new object();
            var b = new object();

            var matcher = new RefMatcher(a);

            Assert.True(matcher.Matches(a));
            Assert.False(matcher.Matches(b));
        }
        public void AtLeastRangesBetweenTimesAndMaxValue()
        {
            var target = Times.AtLeast(10);

            Assert.False(target.Verify(-1));
            Assert.False(target.Verify(0));
            Assert.False(target.Verify(9));
            Assert.True(target.Verify(10));
            Assert.True(target.Verify(int.MaxValue));
        }
        public void DoesntMatchIfNotAssignableType()
        {
            var expr = ToExpression <IFormatProvider>(() => It.IsAny <IFormatProvider>()).ToLambda().Body;

            var matcher = MatcherFactory.CreateMatcher(expr, false);

            matcher.Initialize(expr);

            Assert.False(matcher.Matches("foo"));
        }
        public void AtMostRangesBetweenZeroAndTimes()
        {
            var target = Times.AtMost(10);

            Assert.False(target.Verify(-1));
            Assert.True(target.Verify(0));
            Assert.True(target.Verify(6));
            Assert.True(target.Verify(10));
            Assert.False(target.Verify(11));
            Assert.False(target.Verify(int.MaxValue));
        }
        public void ShouldRaiseEventIfAttachedAfterUse()
        {
            var view      = new Mock <IFooView>();
            var presenter = new FooPresenter(view.Object);

            Assert.False(presenter.Canceled);

            view.Raise(v => v.Canceled += null, EventArgs.Empty);

            Assert.True(presenter.Canceled);
        }
        public void ExactlyCheckExactTimes()
        {
            var target = Times.Exactly(10);

            Assert.False(target.Verify(-1));
            Assert.False(target.Verify(0));
            Assert.False(target.Verify(9));
            Assert.True(target.Verify(10));
            Assert.False(target.Verify(11));
            Assert.False(target.Verify(int.MaxValue));
        }
Example #16
0
        public void OverridesBehaviorFromAbstractClass()
        {
            var mock = new Mock <FooBase>();

            mock.CallBase = true;

            mock.Setup(x => x.Check("foo")).Returns(false);

            Assert.False(mock.Object.Check("foo"));
            Assert.True(mock.Object.Check("bar"));
        }
        public void ShouldExpectAddGenericHandler()
        {
            var view      = new Mock <IFooView>();
            var presenter = new FooPresenter(view.Object);

            Assert.False(presenter.Canceled);

            view.Raise(v => v.Canceled += null, EventArgs.Empty);

            Assert.True(presenter.Canceled);
        }
        public void BetweenExclusiveRangesBetweenFromPlusOneAndToMinusOne()
        {
            var target = Times.Between(10, 20, Range.Exclusive);

            Assert.False(target.Verify(0));
            Assert.False(target.Verify(10));
            Assert.True(target.Verify(11));
            Assert.True(target.Verify(14));
            Assert.True(target.Verify(19));
            Assert.False(target.Verify(20));
            Assert.False(target.Verify(int.MaxValue));
        }
Example #19
0
            public void WhenQueryingWithNoValue_ThenAlwaysHasPropertyStubBehavior()
            {
                var foo = Mock.Of <IFoo>();

                foo.IsValid = true;

                Assert.True(foo.IsValid);

                foo.IsValid = false;

                Assert.False(foo.IsValid);
            }
        public void BetweenInclusiveRangesBetweenFromAndTo()
        {
            var target = Times.Between(10, 20, Range.Inclusive);

            Assert.False(target.Verify(0));
            Assert.False(target.Verify(9));
            Assert.True(target.Verify(10));
            Assert.True(target.Verify(14));
            Assert.True(target.Verify(20));
            Assert.False(target.Verify(21));
            Assert.False(target.Verify(int.MaxValue));
        }
Example #21
0
        public void DoesNotCallBaseClassVirtualImplementationIfSpecified()
        {
            var mock = new Mock <FooBase>();

            mock.CallBase = false;

            Assert.False(mock.Object.BaseCalled);
            mock.Object.BaseCall();

            Assert.Equal(default(bool), mock.Object.BaseCall("foo"));
            Assert.False(mock.Object.BaseCalled);
        }
Example #22
0
        public void CallbackCalledAfterReturnsCallWithArg()
        {
            var  mock          = new Mock <IFoo>();
            bool returnsCalled = false;

            mock.Setup(foo => foo.Execute(It.IsAny <string>()))
            .Callback <string>(s => Assert.False(returnsCalled))
            .Returns(() => { returnsCalled = true; return("ack"); })
            .Callback <string>(s => Assert.True(returnsCalled));

            mock.Object.Execute("ping");

            Assert.True(returnsCalled);
        }
Example #23
0
        public void MatchesDifferentOverloadsWithItIsAny()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(foo => foo.DoTypeOverload(It.IsAny <Bar>()))
            .Returns(true);
            mock.Setup(foo => foo.DoTypeOverload(It.IsAny <Baz>()))
            .Returns(false);

            bool bar = mock.Object.DoTypeOverload(new Bar());
            bool baz = mock.Object.DoTypeOverload(new Baz());

            Assert.True(bar);
            Assert.False(baz);
        }
Example #24
0
        public void FillingDoesNotRemoveIfNotEnoughInStock()
        {
            //setup - data
            var order = new Order(TALISKER, 50);
            var mock  = new Mock <IWarehouse>();

            //setup - expectations
            mock.Setup(x => x.HasInventory(It.IsAny <string>(), It.IsInRange(0, 100, Range.Inclusive))).Returns(false);
            mock.Setup(x => x.Remove(It.IsAny <string>(), It.IsAny <int>())).Throws(new InvalidOperationException());

            //exercise
            order.Fill(mock.Object);

            //verify
            Assert.False(order.IsFilled);
        }
        public void DoesNotRaiseEventOnSubObject()
        {
            var mock = new Mock <IParent> {
                DefaultValue = DefaultValue.Mock
            };

            bool raised = false;

            mock.Object.Adder.Added += (sender, args) => raised = true;

            Assert.Same(mock.Object.Adder, mock.Object.Adder);

            mock.Raise(p => p.Adder.Added += null, EventArgs.Empty);

            Assert.False(raised);
        }
Example #26
0
        public void CallbackCalledAfterReturnsCall()
        {
            var  mock          = new Mock <IFoo>();
            bool returnsCalled = false;
            bool beforeCalled  = false;
            bool afterCalled   = false;

            mock.Setup(foo => foo.Execute("ping"))
            .Callback(() => { Assert.False(returnsCalled); beforeCalled = true; })
            .Returns(() => { returnsCalled = true; return("ack"); })
            .Callback(() => { Assert.True(returnsCalled); afterCalled = true; });

            Assert.Equal("ack", mock.Object.Execute("ping"));

            Assert.True(beforeCalled);
            Assert.True(afterCalled);
        }
Example #27
0
        public void ShouldCreateMocksWithFactoryCallBase()
        {
            var repository = new MockRepository(MockBehavior.Loose);

            var mock = repository.Create <BaseClass>();

            mock.Object.BaseMethod();

            Assert.False(mock.Object.BaseCalled);

            repository.CallBase = true;

            mock = repository.Create <BaseClass>();

            mock.Object.BaseMethod();

            Assert.True(mock.Object.BaseCalled);
        }
Example #28
0
        public void ShouldVerifyVerifiables()
        {
            try
            {
                var repository = new MockRepository(MockBehavior.Default);
                var mock       = repository.Create <IFoo>();

                mock.Setup(foo => foo.Do());
                mock.Setup(foo => foo.Undo()).Verifiable();

                repository.Verify();
            }
            catch (MockException mex)
            {
                Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
                Expression <Action <IFoo> > doExpr = foo => foo.Do();
                Assert.False(mex.Message.Contains(doExpr.ToString()));
            }
        }
        public void ShouldAllowListenerListToBeModifiedDuringEventHandling()
        {
            var parent = new Mock <IParent>(MockBehavior.Strict);

            parent.Object.Event += delegate
            {
                parent.Object.Event += delegate { raisedField = true; };
            };

            parent.Raise(p => p.Event += null, EventArgs.Empty);

            // we don't expect the inner event to be raised the first time
            Assert.False(raisedField);

            // the second time around, the event handler added the first time
            // should kick in
            parent.Raise(p => p.Event += null, EventArgs.Empty);

            Assert.True(raisedField);
        }
        public void ShouldAttachAndDetachListenerMethod()
        {
            var parent = new Mock <IParent>(MockBehavior.Strict);

            raisedField = false;

            parent.Object.Event += this.OnRaised;

            parent.Raise(p => p.Event += null, EventArgs.Empty);

            Assert.True(raisedField);

            raisedField = false;

            parent.Object.Event -= OnRaised;

            parent.Raise(p => p.Event += null, EventArgs.Empty);

            Assert.False(raisedField);
        }