Copied to clipboard

Flag this post as spam?

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


  • Dennis Flæng Jørgensen 35 posts 145 karma points c-trib
    Nov 23, 2016 @ 18:55
    Dennis Flæng Jørgensen
    0

    Unit Testing and mocking UmbracoHelper

    Hey guys.

    How do you guys Unit Test your Umbraco Solutions?

    All my viewmodels fail because UmbracoContext is null and cannot create a UmbracoHelpr without it. So I was wondering: is there any way to mock the UmbracoContext or what alternatives are there to setup unit tests for Umbraco?

    I have read some pretty old posts on the Umbraco forum but the best I've found was to grab a file/class from the source code (because it has some internal methods that are needed to create the UmbracoContext), which seems highly unnecessary (or at least it should be, IMO).

  • Phil Whittaker 63 posts 267 karma points MVP 3x c-trib
    Dec 01, 2016 @ 11:04
    Phil Whittaker
    0

    I have the same problem, what unit testing framework are you using? I use Rhino Mocks and it was OK stubbing a UmbracoHelper but I experienced real difficulties when I came across extension methods. I hear that TypeMock or there is a Ms solution as part of Visual Studio Premium, both are paid for options.

    Just thinking about it another solution may be to use the new Model Builder (or this for older Umbraco versions https://github.com/zpqrtbnk/Zbu.ModelsBuilder) to encapsulate all the UmbracoHelper calls in auto generated code and then mock these models when testing your viewmodels.

    Let me know what you think...

  • Gary Devenay 39 posts 245 karma points
    Feb 01, 2017 @ 10:31
    Gary Devenay
    1

    Hey Dennis, I released a NuGet package to do exactly this. You can find it at https://www.nuget.org/packages/GDev.Umbraco.Testing/ and also some examples on the github repo https://github.com/garydevenay/Umbraco-Context-Mock

    Hope this is helpful!

Please Sign in or register to post replies

Write your reply to:

Draft