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



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

主页类如下所示:

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

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

我会尝试使用PageInfo和PageInfoProvider类。

最新更新