如何在第三方外部DLL上设置shim属性

时间:2014-04-10 14:40:16

标签: c# unit-testing dll shim

我正在为以下功能编写单元测试..

public virtual FacadeClass InitNewAMSObject()
{
    FacadeClass output = null;

    if (ClassMapInfo != null)
    {
        // Override the metadata caching option
        // sets property in external dll
        Avectra.netForum.Common.Config.CacheMetaData = false;

        if (!String.IsNullOrEmpty(ClassMapInfo.AMSClassName))
        {
            output = DataUtils.InstantiateFacadeObject(ClassMapInfo.AMSClassName);
        }
    }
    else
    {
        throw new System.ApplicationException("Need to add the attribute");
    }

    return output;
}

我无法通过评论“在外部dll中设置属性”过去。我的垫片似乎永远不会“完成”被测试的功能。它总是抛出一个错误,它实际上试图使用dll而不是我的垫片。看起来我只是将属性设置为false,但是dll使用属性就像它的Setter中的方法一样。 .Config始终处于错误状态,因为它的构造函数(在dll中)正在尝试设置数据库连接。我希望它至少处于一个空状态,并且还有.Fakes。在班级名称中。我只是想跳过它,因为我不写这个dll,它的第三方。

我现在已经研究了一天,无法在任何地方找到如何填充在引用的dll上设置的属性的示例。

这是我目前对其价值的测试

[TestMethod]
        public void InitNewAMSObjectTEST()
        {
            using (ShimsContext.Create())
            {

                Address amsc = new Address();

                bool test = true;

                ShimConfig.CacheMetaDataGet = () => test;
                ShimConfig.CacheMetaDataSetBoolean = value => test = value;

                amsc.InitNewAMSObject();

            }

        }

0 个答案:

没有答案