2

Trying to utilise the PublishedContentRequest method in order to perform several unit tests. As this is separate project purely for unit testing there is no Umbraco context as such.

Just wondering if anyone else has had an issue with testing Umbraco this way. Really need to understand what uri it is expecting as well. For example is is "contact-us", "/contact-us" or "http://localhost/contact-us". Also would like to know how to setup the routingcontext correctly.

Got something like this at the moment:

private static void SetupControllerContext(UmbracoContext umbCtx, ControllerBase controller)
    {
        var contextBase = umbCtx.HttpContext;
        var pcr = new PublishedContentRequest(new Uri("/contact-us"), umbCtx.RoutingContext);

        var routeData = new RouteData();
        var routeDefinition = new RouteDefinition
        {
            PublishedContentRequest = pcr
        };
        routeData.DataTokens.Add("umbraco-route-def", routeDefinition);
        controller.ControllerContext = new ControllerContext(contextBase, routeData, controller);
}

Any pointers would be great.

Thanks

Netferret
  • 604
  • 4
  • 15

1 Answers1

2

There are basically two options. You can setup and Mock Umbraco yourself, or you can get the Umbraco Source from Github and build the project and use their Umbraco.Tests helper library with makes it VERY easy to mock and setup the Umbraco parts just enough for Unit Testing.

There are actually two good articles on this subject.

For an in depth, roll-your-own perspective, this is a good post and tutorial:

Unit Testing Umbraco - Plausible

http://skrift.io/articles/archive/unit-testing-umbraco-plausible/

For a hint at the roll-your-own, but then really more about how to use the Umbraco.Tests library check out this post:

http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/

Both provide their source code on Github so you can play around with their techniques yourself.

Personally, I'm getting into this and have decided to go the Umbraco.Tests library approach. However, I REALLY wish there was a Nuget repository that I could make sure the Tests library was in sync with my Umbraco nuget packages.

BeaverProj
  • 2,205
  • 1
  • 17
  • 31
  • Hi, thanks for this, its much appreciated, I'll aim to pick this up in the week and have a good play, just got a few things that need to be boxed off urgently. It will be good to finally get something in place. Cheers – Netferret Jan 23 '17 at 08:28