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.
e.g:
public string Url(IPublishedContent currentPage)
{
return currentPage.Url();
}
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:
The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception
Hi, I need to unit test the following code :
When running a unit test I get the following exception :
How can i refactor the code to be more testable or how can I unit test this kind of code ?
Thank you
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 asContentExtensionsWrapper.cs
, which itself has a.Url()
method that calls the real method.e.g:
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:is working on a reply...