/// <summary> /// Reset the selected expectation on this mock object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mock">The mock.</param> /// <param name="options">The options to reset the expectations on this mock.</param> public static void BackToRecord <T>(this T mock, BackToRecordOptions options) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); var mocks = mockedObject.Repository; mocks.BackToRecord(mock, options); }
/// <summary> /// Cause the mock state to change to replay, any further call is compared to the /// ones that were called in the record state. /// </summary> /// <param name="mock">the mocked object to move to replay state</param> public static void Replay <T>(this T mock) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); var mocks = mockedObject.Repository; if (mocks.IsInReplayMode(mock) != true) { mocks.Replay(mockedObject); } }
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 }); }
/// <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); }
/* * Method: On * Gets the method options for the last call for mockedInstance. * This is the recommended approach for multi threaded scenarios. * * Expected usage: * (start code) * LastCall.On(mockObj).Return(4); * (end) * * Thread safety: * This method is safe to use in multi threading scenarios. */ /// <summary> /// Allows to get an interface to work on the last call. /// </summary> /// <param name="mockedInstance">The mocked object</param> /// <returns>Interface that allows to set options for the last method call on this object</returns> public static IMethodOptions <object> On(object mockedInstance) { IMockedObject mockedObj = MockRepository.GetMockedObject(mockedInstance); return(mockedObj.Repository.LastMethodCall <object>(mockedInstance)); }
/* * Method: On * Get the method options for the last method call on the mockInstance. * Unless you're recording in multiply threads, you are probably better off * using <Call> * * Expected usage: * Expect.On(mockList).Call(mockList.Count).Return(50); * * Thread safety: * This method can be used in mutli threading scenarios. */ /// <summary> /// Get the method options for the last method call on the mockInstance. /// </summary> public static ICreateMethodExpectation On(object mockedInstace) { IMockedObject mockedObject = MockRepository.GetMockedObject(mockedInstace); return(new CreateMethodExpectation(mockedObject, mockedInstace)); }
/// <summary> /// Verifies all expectations on this mock object /// </summary> /// <param name="mockObject">The mock object.</param> public static void VerifyAllExpectations(this object mockObject) { IMockedObject mockedObject = MockRepository.GetMockedObject(mockObject); mockedObject.Repository.Verify(mockedObject); }
/// <summary> /// Gets the mock repository for this specificied mock object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mock">The mock.</param> /// <returns></returns> public static MockRepository GetMockRepository <T>(this T mock) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); return(mockedObject.Repository); }