Search code examples
asp.net-mvcunit-testingkentico

Is there a way to mock Kentico providers and types that have been generated for an mvc application?


I'm starting out in my Kentico developer journey and creating an MVC site. I wish to write unit tests against my Builder classes but they rely upon auto-generated provider classes that come out of Kentico. It seems as if the Kentico.Libraries.Tests package will not work with auto-generated provider classes or types. Does anyone know if this is possible?

I'm following the example shown here : https://docs.kentico.com/k11/custom-development/writing-automated-tests/faking-info-and-provider-objects-in-unit-tests

But not getting very far...due to an exception, that is causing me some confusion. The code :

Fake<HomePage,HomePageProvider>();

throws an exception of:

The type 'CMS.DocumentEngine.Types.HomePage' cannot be used as type 
parameter 'TInfo' in the generic type or method 
'AutomatedTestsWithData.Fake<TInfo, TProvider>(TProvider, bool)'. 

There is no implicit reference conversion from 
'CMS.DocumentEngine.Types.HomePage' 
to 
'CMS.DataEngine.AbstractInfoBase<CMS.DocumentEngine.Types.HomePage>'.

The HomePage Class looks like:

//--------------------------------------------------------------------------------------------------
// <auto-generated>
//
//     This code was generated by code generator tool.
//
//     To customize the code use your own partial class. For more info about how to use and customize
//     the generated code see the documentation at http://docs.kentico.com.
//
// </auto-generated>
//--------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

using CMS;
using CMS.Base;
using CMS.Helpers;
using CMS.DataEngine;
using CMS.DocumentEngine.Types;
using CMS.DocumentEngine;

[assembly: RegisterDocumentType(HomePage.CLASS_NAME, typeof(HomePage))]

namespace CMS.DocumentEngine.Types
{
    /// <summary>
    /// Represents a content item of type HomePage.
    /// </summary>
    public partial class HomePage : TreeNode
    {
        #region "Constants and variables"

        /// <summary>
        /// The name of the data class.
        /// </summary>
        public const string CLASS_NAME = "HomePage";


        /// <summary>
        /// The instance of the class that provides extended API for working with HomePage fields.
        /// </summary>
        private readonly HomePageFields mFields;

        #endregion


        #region "Properties"

        /// <summary>
        /// HomePageID.
        /// </summary>
        [DatabaseIDField]
        public int HomePageID
        {
            get
            {
                return ValidationHelper.GetInteger(GetValue("HomePageID"), 0);
            }
            set
            {
                SetValue("HomePageID", value);
            }
        }


        /// <summary>
        /// This heading shown in the breadcrumb.
        /// </summary>
        [DatabaseField]
        public string PageHeading
        {
            get
            {
                return ValidationHelper.GetString(GetValue("PageHeading"), @"");
            }
            set
            {
                SetValue("PageHeading", value);
            }
        }


        /// <summary>
        /// Header Image.
        /// </summary>
        [DatabaseField]
        public string HeaderImage
        {
            get
            {
                return ValidationHelper.GetString(GetValue("HeaderImage"), @"");
            }
            set
            {
                SetValue("HeaderImage", value);
            }
        }


        /// <summary>
        /// Header Image Alt Text.
        /// </summary>
        [DatabaseField]
        public string HeaderImageAltText
        {
            get
            {
                return ValidationHelper.GetString(GetValue("HeaderImageAltText"), @"");
            }
            set
            {
                SetValue("HeaderImageAltText", value);
            }
        }


        /// <summary>
        /// Title.
        /// </summary>
        [DatabaseField]
        public string HeaderTitle
        {
            get
            {
                return ValidationHelper.GetString(GetValue("HeaderTitle"), @"");
            }
            set
            {
                SetValue("HeaderTitle", value);
            }
        }


        /// <summary>
        /// Gets an object that provides extended API for working with HomePage fields.
        /// </summary>
        [RegisterProperty]
        public HomePageFields Fields
        {
            get
            {
                return mFields;
            }
        }


        /// <summary>
        /// Provides extended API for working with HomePage fields.
        /// </summary>
        [RegisterAllProperties]
        public partial class HomePageFields : AbstractHierarchicalObject<HomePageFields>
        {
            /// <summary>
            /// The content item of type HomePage that is a target of the extended API.
            /// </summary>
            private readonly HomePage mInstance;


            /// <summary>
            /// Initializes a new instance of the <see cref="HomePageFields" /> class with the specified content item of type HomePage.
            /// </summary>
            /// <param name="instance">The content item of type HomePage that is a target of the extended API.</param>
            public HomePageFields(HomePage instance)
            {
                mInstance = instance;
            }


            /// <summary>
            /// HomePageID.
            /// </summary>
            public int ID
            {
                get
                {
                    return mInstance.HomePageID;
                }
                set
                {
                    mInstance.HomePageID = value;
                }
            }


            /// <summary>
            /// This heading shown in the breadcrumb.
            /// </summary>
            public string PageHeading
            {
                get
                {
                    return mInstance.PageHeading;
                }
                set
                {
                    mInstance.PageHeading = value;
                }
            }


            /// <summary>
            /// Header Image.
            /// </summary>
            public string HeaderImage
            {
                get
                {
                    return mInstance.HeaderImage;
                }
                set
                {
                    mInstance.HeaderImage = value;
                }
            }


            /// <summary>
            /// Header Image Alt Text.
            /// </summary>
            public string HeaderImageAltText
            {
                get
                {
                    return mInstance.HeaderImageAltText;
                }
                set
                {
                    mInstance.HeaderImageAltText = value;
                }
            }


            /// <summary>
            /// Title.
            /// </summary>
            public string HeaderTitle
            {
                get
                {
                    return mInstance.HeaderTitle;
                }
                set
                {
                    mInstance.HeaderTitle = value;
                }
            }
        }

        #endregion


        #region "Constructors"

        /// <summary>
        /// Initializes a new instance of the <see cref="HomePage" /> class.
        /// </summary>
        public HomePage() : base(CLASS_NAME)
        {
            mFields = new HomePageFields(this);
        }

        #endregion
    }
}

Solution

  • Might want to look at how they did the tests for the MVC project on GitHub here and see if it helps.