Copied to clipboard

Flag this post as spam?

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


  • rob 4 posts 94 karma points
    Mar 09, 2023 @ 09:10
    rob
    0

    Unit test for custom IPublishedContent extension method (FriendlyPublishedContentExtensions error)

    Hello I’m trying to write a unit test for a custom IPublishedContent extension method.

    The extension method looks like this…

    public static string GetAltText(this IPublishedContent iPublishedContent)
    {
        string description = iPublishedContent.Value<string>("alternativeText");
    
        if (string.IsNullOrEmpty(description))
        {
            return iPublishedContent.Name;
        }
    
        return description;    
    }
    

    I've tried a number of ways but end up with the same error message…

    System.TypeInitializationException : The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception. ----> System.ArgumentNullException : Value cannot be null. (Parameter 'provider')

    One attempt was to use a wrapper class but I’m not sure if I am even approached it correctly.

    public interface IExtensionMethodTest
    {
        string GetAltText();
    }
    
    public class ExtensionMethodTest : IExtensionMethodTest
    {
        private readonly IPublishedContent _iPublishedContent;
    
        public ExtensionMethodTest(IPublishedContent iPublishedContent)
        {
            _iPublishedContent = iPublishedContent;
        }
    
        public string GetAltText()
        {
            return _iPublishedContent.GetAltText();
        } 
    }
    
    [Test]
    [TestCase("GetAltText")]
    public void GetAltText(string getAltText)
    {
        Mock<IPublishedContent>  publishedContent = new Mock<IPublishedContent>();
    
        var wrapper = new ExtensionMethodTest(publishedContent.Object);
    
        var result = wrapper.GetAltText();
    
        Assert.AreEqual(getAltText, result);
    }
    

    I’m using Umbraco 11.2.0.

    Any help would be greatly appreciated.

    Thanks.

  • rob 4 posts 94 karma points
    Mar 28, 2023 @ 13:06
    rob
    100

    I finally worked out a solution to this.

    [Test]
    [TestCase("getAltText")]
    public void Given_PublishedContentHasAltText_When_UsingExtensionMethod_Then_ReturnsString(string getAltText)
    {
        _publishedContent.SetupPropertyValue("alternativeText", getAltText);
    
        var model = new ContentModel(_publishedContent.Object);
    
        var result = model.Content.GetAltText(iPublishedValueFallback: Mock.Of<IPublishedValueFallback>());
    
        Assert.That(result, Is.EqualTo(getAltText));
    }
    

    I had to change the extension method a little to this...

    public static string GetAltText(this IPublishedContent iPublishedContent, IPublishedValueFallback iPublishedValueFallback = null)
    {
        string altText = "";
    
        if (iPublishedValueFallback != null)
        {
            altText = iPublishedContent.Value<string>(iPublishedValueFallback, "alternativeText");
        }
        else
        {
            altText = iPublishedContent.Value<string>("alternativeText");
        }
    
        if (string.IsNullOrWhiteSpace(altText))
        {
            return iPublishedContent.Name;
        }
    
        return altText;
    }
    

    Hope this might be useful to someone, if anyone can improve on this please post.

  • rob 4 posts 94 karma points
    Mar 28, 2023 @ 14:32
    rob
    0

    I changed the extension method a little bit more.

    public static string GetAltText(this IPublishedContent iPublishedContent, IPublishedValueFallback iPublishedValueFallback = null)
    {
        string altText = iPublishedContent.Value<string>(iPublishedValueFallback ?? null, "alternativeText");
    
        if (string.IsNullOrWhiteSpace(altText))
        {
            return iPublishedContent.Name;
        }
    
        return altText;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft