【问题标题】:Is there a way to mock Kentico providers and types that have been generated for an mvc application?有没有办法模拟为 mvc 应用程序生成的 Kentico 提供程序和类型?
【发布时间】:2018-08-10 23:32:01
【问题描述】:

我开始了我的 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>'.

HomePage 类如下所示:

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

【问题讨论】:

    标签: asp.net-mvc unit-testing kentico


    【解决方案1】:

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

    【讨论】:

    • 对于其他发现此问题的人,如果您不想查看 github TLDR:使用 Kentico.Libraries.Tests nuget,让您的测试类从“UnitTests”扩展。然后使用:Fake().DocumentType&lt;Model&gt;(Model.CLASS_NAME);
    • 除了@Timtek 的评论 - 您需要调用 UnitTests.UnitTestsSetUp();这有助于在您运行单元测试时设置 Kentico 初始化属性。
    【解决方案2】:

    我会尝试使用PageInfoPageInfoProvider 类。

    【讨论】:

    • PageInfoProvider 不适用于 ContentOnly 页面(Kentico 9 到 11)
    猜你喜欢
    • 2014-06-04
    • 2020-02-17
    • 2020-02-09
    • 2014-02-27
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多