Skip to content

Instantly share code, notes, and snippets.

@dlwiii
Created July 26, 2014 20:01
Show Gist options
  • Save dlwiii/e96800f14eb89e3c7e3e to your computer and use it in GitHub Desktop.
Save dlwiii/e96800f14eb89e3c7e3e to your computer and use it in GitHub Desktop.
Be careful with Moq'ing out lists!
public class Thing
{
public virtual string DoThing(IList<string> args) { return "ONE"; }
}
[Fact]
[Trait("00 Misc", "Reality Check")]
public void Test()
{
var args = new List<string>() { "a", "b" };
var result = "";
var thing = new Thing();
result = thing.DoThing(args);
Assert.True(result == "ONE"); // ONE - self-evident
var moq = new Moq.Mock<Thing>();
thing = moq.Object;
moq.Setup(v => v.DoThing(args)).Returns("TWO");
result = thing.DoThing(args);
Assert.True(result == "TWO"); // TWO - yay, a valid Moq
args = new List<string>() { "a", "b" };
result = thing.DoThing(args);
Assert.True(result == "TWO"); // TWO - because list is the same
args = new List<string>() { "c", "d" };
result = thing.DoThing(args);
Assert.True(result == null); // NULL - the args is a different list
args = new List<string>() { "c", "d","e" };
result = thing.DoThing(args);
Assert.True(result == null); // NULL - definitely a different list
moq.Setup(v => v.DoThing(It.IsAny<IList<string>>())).Returns("THREE");
args = new List<string>() { "c", "d" };
result = thing.DoThing(args);
Assert.True(result == "THREE"); // THREE- any list of strings is good now!
args = new List<string>();
result = thing.DoThing(args);
Assert.True(result == "THREE"); // THREE - even and empty list
args = null;
result = thing.DoThing(args);
Assert.True(result == "THREE"); // THREE - even null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment