/// <summary> /// Returns all of the actual calls made against the given method /// </summary> /// <typeparam name="T">the mocked type</typeparam> /// <typeparam name="TResult">the return type of the method</typeparam> /// <param name="instance">the mocked instance</param> /// <param name="func">the method to retrieve the calls for</param> /// <returns>collection of the actual calls</returns> /// <exception cref="System.ArgumentNullException">thrown when the instance is null</exception> /// <exception cref="System.ArgumentOutOfRangeException">thrown when the instance cannot be identified as a mocked object</exception> public static Actuals[] GetArgumentsForCallsMadeOn <T, TResult>(this T instance, Func <T, TResult> func) where T : class { if (instance == null) { throw new ArgumentNullException("instance", "Assertion cannot be performed on a null object or instance."); } var invocation = instance as IInvocation; var container = GetExpectationContainer(instance); if (container == null) { throw new ArgumentOutOfRangeException("instance", "Assertion can only be performed on a mocked object or instance."); } var assertion = MockRepository.GetMethodCallArguments(instance, func); var methodName = assertion.GetDisplayName(invocation); var actuals = container.ListActuals(); if (actuals.Any()) { return(actuals .Where(x => assertion.MatchesCall(x.Method, x.Arguments)) .ToArray()); } return(new Actuals[0]); }
/// <summary> /// Asserts the given method was called against the mocked object /// </summary> /// <typeparam name="T">the mocked type</typeparam> /// <typeparam name="TResult">the return type of the method</typeparam> /// <param name="instance">the mocked instance</param> /// <param name="func">the method to assert was called</param> /// <exception cref="Rhino.Mocks.Exceptions.ExpectationViolationException">thrown when the method was not called</exception> /// <exception cref="System.ArgumentNullException">thrown when the instance is null</exception> /// <exception cref="System.ArgumentOutOfRangeException">thrown when the instance cannot be identified as a mocked object</exception> public static void AssertWasCalled <T, TResult>(this T instance, Func <T, TResult> func) where T : class { if (instance == null) { throw new ArgumentNullException("instance", "Assertion cannot be performed on a null object or instance."); } var invocation = instance as IInvocation; var container = GetExpectationContainer(instance); if (container == null) { throw new ArgumentOutOfRangeException("instance", "Assertion can only be performed on a mocked object or instance."); } var assertion = MockRepository.GetMethodCallArguments(instance, func); var methodName = assertion.GetDisplayName(invocation); var actuals = container.ListActuals(); if (actuals.Any()) { for (int index = 0; index < actuals.Length; index++) { var actual = actuals[index]; if (assertion.MatchesCall(actual.Method, actual.Arguments)) { return; } } } throw new ExpectationViolationException( string.Format("{0} Expected #1, Actual #0.", methodName)); }