如何在Umbraco中对视图模型进行单元测试

时间:2020-05-15 09:46:08

标签: unit-testing testing umbraco viewmodel umbraco8

我正在尝试为Umbraco 8编写一些单元测试,但是遇到了一些麻烦。我遵循了下一页(https://our.umbraco.com/documentation/Implementation/Unit-Testing)中的步骤,但是发现我没有得到相同的结果。有人在这方面有更多经验吗?当前,当我运行其中的一项测试并尝试设置viewmodel的标题时,从viewmodel返回的标题为null。

该项目是单元测试项目(.NET Framework 4.7.2)

这是我的UmbracoBaseTest类

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);
        }
    }
}

这是我的考试班

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);
        }
    }
}

这就是我从测试中得到的回报 Error message

0 个答案:

没有答案