/// <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> /// Throws an exception indicating that the specified invocation is not expected. /// </summary> /// <param name="invocation">The invocation.</param> private void FailUnexpectedInvocation(Invocation invocation) { var writer = new DescriptionWriter(); //var unexpectedWriter = new DescriptionWriter(); var expectedWriter = new DescriptionWriter(); //((ISelfDescribing)invocation).DescribeTo(unexpectedWriter); _expectations.DescribeTo(expectedWriter); //expectations.DescribeActiveExpectationsTo(expectedWriter); string theexpectations = expectedWriter.ToString().Replace(EqualMatcher.EQUAL_TO, string.Empty); //if (theexpectations.Contains(unexpectedWriter.ToString())) //{ // writer.WriteLine(); // writer.WriteLine("What happened? An expectation listed below is similar to the unexpected call above."); // writer.WriteLine("Why did this happen? A parameter reference in your expectation refers to a different instance than the reference used in the call."); // writer.WriteLine("How to fix this: Use a matcher, override .Equals() on the class, or adjust your API."); // writer.WriteLine(); //} if (theexpectations.Contains(", returning an invoker") && invocation.Method.IsEvent()) { writer.WriteLine(); writer.WriteLine("If this exception was unexpected, the delegates used in the expectation and invocation may be different. To bind to *any* delegate, use 'null' in the expectation."); writer.WriteLine(); } // try catch to get exception with stack trace. try { //throw new UnexpectedInvocationException(invocation, topOrdering, writer.ToString()); throw new UnexpectedInvocationException(this, invocation, _expectations.Root, writer.ToString()); } catch (UnexpectedInvocationException e) { // remember only first exception if (_thrownUnexpectedInvocationException == null) { _thrownUnexpectedInvocationException = e; } //always rethrow the first exception (that could have caused the rest i.e. a dispose to be called when it shouldn't have) throw _thrownUnexpectedInvocationException; } }
/// <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(); } }
/// <summary> /// Clears thrown unexpected exceptions so that a new exception will be thrown. /// </summary> public void ClearException() { _thrownUnexpectedInvocationException = null; }