Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created February 6, 2012 11:40
Show Gist options
  • Save dtchepak/1751666 to your computer and use it in GitHub Desktop.
Save dtchepak/1751666 to your computer and use it in GitHub Desktop.
Sample for running a sequence of Do actions for NSubstitute When.Do
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