Last active
August 29, 2015 14:23
-
-
Save rubberduck203/709e68963a85b40f4b86 to your computer and use it in GitHub Desktop.
Mock a plain Enumerable with Moq that also works with Linq
This file contains 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
[TestMethod] | |
public void PlainEnumerableExample() | |
{ | |
var project = new Mock<VBProject>(); | |
var refs = new Mock<References>(); | |
refs.Setup(r => r.GetEnumerator()).Returns(ReferenceList()); | |
refs.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(ReferenceList); | |
project.SetupGet(p => p.References).Returns(refs.Object); | |
AccessRefsFromProject(project.Object); | |
} | |
private static IEnumerator<Reference> ReferenceList() | |
{ | |
var ref1 = new Mock<Reference>(); | |
ref1.SetupGet(r => r.Name).Returns("Library_Foo"); | |
yield return ref1.Object; | |
var ref2 = new Mock<Reference>(); | |
ref2.SetupGet(r => r.Name).Returns("Library_Bar"); | |
yield return ref2.Object; | |
} | |
private void AccessRefsFromProject(VBProject project) | |
{ | |
foreach (Reference ref1 in project.References) | |
{ | |
Console.WriteLine(ref1.Name); | |
} | |
var refs = project.References.Cast<Reference>(); | |
var list = refs.ToList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment