Copied to clipboard

Flag this post as spam?

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


  • Michael Hyldsgaard 64 posts 124 karma points
    Jan 21, 2014 @ 11:29
    Michael Hyldsgaard
    0

    Unit testing a web api controller

    I'm trying to setup a unit test of this web api controller.

    namespace customer.Controllers
    {
        public class UsersApiController : UmbracoApiController
        {
            [HttpGet]
            public string GetUser()
            { return "Michael"; } } }

    My very simple unit test looks like this:

    namespace customer.UnitTest
    {
        [TestClass]
        public class TestUsers
        {
            [TestMethod]
            public void GetUser_ShouldReturnMichael()
            { var testValue = "Michael";
                var controller = new customer.Controllers.UserUsersApiController();
                var result = controller.GetUser();
                Assert.AreEqual(testValue, result);
            }
        }
    }

    When I run this very simple test I get the following error:

    Result Message:  
    Test method customer.UnitTest.TestUsers.GetUser_ShouldReturnMichael threw exception: 
    System.ArgumentNullException: Value cannot be null.
    Parameter name: umbracoContext
    Result StackTrace:
    at Umbraco.Web.WebApi.UmbracoApiController..ctor(UmbracoContext umbracoContext)
       at Umbraco.Web.WebApi.UmbracoApiController..ctor()
       at customer.Controllers.UsersApiController..ctor()
       at customer.UnitTest.TestUsers. GetUser_ShouldReturnMichael() in xxx

    It is the line:

    var controller = new customer.Controllers.UserUsersApiController();

    that triggers the error but I can't seem to find out why.

    Am I missing something?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 21, 2014 @ 12:06
    Jeroen Breuer
    0

    Looks like the UmbracoContext is empty. I know that Umbraco also have some unit tests in the source code. Maybe check those out to see if they have any UmbracoApiController tests.

    Jeroen

  • Michael Hyldsgaard 64 posts 124 karma points
    Jan 21, 2014 @ 12:43
    Michael Hyldsgaard
    0

    Hey Jeroen.

    Thanks for leading me in the right direction.

    There's actually a great article on this issue with a description on how to obtain an UmbracoContext when unit testing.

    https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/vEjdzjqmtsU

    //Michael

  • Lennart Stoop 304 posts 842 karma points
    Jun 28, 2014 @ 20:06
    Lennart Stoop
    0

    Hi Michael,

    How did you eventually end up testing your controllers?

    I have tried adding a constructor to my controllers which accepts an UmbracoContext but I'm not sure how to mock this object.

    Grtz

    L

  • Michael Hyldsgaard 64 posts 124 karma points
    Jun 28, 2014 @ 20:19
    Michael Hyldsgaard
    0

    I actually kind of gave up on testing controllers that is using the context from Umbraco. I'm trying to seperate my code as much as possible so I'll be able to test the most important parts of it.

    /Michael

  • Lennart Stoop 304 posts 842 karma points
    Jun 28, 2014 @ 20:39
    Lennart Stoop
    0

    Hmmm I can't seem to catch a break either, testing code seperately is fair enough for now.

    Adding a constructor is not a good idea btw, that seems to break the service when running within Umbraco.

    Thanks for quick reply :)

  • Jose Rojas 1 post 21 karma points
    Feb 18, 2015 @ 10:37
    Jose Rojas
    0

    I am trying to do the very same thing. I added the Umbraco.Tests.dll to the test project, inherited my test class from BaseRoutingTest and set the UmbracoContext by calling GetRoutingContext("/test").UmbracoContext. Still, I could not get it working as it complains about some missing config files. Any ideas?

Please Sign in or register to post replies

Write your reply to:

Draft