using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
public static async Task<T> DequeueAsync<T>(this Queue<T> queue)
return await Task.FromResult<T>(queue.Dequeue());
public static void Main()
Console.WriteLine("Creating Moqs and attempting to return sequences of results for Async methods");
Console.WriteLine("Uses mock.Setup with Returns method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the Queue object type and an extension method to return the Task");
Console.WriteLine("Expects to return odd numbers 1-7");
StandardQueueWorks().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.SetupSequence with Returns method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the Queue object type and an extension method to return the Task");
Console.WriteLine("Expected to return 2, then error message");
StandardQueueFails().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.SetupSequence, capturing the fluent api object returned by SetupSequence, then looping through the list of expected return objects and calling ReturnsAsync on the fluent api object for each expected result, utilizing the Queue object type and an extension method to return the Task");
Console.WriteLine("Expected to return odd numbers 3-9");
StandardQueueWithSetupSequenceWorks().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.Setup with Returns method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the AsyncQueue object type");
Console.WriteLine("Expected to return even numbers 4-10");
AsyncQueueWorks().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.SetupSequence with Returns method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the AsyncQueue object type");
Console.WriteLine("Expected to immediatly throw an error message");
AsyncQueueFails().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.Setup with Returns method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the Queue object type and an extension method to return the Task, attempts to return more results than have been queued");
Console.WriteLine("Expected to return odd numbers 1-7, then Queue empty error");
StandardQueueWithDelegateFails().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.SetupSequence with ReturnsAsync method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the Queue object type and an extension method to return the Task, attempts to return more results than have been queued");
Console.WriteLine("Expected to return odd numbers 1-7, then Null Reference error");
StandardQueueWithSetupSequenceFails().Wait();
Console.WriteLine(ex.Message);
Console.WriteLine("Uses mock.SetupSequence with ReturnsAsync method, returning a delegate for a method that returns a Task of the appropriate type, utilizing the Queue object type and an extension method to return the Task, attempts to return more results than have been queued, adds a default setup");
Console.WriteLine("Expected to return even numbers 2-10");
StandardQueuePlusDefaultSetupWorks().Wait();
Console.WriteLine(ex.Message);
private static async Task StandardQueueWorks()
var queue = new Queue<int>(new []{1,3,5,7});
var moq = new Mock<IFoo>();
moq.Setup(m => m.BarAsync())
.Returns(queue.DequeueAsync);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task StandardQueueFails()
var queue = new Queue<int>(new []{2,4,6,8});
var moq = new Mock<IFoo>();
moq.SetupSequence(m => m.BarAsync())
.Returns(queue.DequeueAsync);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task StandardQueueWithSetupSequenceWorks()
var queue = new Queue<int>(new []{3,5,7,9});
var moq = new Mock<IFoo>();
var setup = moq.SetupSequence(m => m.BarAsync());
foreach(var item in queue)
setup.ReturnsAsync(item);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task AsyncQueueWorks()
var queue = new AsyncQueue<int>();
var moq = new Mock<IFoo>();
moq.Setup(m => m.BarAsync())
.Returns(() => queue.DequeueAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task AsyncQueueFails()
var queue = new AsyncQueue<int>();
var moq = new Mock<IFoo>();
moq.Setup(m => m.BarAsync())
.Returns(queue.DequeueAsync);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task StandardQueueWithDelegateFails()
var queue = new Queue<int>(new []{1,3,5,7});
var moq = new Mock<IFoo>();
var setup = moq.Setup(m => m.BarAsync())
.Returns(() => queue.DequeueAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task StandardQueueWithSetupSequenceFails()
var queue = new Queue<int>(new []{1,3,5,7});
var moq = new Mock<IFoo>();
var setup = moq.SetupSequence(m => m.BarAsync());
foreach(var item in queue)
setup.ReturnsAsync(item);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
private static async Task StandardQueuePlusDefaultSetupWorks()
var queue = new Queue<int>(new []{2,4,6,8});
var moq = new Mock<IFoo>();
var setupSequence = moq.SetupSequence(m => m.BarAsync());
var last = queue.ToList().Last();
foreach(var item in queue)
setupSequence.ReturnsAsync(() => {
moq.Setup(m => m.BarAsync()).ReturnsAsync(10);
setupSequence.ReturnsAsync(item);
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());
Console.WriteLine(await foo.BarAsync());