public void ShouldThrowMockExceptionWhenExactSequenceIsNotMatched()
        {
            var strictLaterSequence = new StrictSequenceStartingAnywhere();

              var a = new Mock<IFoo> {CallSequence = strictLaterSequence};
              var b = new Mock<IFoo> {CallSequence = a.CallSequence};

              b.Object.Do(200);

              Assert.Throws<MockException>(
            () => strictLaterSequence.Verify(a.CallTo(x => x.Do(100))));
        }
        public void ShouldNotSupportCyclicSequencing()
        {
            var strictLaterSequence = new StrictSequenceStartingAnywhere();
              var a = new Mock<IFoo>() { CallSequence = strictLaterSequence };
              var b = new Mock<IFoo>() { CallSequence = a.CallSequence };

              a.Object.Do(200);
              b.Object.Do(100);

              Assert.Throws<MockException>(
            () => strictLaterSequence.Verify(a.CallTo(m => m.Do(100))));
        }
        public void ShouldPassVerificationOfCallsMadeExactlyInTheVerifiedSequence()
        {
            var strictLaterSequence = new StrictSequenceStartingAnywhere();
              var a = new Mock<IFoo>() { CallSequence = strictLaterSequence };
              var b = new Mock<IFoo>() { CallSequence = a.CallSequence };

              a.Object.Do(100);
              b.Object.Do(200);

              strictLaterSequence.Verify(
            a.CallTo(x => x.Do(100)),
            b.CallTo(x => x.Do(200))
              );
        }
        public void ShouldAllowAMixOfSequencedAndNonSequencedMocks()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock2 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock3 = new Mock<RoleWithSingleSimplestMethod>();

              mock1.Object.Do();
              mock3.Object.Do();
              mock2.Object.Do();

              sequence.Verify(
            mock1.CallTo(a => a.Do()),
            mock2.CallTo(b => b.Do())
              );
        }
        public void ShouldIgnoreCallsInbetweenMatchingCalls()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock2 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock3 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };

              mock1.Object.Do();
              mock3.Object.Do();
              mock2.Object.Do();

              sequence.Verify(
            mock1.CallTo(m => m.Do()),
            mock2.CallTo(m => m.Do())
              );
        }
        public void ShouldNotSupportCyclicSequencing()
        {
            var sequence = new CallSequence(MockBehavior.Strict);

              var a = new Mock<IFoo>() { CallSequence = sequence };
              var b = new Mock<IFoo>() { CallSequence = sequence };

              a.Object.Do(200);
              b.Object.Do(100);

              Assert.Throws<MockException>(() =>
                                   sequence.Verify(
                                     a.CallTo(m => m.Do(100)),
                                     b.CallTo(m => m.Do(200))
                                     )
            );
        }
        public void ShouldMatchCallsInTheRightSequenceButMadeLater()
        {
            var sequence = new StrictSequenceStartingAnywhere();
              var a = new Mock<IFoo>() { CallSequence = sequence };
              var b = new Mock<IFoo>() { CallSequence = sequence };
              var c = new Mock<IFoo>() { CallSequence = sequence };
              var d = new Mock<IFoo>() { CallSequence = sequence };

              a.Object.Do(2);
              b.Object.Do(1);
              c.Object.Do(2);
              d.Object.Do(3);

              sequence.Verify(
            b.CallTo(m => m.Do(1)),
            c.CallTo(m => m.Do(2))
            );
        }
        public void ShouldPassVerificationOfCallsMadeExactlyInTheVerifiedSequence()
        {
            var strictSequence = new CallSequence(MockBehavior.Strict);
              var a = new Mock<IFoo>()
              {
            CallSequence = strictSequence
              };
              var b = new Mock<IFoo>()
              {
            CallSequence = strictSequence
              };

              a.Object.Do(100);
              b.Object.Do(200);

              strictSequence.Verify(
            a.CallTo(x => x.Do(100)),
            b.CallTo(x => x.Do(200))
              );
        }
        public void ShouldThrowExceptionWhenVerifyingCallOrderOnMockWithNullSequence()
        {
            var sequence = CallSequence.None();
              var mock1 = new Mock<RoleWithSingleSimplestMethod>()
              {
            CallSequence = sequence
              };

              var mock2 = new Mock<RoleWithSingleSimplestMethod>()
              {
            CallSequence = sequence
              };

              mock1.Object.Do();
              mock2.Object.Do();

              Assert.Throws<NoSequenceAssignedException>(
            () => sequence.Verify(mock1.CallTo(m => m.Do())
               ));
        }
        public void ShouldThrowMockExceptionWhenExactSequenceIsNotMatched()
        {
            var strictSequence = new CallSequence(MockBehavior.Strict);

              var a = new Mock<IFoo>()
              {
            CallSequence = strictSequence
              };

              var b = new Mock<IFoo>()
              {
            CallSequence = strictSequence
              };

              b.Object.Do(200);

              Assert.Throws<MockException>(() =>
                                   strictSequence.Verify(
                                     a.CallTo(m => m.Do(100))
                                   )
            );
        }
        public void ShouldThrowExceptionWhenCallsAreNotMadeInOrderInCallSequence()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock2 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };

              mock2.Object.Do();
              mock1.Object.Do();

              Assert.Throws<MockException>(() =>
            sequence.Verify(
              mock1.CallTo(m => m.Do()),
              mock2.CallTo(m => m.Do())
            )
              );
        }
        public void ShouldSupportMockChainingWithUnmatchingCalls()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleForRecursiveMocking>()
              {
            CallSequence = sequence,
            DefaultValue = DefaultValue.Mock
              };

              mock1.Object.Do().Do();

              sequence.Verify(
            mock1.CallTo(m => m.Do().Do())
            );

              Assert.Throws<MockException>(
            () => sequence.Verify(
              mock1.CallTo(m => m.Do().Do()),
              mock1.CallTo(m => m.Do())
              )
            );
        }
        public void ShouldRecognizeSequenceOfCallsMadeOnTheSameMock()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithArgumentAndReturnValue> {CallSequence = sequence};

              mock1.Object.Do(1);
              mock1.Object.Do(2);
              mock1.Object.Do(3);

              sequence.Verify(
            mock1.CallTo(m => m.Do(2)),
            mock1.CallTo(m => m.Do(3))
            );

              Assert.Throws<MockException>(
            () => sequence.Verify(
              mock1.CallTo(m => m.Do(2)),
              mock1.CallTo(m => m.Do(3)),
              mock1.CallTo(m => m.Do(1))
              )
            );
        }
        public void ShouldProperlyRecognizeNonMatchingArgumentsInSequentialVerification()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithArgumentAndReturnValue> {CallSequence = sequence};
              var mock2 = new Mock<RoleWithArgumentAndReturnValue> {CallSequence = sequence};

              mock2.Object.Do(1);
              mock1.Object.Do(2);
              mock2.Object.Do(3);

              Assert.Throws<MockException>(() =>
            sequence.Verify(
              mock1.CallTo(m => m.Do(2)),
              mock2.CallTo(m => m.Do(1))
              )
            );
        }
        public void ShouldNotThrowAnyExceptionWhenCallsAreMadeInOrderInCallSequence()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock2 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };

              mock1.Object.Do();
              mock2.Object.Do();

              sequence.Verify(
            mock1.CallTo(m => m.Do()),
            mock2.CallTo(m => m.Do())
              );
        }
        public void ShouldIgnoreCallsInbetweenUnmatchingCalls()
        {
            var sequence = new CallSequence();
              var mock1 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock2 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };
              var mock3 = new Mock<RoleWithSingleSimplestMethod> { CallSequence = sequence };

              mock2.Object.Do();
              mock3.Object.Do();
              mock1.Object.Do();

              Assert.Throws<MockException>(() =>
            sequence.Verify(
              mock1.CallTo(m => m.Do()),
              mock2.CallTo(m => m.Do())
            )
              );
        }
        public void ShouldThrowWhenMatchedSequenceIsInterruptedByOtherCalls()
        {
            var sequence = new StrictSequenceStartingAnywhere();
              var a = new Mock<IFoo> { CallSequence = sequence };
              var b = new Mock<IFoo> { CallSequence = sequence };
              var c = new Mock<IFoo> { CallSequence = sequence };
              var d = new Mock<IFoo> { CallSequence = sequence };

              a.Object.Do(2);
              b.Object.Do(1);
              d.Object.Do(3);
              c.Object.Do(2);

              Assert.Throws<MockException>(
            () =>
              sequence.Verify(
            b.CallTo(m => m.Do(1)),
            c.CallTo(m => m.Do(2))
              )
            );
        }