有没有办法模拟为 mvc 应用程序生成的 Kentico 提供程序和类型?

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

我正在开始我的 Kentico 开发之旅并创建一个 MVC 站点。 我希望针对我的 Builder 类 编写单元测试,但它们依赖于来自 Kentico 的自动生成的提供程序 类。 Kentico.Libraries.Tests 包似乎无法与自动生成的提供程序 类 或类型一起使用。有谁知道这是否可能?

我正在按照此处显示的示例进行操作:https://docs.kentico.com/k11/custom-development/writing-automated-tests/faking-info-and-provider-objects-in-unit-tests

但没走多远...由于异常,这让我有些困惑。 代码:

Fake<HomePage,HomePageProvider>();

抛出异常:

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>'.

主页 Class 看起来像:

//--------------------------------------------------------------------------------------------------
// <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
    }
}

我会尝试和 PageInfo and PageInfoProvider 类 一起玩。

可能想看看他们是如何在 GitHub here 上对 MVC 项目进行测试的,看看是否有帮助。