Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sebastian Felis 9 posts 127 karma points
    Feb 06, 2021 @ 12:07
    Sebastian Felis
    1

    Unit testing: Mock PublishedDataType

    There is any posibilty to mock PublishedDataType class?

    I need to make a mock of IPublishedContent with property witch contains PublishedPropertyType with specify PublishedDataType.

    I wrote somthing like that:

            var publishedDataType = Mock.Of<PublishedDataType>(dt => dt.EditorAlias == editorAlias && dt.Configuration == defaultConfiguration);
    
            var publishedPropertyType = new Mock<PublishedPropertyType>();
            publishedPropertyType.Setup(s => s.DataType).Returns(publishedDataType);
    
            var publishedContentMock = new Mock<IPublishedContent>();            
            SetupPropertyValue(publishedContentMock, publishedPropertyType.Object, "propertyName", null);
            PublishedContentQuery.Setup(c => c.Content(contentId)).Returns(publishedContentMock.Object);
    
            public void SetupPropertyValue(Mock<IPublishedContent> publishedContentMock, IPublishedPropertyType publishedPropertyType, 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);
                property.Setup(x => x.PropertyType).Returns(publishedPropertyType);
    
                publishedContentMock.Setup(x => x.GetProperty(alias)).Returns(property.Object);
            }
    

    But PublishedDataType class has only internal constructor so I can't create them. Is any way to get around this problem?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Feb 07, 2021 @ 13:28
    Søren Gregersen
    100

    Hi,

    The most simple way is to let your test assembly identity as "Umbraco.Tests".

    From Umbraco.Tests:

    https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Tests/Properties/AssemblyInfo.cs

    From Umbraco.Core:

    https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Properties/AssemblyInfo.cs

    Umbraco does this, to allow internals to be called from their own tests.

  • Sebastian Felis 9 posts 127 karma points
    Feb 11, 2021 @ 12:24
    Sebastian Felis
    0

    Thank for the hint. I added the below tags to project file (.csproj) of my unit tests and it's works.

    <PropertyGroup>
        <AssemblyName>Umbraco.Tests</AssemblyName>
    </PropertyGroup>
    
Please Sign in or register to post replies

Write your reply to:

Draft