Created
January 22, 2013 00:10
-
-
Save aaronpowell/4590792 to your computer and use it in GitHub Desktop.
Umbraco unit test mocking
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
| public class DataLayer { | |
| private readonly INeedAnAbstraction abs; | |
| public DataLayer() : this(new UmbracoAbstraction()) {} | |
| public DataLayer(INeedAnAbstraction abs) { | |
| this.abs = abs; | |
| } | |
| public IEnumerable<Node> FilterSomeNodes(string docTypeAlias) { | |
| return abs.Nodes.Where(x => x.DocumentTypeAlias == docTypeAlias); | |
| } | |
| } |
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
| public interface INeedAnAbstraction { | |
| IEnumerable<Node> Nodes { get; } | |
| } | |
| private class UmbracoAbstraction : INeedAnAbstraction { | |
| public UmbracoAbstraction() { | |
| Nodes = nodeFactory.GetAll(); | |
| } | |
| public IEnumerable<Node> Nodes { get; private set; } | |
| } |
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
| [TestFixture] | |
| public class Tests { | |
| [Test] | |
| public void I_Can_Haz_Tests() { | |
| var abs = Substitute.For<INeedAnAbstraction>(); | |
| abs.Nodes.Returns(new[] { new Node { DocumentTypeAlias = "Foo" } }); | |
| var dal = new DataLayer(abs); | |
| var result = dal.FilterSomeNodes("Foo"); | |
| Assert.AreEqual(1, result.Count()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment