Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi, I need to unit test the following code :
var myPage = _publishedContentQuery.ContentAtRoot().DescendantsOrSelfOfType("myDocAlias").FirstOrDefault();
When running a unit test I get the following exception :
The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception.'
How can i refactor the code to be more testable or how can I unit test this kind of code ?
A good question. I hit the same issue attempting to use IPublishedContent.Url() in a unit test.
This extension method retrieves an instance of IPublishedUrlProvider from the internal StaticServiceProvider service locator which renders the code "basically untestable", according to the documentation.
I've seen a way round the Url() issue by wrapping the .Url() method inside another method within a new, mockable class. Such as ContentExtensionsWrapper.cs, which itself has a .Url() method that calls the real method.
public string Url(IPublishedContent currentPage)
Then, using Moq for example, you can pass your content item into your own mocked wrapper class, setup the expectation on the .Url() method, and return it that way:
mockContentExtensionsWrapper.Setup(x => x.Url(It.IsAny<IPublishedContent>())).Returns("https://google.com");
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted