Example #1
0
        private static ExpectationVerificationInformation GetExpectationsToVerify <T>(T mock, Action <T> action,
                                                                                      Action <IMethodOptions <object> >
                                                                                      setupConstraints)
        {
            IMockedObject  mockedObject = MockRepository.GetMockedObject(mock);
            MockRepository mocks        = mockedObject.Repository;

            if (mocks.IsInReplayMode(mockedObject) == false)
            {
                throw new InvalidOperationException(
                          "Cannot assert on an object that is not in replay mode. Did you forget to call ReplayAll() ?");
            }

            var mockToRecordExpectation =
                (T)mocks.DynamicMock(FindAppropriteType <T>(mockedObject), mockedObject.ConstructorArguments);

            action(mockToRecordExpectation);

            AssertExactlySingleExpectaton(mocks, mockToRecordExpectation);

            IMethodOptions <object> lastMethodCall = mocks.LastMethodCall <object>(mockToRecordExpectation);

            lastMethodCall.TentativeReturn();
            if (setupConstraints != null)
            {
                setupConstraints(lastMethodCall);
            }
            ExpectationsList expectationsToVerify = mocks.Replayer.GetAllExpectationsForProxy(mockToRecordExpectation);

            if (expectationsToVerify.Count == 0)
            {
                throw new InvalidOperationException(
                          "The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()");
            }
            IExpectation           expected             = expectationsToVerify[0];
            ICollection <object[]> argumentsForAllCalls = mockedObject.GetCallArgumentsFor(expected.Method);

            return(new ExpectationVerificationInformation
            {
                ArgumentsForAllCalls = new List <object[]>(argumentsForAllCalls),
                Expected = expected
            });
        }