uMocks is a very simple Umbraco mock object builder written using fluent design approach. It is built on top of Moq, Json.Fluently and Umbraco.Core libraries. uMocks makes Umbraco unit testing easier and more human-readable giving developers ability to fake whole document within complex values stored inside of it.
You can create complex relations between mocked documents which are always being updated dynamically. Example document structure:
[TestMethod]
public void PublishedContent_ShouldUpdateFamilyTreeDynamically()
{
// Arrange
var mockSession = PublishedContentMockSession.CreateNew(); // create new mock session
var doc1 = mockSession.PublishedContentBuilder // use IPublishedContentMockBuilder to build your content fluently
.PrepareNew("documentTypeAlias", documentId: 1001)
.WithProperty("propAlias", "propValue")
.WithChildren(stx => new[] {stx.PrepareNew("anotherDocumentTypeAlias", documentId: 1002)})
.Build();
// Assert
Assert.AreEqual(1, doc1.Children.Count());
Assert.AreEqual(1002, doc1.Children.First().Id);
// Arrange
var doc2 = mockSession.PublishedContentBuilder
.PrepareNew("documentTypeAlias", documentId: 1003)
.OfParent(doc1)
.Build();
// Assert
Assert.AreEqual(2, doc1.Children.Count());
Assert.AreEqual(2, doc1.Children.OfTypes("documentTypeAlias", "anotherDocumentTypeAlias").Count());
}
IPublishedContentMockBuilder gives ability to mock almost all available IPublishedContent properties and methods. Take a look on example:
[TestMethod]
public void PublishedContent_ShouldBeMockedProperly()
{
// Arrange
var mockSession = PublishedContentMockSession.CreateNew();
var doc1 = mockSession.PublishedContentBuilder
.PrepareNew("documentTypeAlias", documentId: 1001)
.WithProperty("propAlias", "propValue")
.Build();
var doc2 = mockSession.PublishedContentBuilder
.PrepareNew("documentTypeAlias", documentId: 1002)
.OfParent(doc1)
.Build();
var doc3 = mockSession.PublishedContentBuilder
.PrepareNew("documentTypeAlias", documentId: 1003)
.OfParent(doc2)
.Build();
var doc4 = mockSession.PublishedContentBuilder
.PrepareNew("documentTypeAlias", documentId: 1004)
.OfParent(doc2)
.Build();
// Assert
// getting basic content data are supported in all possible ways
Assert.AreEqual("documentTypeAlias", doc1.DocumentTypeAlias);
Assert.AreEqual("documentTypeAlias", doc1.ContentType.Alias);
Assert.AreEqual("propValue",doc1.GetPropertyValue<string>("propAlias"));
Assert.AreEqual("propValue", doc1.GetPropertyValue("propAlias"));
Assert.AreEqual("propValue", doc1.GetProperty("propAlias").Value);
Assert.AreEqual("propValue", ((IPublishedProperty)doc1["propAlias"]).Value);
// parent-children relations behave as expected in scope of single mock session - relations are dynamically updated
Assert.AreEqual("-1,1001,1002,1003", doc3.Path);
Assert.AreEqual(2, doc2.Children.Count());
Assert.AreEqual(1002, doc4.Parent.Id);
Assert.AreEqual(2, doc4.ContentSet.Count()); // siblings count
// most extension methods evaluates properly (methods independent on HttpContext and ApplicationContext)
Assert.AreEqual(1004, doc3.FollowingSibling().Id); // following sibling of doc3 => doc4.Id
Assert.AreEqual(null, doc3.PrecedingSibling()); // no preceding sibling of doc3 found
}
Umbraco 7 provides powerful content editor called Grid Layout which allows to define rich content structures very easily. Grid Layout editor stores underlying model in complex JSON structure so it's often very inconvinient to write unit tests for grid layout related logic. uMocks provides fluent Grid Layout builder which gives you maximum freedom of grid content definition. You can inject it into property value without a hassle then. Together with IPublishedContentMockBuilder you will be able to unit test whatever service you have. Example layout definition with custom component inside:
[TestMethod]
public void GridComponent_ShouldHaveGivenValue()
{
// Arrange
var mockSession = PublishedContentMockSession.CreateNew();
var gridEditor = mockSession.GridEditorBuilder
.CreateNew("1 column layout")
.AddSection(12)
.AddFullWidthRow()
.SubmitLayout()
.PutGridComponent(sectionIndex: 0, rowIndex: 0, columnIndex: 0, alias: "componentAlias", b => b.CreateNew()
.WithProperty("propertyName1", "propertyValue1")
.WithProperty("propertyName2", "propertyValue2"))
.Build();
var controlTokens = gridEditor.SelectTokens("$..controls[*]").ToArray();
// Assert
// grid editor should have one component defined
Assert.AreEqual(1, controlTokens.Count());
var control = controlTokens.First();
// grid editor should have given alias
Assert.AreEqual("componentAlias", control.SelectToken("$.editor").Value<string>("alias"));
// grid editor should have given property value
Assert.AreEqual("propertyValue1", control.SelectToken("$.value.value").Value<string>("propertyName1"));
}
For further exploration, please check out uMocks.Samples project
There is a small road map of uMocks feature development:
- Examine search result mocking helper implementation
- IContent mock implementation
- Umbraco ContentService events sandbox implementation
MIT License
Copyright (c) 2019 psmoq
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.