Copied to clipboard

Flag this post as spam?

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


  • Desley 44 posts 169 karma points
    Jun 19, 2014 @ 11:26
    Desley
    0

    Unit Testing

    Hi,

    I am trying to create my first unit tests. I have the following code:

        [TestClass]
        public class ClosestAdvertisementTest
        {
            private ClosestAdvertisementsSurfaceController target;
           [TestInitialize]
            public void Initialize()
            {
                target = new ClosestAdvertisementsSurfaceController();
            }
    
           [TestMethod]
            public void Calculate_Distance_Between_Two_Coordinates_In_Km()
            {
                //arrange
                const double latitude1 = 52.1107526;
                const double longitude1 = 5.118116999999984;
                const double latitude2 = 52.1371316;
                const double longitude2 = 5.572416299999986;
                const double expected = 31.15;
    
                //act
                double actual = target.CalculateDistance(latitude1, longitude1, latitude2, longitude2);
    
                //assert
                Assert.AreEqual(expected, actual, 1);
            }
    }
    

    The test fails and says the umbracoContext cannot be null. I found this post http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/48015-Unit-Testing-Surface-Controllers saying I need the umbraco.test.dll but I cannot find it anywhere.

    Hopefully someone can help me out.

  • kim Thomsen 59 posts 277 karma points
    Jun 19, 2014 @ 11:47
    kim Thomsen
    1

    You can finde the umbraco.test.dll on Github

    https://github.com/umbraco/Umbraco-CMS

    Just download and build the project

     

  • Desley 44 posts 169 karma points
    Jun 19, 2014 @ 13:23
    Desley
    0

    I added the .dll but still having trouble with the umbracoContext. The following code:

    UmbracoContext umbracoContext = UmbracoContext.Current
    

    is null. How can I create a proper UmbracoContext?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 19, 2014 @ 16:26
    Andy Butland
    0

    I wrote a couple of blog posts on this problem recently.  There's perhaps not an ideal solution, but some techniques that might help you:

    http://web-matters.blogspot.it/2014/03/unit-testing-umbraco-surface-controllers.html

    http://web-matters.blogspot.it/2014/04/unit-testing-umbraco-with-base-test-classes.html

    In your case I think I'd look at the first one - move your method that does the calculating distance into its own class, that needs (as far as I can see) to have no reference to Umbraco, and write your tests around that.  Then have your controller take an instance of that class as a dependency, or new one up in your controller's constructor or CalculateDistance method, and call out to your new class for the functionality.

    In that case you would be unit testing the core function you want to test, but not testing the controller.  However given the controller method remaining would be extremely simply, there's much less value in doing so.

    Hope that helps.

    Andy

Please Sign in or register to post replies

Write your reply to:

Draft