/// <summary> /// Resets the state of the factory. /// </summary> /// <remarks> /// Use this method after expected exceptions. /// </remarks> public void ClearExpectations() { _currentMockObjectFactory = new CastleMockObjectFactory(); //ReflectiveMockObjectFactory(); // _expectations = new UnorderedExpectationList(null); _thrownUnexpectedInvocationException = null; FirstIncompleteExpectationException = null; }
/// <summary> /// Dispatches the specified invocation. /// </summary> /// <param name="invocation">The invocation.</param> internal void Dispatch(Invocation invocation) { if (!_expectations.IsValid) { var expectation = _expectations.First(_ => !_.IsValid); FirstIncompleteExpectationException = new IncompleteExpectationException(expectation); throw FirstIncompleteExpectationException; } if (!_expectations.Perform(invocation)) { var mock = invocation.Receiver as IMockObject; if (mock != null && mock.IgnoreUnexpectedInvocations && invocation.Method.ReturnType != typeof(void)) { throw new ExpectationException(string.Format("The method '{0}' requires a return value and can not be ignored using the IgnoreUnexpectedInvocations setting.", invocation.MethodSignature)); } FailUnexpectedInvocation(invocation); } }
/// <summary> /// Verifies that all expectations have been met. /// Will be called in <see cref="Dispose"/>, too. /// </summary> /// <param name="rethrowThrownExceptions">A value indicating if exceptions that have already been thrown should be thrown again.</param> public void VerifyAllExpectationsHaveBeenMet(bool rethrowThrownExceptions) { //if (_suppressExpectations) //{ // _suppressExpectations = false; // return; //should this ClearExpectations(); //} if (!rethrowThrownExceptions && _thrownUnexpectedInvocationException != null) { return; //should this ClearExpectations(); } //if (thrownUnexpectedInvocationException != null) // return; //check for ordering exceptions if (rethrowThrownExceptions && FirstIncompleteExpectationException != null) { Exception exceptionToBeRethrown = FirstIncompleteExpectationException; FirstIncompleteExpectationException = null; // only rethrow once ClearExpectations(); throw exceptionToBeRethrown; } // check for swallowed exception if (rethrowThrownExceptions && _thrownUnexpectedInvocationException != null) { Exception exceptionToBeRethrown = _thrownUnexpectedInvocationException; _thrownUnexpectedInvocationException = null; // only rethrow once ClearExpectations(); throw exceptionToBeRethrown; } if (!_expectations.HasBeenMet && _expectations.IsValid) { FailUnmetExpectations(); } }