internal static ExpectMethod <TResult> GetMethodCallArguments <T, TResult>(T instance, Func <T, TResult> func) where T : class { if (instance == null) { throw new ArgumentNullException("instance", "Assertions cannot be made on a null object or instance."); } var container = GetExpectationContainer(instance); if (container == null) { throw new ArgumentOutOfRangeException("instance", "Assertions can only be made on a mocked object or instance."); } var assertion = new ExpectMethod <TResult>(); container.MarkForAssertion(assertion); try { func(instance); } catch (Exception ex) { throw new InvalidOperationException("Exception caught while making assertion.", ex); } if (container.ExpectationMarked) { throw new InvalidOperationException(); } return(assertion); }
/// <summary> /// Creates an expectation against the mocked object for the given method /// with a return type /// </summary> /// <example> /// The following is an example of how to setup an expectation against a method: /// <code> /// [Fact] /// public void Test() { /// var mock = MockRepository.Mock{ICustomerService}(); /// mock.Expect(x => x.FindCustomer(1)) /// .Return(new Customer /// { /// Id = 1, /// }); /// } /// </code> /// </example> /// <remarks> /// Applicable for methods with a return type /// </remarks> /// <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 create the expectation against</param> /// <returns>expectation targeted for methods</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> /// <exception cref="System.InvalidOperationException">thrown when the method cannot be resolved</exception> public static IMethodOptions <TResult> Expect <T, TResult>(this T instance, Func <T, TResult> func) where T : class { if (instance == null) { throw new ArgumentNullException("instance", "Expectations cannot be set on a null object or instance."); } var container = GetExpectationContainer(instance); if (container == null) { throw new ArgumentOutOfRangeException("instance", "Expectations can only be set on a mocked object or instance."); } var expectation = new ExpectMethod <TResult>(); container.MarkForExpectation(expectation); try { func(instance); } catch (Exception ex) { throw new InvalidOperationException("Exception caught while setting up expectation", ex); } if (container.ExpectationMarked) { throw new InvalidOperationException(); } return(expectation); }
/// <summary> /// Creates a stub against the mocked object for the given method with a return type of void /// </summary> /// <example> /// The following is an example of how to setup a stub against a method: /// <code> /// [Fact] /// public void Test() { /// var mock = MockRepository.Mock{ILoggingService}(); /// mock.Stub(x => x.Log('User saved.'); /// } /// </code> /// </example> /// <remarks> /// Applicable for methods with a return type of void /// </remarks> /// <typeparam name="T">the mocked type</typeparam> /// <param name="instance">the mocked instance</param> /// <param name="action">the method to create the stub against</param> /// <returns>stub targeted for methods</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> /// <exception cref="System.InvalidOperationException">thrown when the method cannot be resolved</exception> public static IMethodOptions Stub <T>(this T instance, Action <T> action) where T : class { if (instance == null) { throw new ArgumentNullException("instance", "Stubs cannot be set on a null object or instance."); } var container = GetExpectationContainer(instance); if (container == null) { throw new ArgumentOutOfRangeException("instance", "Stubs can only be set on a mocked object or instance."); } var expectation = new ExpectMethod(); expectation.SetExpectedCount(new Range(int.MaxValue, int.MaxValue)); container.MarkForExpectation(expectation); try { action(instance); } catch (Exception ex) { throw new InvalidOperationException("Exception caught while setting up stub", ex); } if (container.ExpectationMarked) { throw new InvalidOperationException(); } return(expectation); }
/// <summary> /// constructor /// </summary> public RepeatOptions(ExpectMethod expectation) { this.expectation = expectation; }