Esempio n. 1
0
 public void HandleMockInvocation(IInvocation invocation)
 {
     this.lastInvocation = invocation;
     GetInvocationCounter(invocation).Add(NMockitoInvocationCounters.TakeNextInvocationCounter());
     invocation.ReturnValue = GetInvocationResult(invocation);
     NMockitoGlobals.SetLastInvocationAndMockState(invocation, this);
 }
Esempio n. 2
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();
            }
        }