Exemple #1
0
        /// <summary>
        /// Verifies this expectation, when the method is called
        /// </summary>
        public void Verify( )
        {
            if (!ActualMethodCallWasSet)
            {
                Assertion.Fail(String.Format(
                                   "{0}({1}) expected but never called.",
                                   ExpectedMethodName,
                                   argsToString(_argumentExpectations)
                                   ));
            }
            if (ExpectedMethodName != ActualMethodName && _expectationType == ExpectationMethodType.Call)
            {
                Assertion.Fail(String.Format(
                                   "{0}({1}) expected, but {2} called.",
                                   ExpectedMethodName, argsToString(_argumentExpectations),
                                   ActualMethodCall
                                   ));
            }
            if (ExpectedMethodName == ActualMethodName && _expectationType == ExpectationMethodType.NoCall)
            {
                Assertion.Fail(String.Format(
                                   "{0}({1}) was not expected.",
                                   ExpectedMethodName, argsToString(_argumentExpectations)
                                   ));
            }

            if (_argumentExpectations == null)
            {
                return;
            }
            object[] actualArguments = ActualMethodCall.Arguments;
            // actual arguments must be equal to expectations
            if (actualArguments.Length != _argumentExpectations.Length)
            {
                Assertion.Fail(String.Format(
                                   "Expected {0} arguments but received {1} " +
                                   "in method call {2}.",
                                   _argumentExpectations.Length,
                                   actualArguments.Length,
                                   ActualMethodCall
                                   ));
            }
            // assert that each passed in arg is validated by the appropriate predicate.
            for (int i = 0; i < _argumentExpectations.Length; i++)
            {
                object argumentExpectation = _argumentExpectations[i];
                object actualArgument      = actualArguments[i];
                // evaluate whether input expectations have been met
                IPredicate predicate =
                    PredicateUtils.ConvertFrom(argumentExpectation);
                bool isPredicateSatisfied =
                    predicate.Eval(actualArgument);
                if (!isPredicateSatisfied)
                {
                    Assertion.Fail(String.Format(
                                       "Failed to satisfy '{0}' on argument[{1}] " +
                                       "of method call {2}",
                                       predicate,
                                       i,
                                       ActualMethodCall
                                       ));
                }
                // return output expectations if specified
                IArgumentMutator mutator =
                    argumentExpectation as IArgumentMutator;
                if (mutator != null)
                {
                    mutator.Mutate(ref actualArguments[i]);
                }
            }
            if (_expectedReturnValue != null)
            {
                // handle return values that are ValueTypes and ensure they can be casted
                // in the IL that unboxes the return value.
                Type expectedReturnType = _expectedReturnValue.GetType();
                Type returnType         = ActualMethod.ReturnType;
                if (returnType != typeof(void) && returnType.IsValueType)
                {
                    if (returnType != expectedReturnType)
                    {
                        _expectedReturnValue = Convert.ChangeType(_expectedReturnValue, returnType);
                    }
                }
            }
            // if exception setup to be thrown, throw it
            if (_expectedException != null)
            {
                throw _expectedException;
            }
        }
 /// <summary>
 /// Chains the current instance to another predicate to evaluate when
 /// exchanging parameters
 /// </summary>
 /// <param name="requirement">object requirement that can be converted
 /// to a <see cref="IPredicate"/></param>
 /// <returns>reference to this instance</returns>
 public object AndRequire(object requirement)
 {
     _predicate = PredicateUtils.ConvertFrom(requirement);
     return(this);
 }