How to unit test with the new .Url() extension method? Throws exception and can't mock it
The old .Url property on IPublishedContent is marked as obsolete and will dissapear. It has been replaced by an extension method in Umbraco.Web namespace.
The old .Url property was easy to mock:
content.Setup(x => x.Url).Returns(url);
Now it doesn't work to mock the Setup of the property because it is an extension method and they can't be mocked. Using a wrapper around the extension method doesn't work either.
If I skip setting up the extension method then my unit tests will throw a null reference exception because the UmbracoMapper lib from Andy Butland requires the Url() to be set.
.Url() extension is not testable since it uses a static service locator without a setter so it can´t be mocked (Current.Factory).
.Url has been deprecated so you can still use it but be aware in the netcore version it’s going away. It will not go away in v8 but you would have to change it if you updgrade/rebuild to v9.
In Netcore version .Url() will be testable since dotnetcore has the option of passing in its dependencies which makes it easier to test.
I had this same problem a while back when first writing Xunit tests for a project I was working on and I found that you can use the .Url method without brackets instead, and mock that instead of the .Url() method. However, you should be careful when changing the methods over to ensure the data is still being recieved properly.
Hi there
We are stuck in the same place. We are trying to mock models builder with nSubstitute but we couldn't mock this code
This is the error we get
System.TypeInitializationException: The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception.
System.TypeInitializationException
The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception.
at Umbraco.Extensions.FriendlyPublishedContentExtensions.get_VariationContextAccessor()
at Umbraco.Extensions.FriendlyPublishedContentExtensions.FirstChild[T](IPublishedContent content, String culture)
at Raised.Common.Services.UmbracoContentNodeService.GetHomePage() in D:\Projects\Raised\src\Raised.Common\Services\UmbracoContentNodeService.cs:line 73
at Raised.Tests.Common.HelperTests.UmbracoContentNodeServiceTests.GetHomePage_Returns_HomePage() in D:\Projects\Raised\src\Raised.Tests.Common\HelperTests\UmbracoContentNodeServiceTests.cs:line 57
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
System.ArgumentNullException
Value cannot be null. (Parameter 'provider')
at System.ThrowHelper.Throw(String paramName)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Umbraco.Extensions.FriendlyPublishedContentExtensions..cctor()
Hi there,
I need to write some unit tests but we still got this error. I use these packages and this code. nothing else
. umbraco v12.2.0
. xunit v2.5.3
. NSubstitute v5.1.0
var content = Substitute.For<IPublishedContent>();
content.Value<string>("title").Returns("Title");
How to unit test with the new .Url() extension method? Throws exception and can't mock it
The old .Url property on IPublishedContent is marked as obsolete and will dissapear. It has been replaced by an extension method in Umbraco.Web namespace.
The old .Url property was easy to mock: content.Setup(x => x.Url).Returns(url);
Now it doesn't work to mock the Setup of the property because it is an extension method and they can't be mocked. Using a wrapper around the extension method doesn't work either.
If I skip setting up the extension method then my unit tests will throw a null reference exception because the UmbracoMapper lib from Andy Butland requires the Url() to be set.
Any advice?
Hi Rasmus.
See conversation on twitter: https://twitter.com/dadolfi/status/1355120357301231618
To summarize:
Hi,
If currently .Url() can't be mocked, what is the alternative to get the Url and be able to unit test the code ?
Thank you
Hey Alexandre,
I had this same problem a while back when first writing Xunit tests for a project I was working on and I found that you can use the .Url method without brackets instead, and mock that instead of the .Url() method. However, you should be careful when changing the methods over to ensure the data is still being recieved properly.
Hi there We are stuck in the same place. We are trying to mock models builder with nSubstitute but we couldn't mock this code
This is the error we get
Hi there, I need to write some unit tests but we still got this error. I use these packages and this code. nothing else . umbraco v12.2.0 . xunit v2.5.3 . NSubstitute v5.1.0
@DennisAdolfi @RasmusOlofsson
is working on a reply...