Copied to clipboard

Flag this post as spam?

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


  • Rasmus Olofsson 23 posts 187 karma points c-trib
    Oct 19, 2020 @ 13:05
    Rasmus Olofsson
    1

    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?

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Feb 02, 2021 @ 05:53
    Dennis Adolfi
    102

    Hi Rasmus.

    See conversation on twitter: https://twitter.com/dadolfi/status/1355120357301231618

    To summarize:

    1. .Url() extension is not testable since it uses a static service locator without a setter so it can´t be mocked (Current.Factory).
    2. .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.
    3. In Netcore version .Url() will be testable since dotnetcore has the option of passing in its dependencies which makes it easier to test.
  • Alexandre Locas 52 posts 219 karma points
    Mar 21, 2022 @ 18:07
    Alexandre Locas
    0

    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

  • Alex Ellis-Wilson 1 post 71 karma points
    Oct 24, 2022 @ 16:33
    Alex Ellis-Wilson
    0

    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.

  • Hüseyin 13 posts 103 karma points
    Nov 18, 2023 @ 12:39
    Hüseyin
    0

    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()
    

    enter image description here

  • Hüseyin 13 posts 103 karma points
    Nov 20, 2023 @ 10:00
    Hüseyin
    0

    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");
    

    @DennisAdolfi @RasmusOlofsson

Please Sign in or register to post replies

Write your reply to:

Draft