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);
}
[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...
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…
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.
I’m using Umbraco 11.2.0.
Any help would be greatly appreciated.
Thanks.
I finally worked out a solution to this.
I had to change the extension method a little to this...
Hope this might be useful to someone, if anyone can improve on this please post.
I changed the extension method a little bit more.
is working on a reply...