/// <summary>
        /// Specifies that the arranged member will return consecutive values from the given array.
        /// If the arranged member is called after it has returned the last value, the behavior depends on the behavior parameter.
        /// </summary>
        /// <typeparam name="TReturn">Type of return value</typeparam>
        /// <param name="func">The arranged member</param>
        /// <param name="values">The list of values that will be returned by the arranged member. The list may be modified after the arrangement is made.</param>
        /// <param name="behavior">The behavior after the last value has been returned.</param>
        /// <returns>Reference to <see cref="IAssertable"/> interface.</returns>
        public static IAssertable ReturnsMany <TReturn>(this IFunc <TReturn> func, IList <TReturn> values, AfterLastValue behavior)
        {
            return(ProfilerInterceptor.GuardInternal(() =>
            {
                if (values == null || values.Count == 0)
                {
                    throw new ArgumentException("Expected at least one value to return", "values");
                }

                Action <ReturnsManyImpl <TReturn> > afterEndAction = null;
                switch (behavior)
                {
                case AfterLastValue.ThrowAssertionFailed:
                    afterEndAction = impl => MockingContext.Fail("List of arranged return values exhausted.");
                    break;

                case AfterLastValue.KeepReturningLastValue:
                    afterEndAction = impl => impl.CurrentIndex = values.Count - 1;
                    break;

                case AfterLastValue.StartFromBeginning:
                    afterEndAction = impl => impl.CurrentIndex = 0;
                    break;

                default:
                    throw new ArgumentException("behavior");
                }

                return func.Returns(new ReturnsManyImpl <TReturn>(values, afterEndAction).GetNext);
            }));
        }
Exemplo n.º 2
0
 public void Assert()
 {
     if (!IsExpectationMet)
     {
         MockingContext.Fail("Calls should be executed in the order they are expected. Actual order of calls:\n{0}", InOrderExecutionMessage);
     }
 }
Exemplo n.º 3
0
 public void Assert()
 {
     if (this.strictnessViolationMessage != null)
     {
         MockingContext.Fail(this.strictnessViolationMessage.ToString());
     }
 }
Exemplo n.º 4
0
 public void Assert()
 {
     if (!this.IsMet)
     {
         MockingContext.Fail("Not all prerequisites are met. Actual call processed:\n{0}", this.ExecutionMessage);
     }
 }
Exemplo n.º 5
0
 public void Assert()
 {
     if (!IsExpectationMet)
     {
         MockingContext.Fail("{0}Calls should be executed in the order they are expected. Actual order of calls:\n{1}",
                             this.message != null ? this.message + " " : "", InOrderExecutionMessage);
     }
 }
Exemplo n.º 6
0
        public void Process(Invocation invocation)
        {
            this.wasCalled          = true;
            this.calledInWrongOrder = (this.LastIdInOrder != this.arrangementId - 1);
            this.LastIdInOrder      = this.arrangementId;

            this.InOrderExecutionLog += invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace("    ");

            if (this.calledInWrongOrder)
            {
                MockingContext.Fail("Last call executed out of order. Order of calls so far:\n{1}", invocation.InputToString(), InOrderExecutionMessage);
            }
        }
        public static void Assert(int?lowerBound, int?upperBound, int calls, object expression)
        {
            if (IsInRange(lowerBound, upperBound, calls))
            {
                return;
            }

            var message = String.Format("Occurrence expectation failed. {0}. Calls so far: {1}",
                                        MakeRangeString(lowerBound, upperBound),
                                        calls);

            if (expression != null)
            {
                message += String.Format("\nArrange expression: {0}", expression).EscapeFormatString();
            }

            MockingContext.Fail(message);
        }
Exemplo n.º 8
0
        public void Process(Invocation invocation)
        {
            this.wasCalled = true;

            bool processedOnce = this.LastIdInOrder > -1;

            this.calledInWrongOrder =
                processedOnce &&
                (this.LastIdInOrder - this.arrangementId) > 0 || (this.LastIdInOrder - this.arrangementId) < -1;

            this.LastIdInOrder = this.arrangementId;

            this.InOrderExecutionLog += invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace("    ");

            if (this.calledInWrongOrder)
            {
                MockingContext.Fail("{0}Last call executed out of order. Order of calls so far:\n{1}",
                                    this.message != null ? this.message + " " : "", InOrderExecutionMessage);
            }
        }
        public void Process(Invocation invocation)
        {
            if (invocation.Recording || invocation.InArrange || invocation.InAssertSet)
            {
                return;
            }

            var returnType = invocation.Method.GetReturnType();

            if (!typeof(Task).IsAssignableFrom(returnType))
            {
                MockingContext.Fail("Wrong invocation to arrangement: return type of {0}.{1} is not a task",
                                    invocation.Instance != null ? MockingUtil.GetUnproxiedType(invocation.Instance) : invocation.Method.DeclaringType, invocation.Method.Name);
            }

            var elementType = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>)
                    ? returnType.GetGenericArguments()[0] : typeof(object);
            Expression <Func <Task <object> > > taskFromException =
                () => MockingUtil.TaskFromException <object>((Exception)null);
            var mock =
                ((MethodCallExpression)taskFromException.Body).Method
                .GetGenericMethodDefinition()
                .MakeGenericMethod(elementType)
                .Invoke(null, new object[] { this.exception });

            var parentMock = invocation.MockMixin;
            var mockMixin  = MocksRepository.GetMockMixin(mock, null);

            if (parentMock != null && mockMixin != null)
            {
                parentMock.DependentMocks.Add(mock);
            }

            invocation.ReturnValue  = mock;
            invocation.CallOriginal = false;
            invocation.UserProvidedImplementation = true;
        }