Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 872 karma points
    Jan 15, 2015 @ 18:04
    jake williamson
    0

    unit testing: creating an 'UmbracoContext ' throws an exception?!

    hey out there,

    we're in the process of getting a project unit tested... we're getting there, the learning curve is steep but it's beginning to make a bit of sense!

    what we've done so far:

    1. got the 'Umbraco.Tests.dll' and config files dropped in and working
    2. worked out how to test the 'GetPropertyValue
    3. got a basic test running!
    4. found some useful stuff online re unit testing http://justthisguy.co.uk/unit-testing-umbraco/ http://web-matters.blogspot.it/2014/03/unit-testing-umbraco-ipublishedcontent.html http://www.bomboca.co.uk/umbraco-cms/unit-testing-umbraco/

    this was working fine with basic testing, however it's gone to pot the moment we've got a class/interface that's using the 'UmbracoHelper' which of course needs the good old 'UmbracoContext'.

    on the advice of many blog posts we've used this to get the context:

    private UmbracoContext GetUmbracoContext()
    {
        var routingContext = GetRoutingContext("/");
        return routingContext.UmbracoContext;
    }
    

    but when we run our tests, we get this:

    Test method Site.Tests.UmbracoTests.TestMethod1 threw exception: 
    System.ArgumentNullException: Value cannot be null.
    Parameter name: applicationContext
    at Umbraco.Web.PublishedCache.XmlPublishedCache.PublishedMediaCache..ctor(ApplicationContext applicationContext)
    at Umbraco.Tests.TestHelpers.BaseDatabaseFactoryTest.GetUmbracoContext(String url, Int32 templateId, RouteData routeData, Boolean setSingleton)
    at Umbraco.Tests.TestHelpers.BaseRoutingTest.GetRoutingContext(String url, Int32 templateId, RouteData routeData, Boolean setUmbracoContextCurrent)
    at Umbraco.Tests.TestHelpers.BaseRoutingTest.GetRoutingContext(String url, RouteData routeData)
    at Site.Tests.UmbracoTests.GetUmbracoContext() in UmbracoTests.cs: line 28
    at Site.Tests.UmbracoTests.TestMethod1() in UmbracoTests.cs: line 55
    

    which has left us a bit stumped!

    has anyone hit this before or has a better solution to creating the 'UmbracoContext'?

    cheers,

    jake

  • jake williamson 207 posts 872 karma points
    Jan 16, 2015 @ 13:37
    jake williamson
    100

    found it!

    did a lot of parallel stepping through code in visual studio with the umbraco test project on one side and ours on the other trying to figure out what was going wrong. line 356 in 'Umbraco-CMS-dev-v7\src\Umbraco.Tests\TestHelpers\BaseDatabaseFactoryTest.cs' was the key: in the umbraco project 'ApplicationContext' was populated, in ours it wasn't.

    we traced that back to the set up and noticed it was using nunit in the Umbraco.Tests.dll...

    in my project i was using the ms test declarations ([TestClass] and [TestMethod]) so i changed them to [TestFixture] and [Test] and guess what?

    one populated UmbracoContext - happy days!

    now all i need to do is get my ms fakes to play nicely so i can shim out the 'GetPropertyValue' extension methods and i'm getting somewhere.

    happy (almost) days ;)

  • Terry Clancy 204 posts 944 karma points
    Apr 19, 2018 @ 22:48
    Terry Clancy
    0

    Hi,

    I have exactly the same problem but moving to NUnit did not resolve it. Please see my detailed question at https://our.umbraco.org/forum/using-umbraco-and-getting-started//91668-umbraco-unit-testing-problems-due-to-null-applicationcontext-umbraco-context

    Thanks

    Terry Clancy

    ClanceZ

  • Terry Clancy 204 posts 944 karma points
    Apr 20, 2018 @ 07:44
Please Sign in or register to post replies

Write your reply to:

Draft