Customizing the system's data classes (Info objects) in Unit Tests

Adam Steffes asked on July 16, 2015 00:09

I've successfully extended the SKUInfo object leveraging this Kentico article.

However, I've been unable to get the extended properties to work in a Unit Test context. I'm using Kentico's Unit Test project, with my test inheriting from CMS.Tests.IntegrationTests, and I have a CMSTestConnectionString setup correctly. My SKUInfo object comes back instantiated and hydrated.

Can someone please help me understand what I need to configure differently so that these properties will return in a unit test context? What's missing?

Here's my setup:

[SKUInfoCustomizationLoaderAttribute]
public partial class CMSModuleLoader
{
    private class SKUInfoCustomizationLoaderAttribute : CMSLoaderAttribute
{
    public override void Init()
    {
        SKUInfo.TYPEINFOSKU.OnLoadRelatedData += SKUInfo_OnLoadRelatedData;
    }

    static object SKUInfo_OnLoadRelatedData(BaseInfo infoObj)
    {
        return SKUInfo_OnLoadRelatedData((SKUInfo)infoObj);
    }

And...

 public class SKUInfoData : IDataContainer

And...

 public static string GetTaxClassification(this SKUInfo skuInfo)
    {
        return  skuInfo.GetStringValue(SKUInfoData.TaxClassificationColumnName, string.Empty);
    }

Correct Answer

Josef Dvorak answered on August 6, 2015 12:42

Hello,

We have resolved the issue using standard support. I am publishing the solution, in case someone else runs into the same issue.

Kentico does not fully initialize before tests are run, so the tests are executed before some parts of the application initialization are finished. To resolve this, you can add an init method to the test, to serve the same purpose as the CMSModuleLoader.Init() method:

[TestInitialize]
public void Init()
{
    SKUInfo.TYPEINFOSKU.OnLoadRelatedData += CMSModuleLoader.SystemObjectCustomizationLoaderAttribute.SKUInfo_OnLoadRelatedData;
}
1 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.