Unit Testing
A guide to getting started with unit testing in Umbraco
Mocking
Testing a ContentModel
public class PageViewModel : ContentModel
{
public PageViewModel(IPublishedContent content) : base(content) { }
public string Heading => (string)this.Content.GetProperty(nameof(Heading)).GetValue();
}
public class PageViewModelTests
{
[Test, AutoData]
public void Given_PublishedContent_When_GetHeading_Then_ReturnPageViewModelWithHeading(string value, Mock<IPublishedContent> content)
{
SetupPropertyValue(content, nameof(PageViewModel.Heading), value);
var viewModel = new PageViewModel(content.Object);
Assert.AreEqual(value, viewModel.Heading);
}
public void SetupPropertyValue(Mock<IPublishedContent> content, string propertyAlias, string propertyValue, string culture = null)
{
var property = new Mock<IPublishedProperty>();
property.Setup(x => x.Alias).Returns(nameof(PageViewModel.Heading));
property.Setup(x => x.GetValue(culture, null)).Returns(propertyValue);
content.Setup(x => x.GetProperty(propertyAlias)).Returns(property.Object);
}
}Testing a RenderController
Testing a SurfaceController
Testing an UmbracoApiController
Testing ICultureDictionary using the UmbracoHelper
Last updated
Was this helpful?