Пример #1
0
        public static T Verify <T>(T mock, INMockitoTimesMatcher times = null, NMockitoOrder order = NMockitoOrder.DontCare)
            where T : class
        {
            times = times ?? new NMockitoTimesAnyMatcher();

            var state       = statesByMock[mock];
            var interceptor = new MockVerifyInterceptor(state, times, order);
            var proxy       = proxyGenerator.CreateInterfaceProxyWithoutTarget <T>(interceptor);

            return(proxy);
        }
Пример #2
0
 public static void AcceptVerificationCounterOrThrow(int counter, NMockitoOrder order)
 {
     if (order == NMockitoOrder.DontCare)
     {
         HandleDontCareVerify(counter);
     }
     else if (order == NMockitoOrder.AfterPrevious)
     {
         HandleAfterPreviousVerify(counter);
     }
     else if (order == NMockitoOrder.WithPrevious)
     {
         HandleWithPreviousVerify(counter);
     }
     else if (order == NMockitoOrder.Whenever)
     {
         HandleWheneverVerify(counter);
     }
     else
     {
         throw new InvalidOperationException("Unknown orderness " + order);
     }
 }
Пример #3
0
 public MockVerifyInterceptor(MockState state, INMockitoTimesMatcher times, NMockitoOrder order)
 {
     this.state = state;
     this.times = times;
     this.order = order;
 }
Пример #4
0
        public void HandleMockVerification(IInvocation invocation, INMockitoTimesMatcher times, NMockitoOrder order)
        {
            invocation.ReturnValue = GetDefaultValue(invocation.Method.ReturnType);

            // Flatten params[] of invocation arguments
            var method              = invocation.Method;
            var methodParameters    = method.GetParameters();
            var invocationArguments = invocation.Arguments;

            if (methodParameters.Length > 0 &&
                methodParameters.Last().GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0 &&
                invocationArguments.Last() != null)
            {
                var paramsArray            = (Array)invocationArguments.Last();
                var newInvocationArguments = new object[invocationArguments.Length + paramsArray.Length - 1];
                var cutIndex = invocationArguments.Length - 1;
                Array.Copy(invocationArguments, newInvocationArguments, cutIndex);
                Array.Copy(paramsArray, 0, newInvocationArguments, cutIndex, paramsArray.Length);
                invocationArguments = newInvocationArguments;
            }

            var smartParameters = NMockitoSmartParameters.CopyAndClearSmartParameters().ToArray();

            if (smartParameters.Length == 0)
            {
                smartParameters = ConvertInvocationArgumentsToEqualityParameters(invocationArguments);
            }
            if (smartParameters.Length != invocationArguments.Length)
            {
                throw new NMockitoNotEnoughSmartParameters();
            }
            // Find out/ref parameters, swap them with null
            for (var i = 0; i < methodParameters.Length; i++)
            {
                var parameter = methodParameters[i];
                if (parameter.Attributes.HasFlag(ParameterAttributes.Out))
                {
                    smartParameters[i] = null;
                }
            }

            var counters = FindMatchingInvocationCounters(invocation.Method, invocation.GenericArguments, smartParameters);

            times.MatchOrThrow(counters.Sum(counter => counter.Count), invocation, invocationCountsByInvocation);
            foreach (var counter in counters)
            {
                foreach (var count in counter)
                {
                    NMockitoInvocationCounters.AcceptVerificationCounterOrThrow(count, order);
                }
                counter.Clear();
            }
        }
Пример #5
0
 [DebuggerHidden] public T Verify <T>(T mock, INMockitoTimesMatcher times = null, NMockitoOrder order = NMockitoOrder.DontCare) where T : class
 {
     return(NMockitoStatic.Verify(mock, times, order));
 }