Пример #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
            });
        }
Пример #2
0
        /// <summary>
        /// Create an expectation on this mock for this action to occur
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="mock">The mock.</param>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        public static IMethodOptions <R> Expect <T, R>(this T mock, Function <T, R> action)
            where T : class
        {
            if (mock == null)
            {
                throw new ArgumentNullException("mock", "You cannot mock a null instance");
            }

            IMockedObject  mockedObject   = MockRepository.GetMockedObject(mock);
            MockRepository mocks          = mockedObject.Repository;
            var            isInReplayMode = mocks.IsInReplayMode(mock);

            mocks.BackToRecord(mock, BackToRecordOptions.None);
            action(mock);
            IMethodOptions <R> options = LastCall.GetOptions <R>();

            options.TentativeReturn();
            if (isInReplayMode)
            {
                mocks.ReplayCore(mock, false);
            }
            return(options);
        }