I'm trying to write some unittests for Umbraco 8 but had some troubles with it. I've followed the stpes from the following page (https://our.umbraco.com/documentation/Implementation/Unit-Testing) but found that I don't come to the same result. Does anyone have more experience in this ? Currently when I run one of my tests and try to set the title of the viewmodel, the title that is returned from the viewmodel is null.
The project is a Unit Test Project (.NET Framework 4.7.2)
This is my UmbracoBaseTest Class
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using Umbraco.Core.Cache;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Logging;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Security;
using Umbraco.Web.Security.Providers;
namespace SportVlaanderen.Testing
{
public abstract class UmbracoBaseTest
{
public ServiceContext ServiceContext;
public MembershipHelper MembershipHelper;
public UmbracoHelper UmbracoHelper;
public UmbracoMapper UmbracoMapper;
public Mock<ICultureDictionary> CultureDictionary;
public Mock<ICultureDictionaryFactory> CultureDictionaryFactory;
public Mock<IPublishedContentQuery> PublishedContentQuery;
public Mock<HttpContextBase> HttpContext;
public Mock<IMemberService> memberService;
public Mock<IPublishedMemberCache> memberCache;
[SetUp]
public virtual void SetUp()
{
this.SetupHttpContext();
this.SetupCultureDictionaries();
this.SetupPublishedContentQuerying();
this.SetupMembership();
this.ServiceContext = ServiceContext.CreatePartial();
this.UmbracoHelper = new UmbracoHelper(Mock.Of<IPublishedContent>(), Mock.Of<ITagQuery>(), this.CultureDictionaryFactory.Object, Mock.Of<IUmbracoComponentRenderer>(), this.PublishedContentQuery.Object, this.MembershipHelper);
this.UmbracoMapper = new UmbracoMapper(new MapDefinitionCollection(new List<IMapDefinition>()));
}
public virtual void SetupHttpContext()
{
this.HttpContext = new Mock<HttpContextBase>();
}
public virtual void SetupCultureDictionaries()
{
this.CultureDictionary = new Mock<ICultureDictionary>();
this.CultureDictionaryFactory = new Mock<ICultureDictionaryFactory>();
this.CultureDictionaryFactory.Setup(x => x.CreateDictionary()).Returns(this.CultureDictionary.Object);
}
public virtual void SetupPublishedContentQuerying()
{
this.PublishedContentQuery = new Mock<IPublishedContentQuery>();
}
public virtual void SetupMembership()
{
this.memberService = new Mock<IMemberService>();
var memberTypeService = Mock.Of<IMemberTypeService>();
var membershipProvider = new MembersMembershipProvider(memberService.Object, memberTypeService);
this.memberCache = new Mock<IPublishedMemberCache>();
this.MembershipHelper = new MembershipHelper(this.HttpContext.Object, this.memberCache.Object, membershipProvider, Mock.Of<RoleProvider>(), memberService.Object, memberTypeService, Mock.Of<IUserService>(), Mock.Of<IPublicAccessService>(), AppCaches.NoCache, Mock.Of<ILogger>());
}
public void SetupPropertyValue(Mock<IPublishedContent> publishedContentMock, string alias, object value, string culture = null, string segment = null)
{
var property = new Mock<IPublishedProperty>();
property.Setup(x => x.Alias).Returns(alias);
property.Setup(x => x.GetValue(culture, segment)).Returns(value);
property.Setup(x => x.HasValue(culture, segment)).Returns(value != null);
publishedContentMock.Setup(x => x.GetProperty(alias)).Returns(property.Object);
}
}
}
This is my test class
using Moq;
using NSubstitute;
using NUnit.Framework;
using SportVlaanderen.Models;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
namespace SportVlaanderen.Testing.ContentModels
{
[TestFixture]
public class NcTitleTest : UmbracoBaseTest
{
[SetUp]
public override void SetUp()
{
base.SetUp();
Current.Factory = Substitute.For<IFactory>();
}
[TearDown]
public void TearDown()
{
Current.Reset();
}
[Test]
[TestCase("", "")]
[TestCase("My Heading", "My Heading")]
[TestCase("Another Heading", "Another Heading")]
public void GivenPublishedContent_WhenGetTitle_ThenReturnCustomViewModelWithTitleValue(string value, string expected)
{
var publishedContent = new Mock<IPublishedContent>();
base.SetupPropertyValue(publishedContent, nameof(NcTitle.Title), value);
var model = new NcTitle(publishedContent.Object);
Assert.AreEqual(expected, model.Title);
}
}
}
This is what i get returned from the tests Error message
Aucun commentaire:
Enregistrer un commentaire