Fluently Mocking an Umbraco Node

I posted about Mocking an Umbraco Node and have not been very happy with the syntax of using it. After writing more and more tests where I mock out a node I decided that it was ugly. I took a shot at making it look better, giving it a fluent api.

This code will surely evolve with each test I write, so will make sure I will post an update as it changes.

public class MockNode
{
    private readonly IDictionary<string, object> properties;

    public MockNode()
    {
        this.properties = new Dictionary<string, object>();
    }

    public MockNode AddProperty(string alias, string value)
    {
        properties.Add(alias, value);
        return this;
    }

    public INode Mock()
    {
        var node = Substitute.For<INode>();
        foreach (var pair in properties)
        {
            var property = Substitute.For<IProperty>();
            property.Alias.Returns(pair.Key);
            property.Value.Returns(pair.Value);
            node.GetProperty(pair.Key).Returns(property);
        }

        return node;
    }
}

Here is an example of using it:

var mockNode = new MockNode()
    .AddProperty("Body", "Body")
    .AddProperty("Title", "Title")
    .Mock();

As before it has a dependency on NSubstitute howevere you should be able to swap that out for your favorite mocking framework.

Aaron Job

Aaron Job

It all started at age 10 with an Amstrad 80386SX and a book. That book Computer Space-games made no sense to that 10-year-old but it kicked started an obsession with programming.

Read More
Fluently Mocking an Umbraco Node
Share this

Subscribe to public abstract Gravy : Power