Created
February 6, 2012 11:40
-
-
Save dtchepak/1751666 to your computer and use it in GitHub Desktop.
Sample for running a sequence of Do actions for NSubstitute When.Do
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using NSubstitute; | |
using NSubstitute.Core; | |
using NUnit.Framework; | |
namespace Sample { | |
public static class MyTestExtensions { | |
public static void DoThese<T>(this WhenCalled<T> when, params Action<CallInfo>[] actions) { | |
var actionQueue = new Queue<Action<CallInfo>>(actions); | |
when.Do(x => RunNextAction(x, actionQueue)); | |
} | |
private static void RunNextAction(CallInfo callInfo, Queue<Action<CallInfo>> actions) { | |
if (actions.Count == 0) return; | |
actions.Dequeue()(callInfo); | |
} | |
} | |
public class DoTheseTests { | |
public interface IFoo { void Bar(out string msg); } | |
[Test] | |
public void SetOutParamToMultipleValues() { | |
var foo = Substitute.For<IFoo>(); | |
string value; | |
foo.WhenForAnyArgs(x => x.Bar(out value)).DoThese(x => x[0] = "hi", x => x[0] = "there", x => x[0] = "world"); | |
foo.Bar(out value); | |
Assert.AreEqual("hi", value); | |
foo.Bar(out value); | |
Assert.AreEqual("there", value); | |
foo.Bar(out value); | |
Assert.AreEqual("world", value); | |
foo.Bar(out value); | |
Assert.AreEqual("world", value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment