Copied to clipboard

Flag this post as spam?

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


  • Keith Jackson 183 posts 552 karma points
    Mar 28, 2014 @ 18:45
    Keith Jackson
    0

    Unit Testing Broken in Umbraco 7

    I've recently upgraded my site from U6 to U7 and, so far, I've found the ability to unit test my code has been severely hampered.

    The introduction of extension methods and internal constructors in parts of the codebase I've been talking to has made mocking and testing practically impossible in some cases, whereas I had a nearly 90% coverage in Umbraco 6. This seems like a really nasty step backward.

    I used to be able to set expected property values in my test using a test extension method. Since upgrading to U7 I now find that every time .HasProperty() is called on an item of content it calls through to the ContentType object like this...

        /// <summary>
        /// Gets a value indicating whether the content has a property identified by its alias.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="alias">The property alias.</param>
        /// <returns>A value indicating whether the content has the property identified by the alias.</returns>
        /// <remarks>The content may have a property, and that property may not have a value.</remarks>
        public static bool HasProperty(this IPublishedContent content, string alias)
        {
            return content.ContentType.GetPropertyType(alias) != null;
        }
    

    This is in an extension method, so I can't mock it directly. OK, so the next thing I try and do is mock ContentType, but ContentType is of type PublishedContentType and implements no interfaces, so I try to create a fake but there are no public constructors.

    This is a bit of a nightmare to say the least so, in order to test my code I can no longer use the 'HasProperty' method at all, so instead I have to use 'GetProperty' and check it's null just so my test will work.

    I'm not sure whether this is the best place to air this, but it would be much more helpful to keep classes like this open so at least you leave some route to enable proper testing.

  • Keith Jackson 183 posts 552 karma points
    Mar 28, 2014 @ 19:11
    Keith Jackson
    100

    For others finding this, I've found that the public static bool HasValue(this IPublishedContent content, string alias) extension method is a little less painful as an alternative. You still can't mock it, but it uses GetProperty internally instead of GetPropertyType.

    This may be the key change in this area that's happened since my previous 6.x version.

  • Keith Jackson 183 posts 552 karma points
    Mar 31, 2014 @ 20:02
    Keith Jackson
    0

    I've raised this as a general issue at http://issues.umbraco.org/issue/U4-4572 in terms of the usage of extension methods vs a base class. Again, time permitting I'd be happy to help look into this.

Please Sign in or register to post replies

Write your reply to:

Draft