示例#1
0
 /// <summary>
 /// Verifies a method invocation matching the <paramref name="function"/> was executed at
 /// least once. If <paramref name="times"/> is provided, the number of calls is verified too.
 /// </summary>
 /// <param name="function">The method invocation to match against actual calls.</param>
 /// <param name="times">Optional number of times the method should have been called. Defaults to <see cref="Times.AtLeastOnce"/>.
 /// An integer value can also be specificed since there is built-in conversion support from integer to <see cref="Times"/>.</param>
 /// <param name="message">Optional user message to show.</param>
 internal static void CalledImpl <T>(Func <T> function, Sdk.Times times = default, string?message = null)
 {
     using (new SetupScope())
     {
         function();
         var setup = MockContext.CurrentSetup ?? CallContext.ThrowUnexpectedNull <IMockSetup>();
         var mock  = (MockContext.CurrentInvocation ?? CallContext.ThrowUnexpectedNull <IMethodInvocation>()).Target.AsMock();
         var calls = mock.Invocations.Where(x => setup.AppliesTo(x));
         if (!times.Validate(calls.Count()))
         {
             throw new VerifyException(mock, setup, message);
         }
     }
 }
示例#2
0
 /// <summary>
 /// Verifies a method invocation matching the <paramref name="action"/> was executed at
 /// least once. If <paramref name="times"/> is provided, the number of calls is verified too.
 /// </summary>
 /// <param name="action">The method invocation to match against actual calls.</param>
 /// <param name="times">Optional number of times the method should have been called. Defaults to <see cref="Times.AtLeastOnce"/>.
 /// An integer value can also be specificed since there is built-in conversion support from integer to <see cref="Times"/>.</param>
 /// <param name="message">Optional user message to show.</param>
 internal static void CalledImpl(Action action, Sdk.Times times = default, string message = null)
 {
     using (new SetupScope())
     {
         action();
         var setup = MockContext.CurrentSetup;
         var mock  = MockContext.CurrentInvocation.Target.AsMock();
         var calls = mock.Invocations.Where(x => setup.AppliesTo(x));
         if (!times.Validate(calls.Count()))
         {
             throw new VerifyException(mock, setup, message);
         }
     }
 }