Copied to clipboard

Flag this post as spam?

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


  • Jakob Lithner 61 posts 264 karma points
    Aug 16, 2018 @ 12:10
    Jakob Lithner
    0

    How to get hold of UmbracoHelper in unit test

    Is there any way I could get hold of the UmbracoHelper in unit test?

    I have managed to set up an ApplicationBase that can be used in unit tests by following this example: https://github.com/thenerdery/UmbracoVault/wiki/Accessing-Umbraco-data-outside-the-Umbraco-Request-Pipeline

    I tried to use the explicit instantiation of UmbracoHelper:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current)
    

    But ApplicationBase.Context is not available so I can not use it to feed the constructor.

  • Rami H 13 posts 141 karma points
    Aug 16, 2018 @ 14:53
    Rami H
    0

    Hi Jakob, In the link you posted, the read me says in its limitations section that this lacks the concept of "Current". That it won't be available in that execution context.

    See Limitations

    Are you trying to find a workaround? Like to hack one together? Looks like this is directly querying the Db, so it doesn't read from umbraco.config at all. Hope that helps.

  • Jakob Lithner 61 posts 264 karma points
    Aug 20, 2018 @ 12:06
    Jakob Lithner
    0

    Thanks for answering! I am aware of the limitations of the above approach.

    My question is still valid: Can you help me to get access to the UmbracoHelper service from a unit test?

    I prefer to experiment my data read out in unit tests to understand the data models retrieved. I understand UmbracoHelper is now the preferred method to retrieve data. Right now I need to use the TypedContent and TypedMedia methods of the UmbracoHelper class.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Aug 20, 2018 @ 12:38
  • Rami H 13 posts 141 karma points
    Aug 22, 2018 @ 21:30
    Rami H
    0

    Heck yeah, way to be Steve. I'm bookmarking this!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 20, 2018 @ 19:56
    David Brendel
    0

    Hi Jakob,

    there are special nuget packages to help do exactly that. Mock the (entire) stuff for you. Try a quick search for umbraco context mock. There are articles in the 24days website and some blog posts.

  • Jakob Lithner 61 posts 264 karma points
    Aug 21, 2018 @ 07:17
    Jakob Lithner
    0

    Thanks for your answer! It looks like a clean way to mock context and helper objects.

    But in my current project I was actually looking for a way to retrieve real data with the UmbracoHelper class. In that sense my "unit tests" are not really unit tests, but only a convenient way to experiment with data in debug mode to understand what data is actually returned :)

    For this scenario I have decided to use the service classes. I just need to make sure I use another approach for data retrieval in the real code, with UmbracoHelper to ensure I use the provided caching.

    For real unit tests I will look into your suggested approach.

Please Sign in or register to post replies

Write your reply to:

Draft