Home > Uncategorized > moq-returns-in-order

moq-returns-in-order

October 3rd, 2010 Leave a comment Go to comments

Oh, how I wanted to use this code, but alas as I look at some old code I now need to delete it. Here it is for later use that will never occur. The original site is the remarks below.

using System;
using System.Collections.Generic;
using Moq.Language.Flow;

namespace UnitTests
{
    /// <summary>
    ///  Moq Extensions
    /// </summary>
    public static class MoqExtensions
    {
        /// <summary>
        /// Returns delegates the in order setup.
        /// </summary>
        /// <example>var category = new Mock<ICategory>();
        ///          category.Setup(c => c.Id).ReturnsInOrder(
        ///             () => 1,
        ///             () => 2,
        ///             () => 3,
        ///             () => 4);
        ///
        ///              var expectedIds = new List<int> { 1, 2, 3, 4 };
        ///              foreach (var expectedId in expectedIds) Assert.Equal(expectedId, category.Object.Id);
        /// </example>
        /// <remarks>from http://www.timvw.be/setup-expectation-with-successive-function-calls-using-moq/ </remarks>
        /// <typeparam name="TMock">The type of the mock.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="setup">The setup.</param>
        /// <param name="valueFunctions">The value functions.</param>
        /// <returns></returns>
        public static IReturnsResult<TMock> ReturnsInOrder<TMock, TResult>(this ISetup<TMock, TResult> setup, params Func<TResult>[] valueFunctions) where TMock : class
        {
            var functionQueue = new Queue<Func<TResult>>(valueFunctions);
            return setup.Returns(() => functionQueue.Dequeue()());
        }
    }
}
Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.