Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created January 22, 2013 00:10
Show Gist options
  • Select an option

  • Save aaronpowell/4590792 to your computer and use it in GitHub Desktop.

Select an option

Save aaronpowell/4590792 to your computer and use it in GitHub Desktop.
Umbraco unit test mocking
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);
}
}
public interface INeedAnAbstraction {
IEnumerable<Node> Nodes { get; }
}
private class UmbracoAbstraction : INeedAnAbstraction {
public UmbracoAbstraction() {
Nodes = nodeFactory.GetAll();
}
public IEnumerable<Node> Nodes { get; private set; }
}
[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