设置ShimFileCreationInformation对象的属性

时间:2014-01-03 07:05:12

标签: c# unit-testing sharepoint microsoft-fakes sharepoint-clientobject

我正在使用fakes框架编写一些单元测试用例。我正在使用ShimFileCreationInformation命名空间中的对象Microsoft.SharePoint.Client.Fakes。现在,我将此对象传递给函数。在函数内部,我正在尝试为Url属性赋值。

fileCreationInformation.Url = value;

但即使该值存在,也没有任何内容正确地分配给Url并且它仍为空。这个问题有解决方法吗?更糟糕的是,ShimFileCreationInformation对象上没有可用的文档。

代码示例:

ShimFileCreationInformation fileCreationInformation = new ShimFileCreationInformation();
SomeFunction(fileCreationInformation);

SomeFunction:

public void SomeFunction(FileCreationInformation fileCreationInformation)
{
     fileCreationInformation.Url = value; // This statement had so effect on fileCreationInformation.Url
}

1 个答案:

答案 0 :(得分:0)

fileCreationInformation.Url = value;

如上所述直接设置值将不起作用,因为您设置的是Shim的值而不是实际的对象。您需要使用ShimFileCreationInformation.AllInstances.UrlGet,因此每当调用Url Get时,它都会返回您指定的值。

您的代码应如下所示:

[TestMethod]
public void derived_test()
{
    using (ShimsContext.Create())
    {
        ShimFileCreationInformation fileCreationInformation = new ShimFileCreationInformation();

        ShimFileCreationInformation.AllInstances.UrlGet = (instance) => value;

        SomeFunction(fileCreationInformation);
    }
}

public void SomeFunction(FileCreationInformation fileCreationInformation)
{
    var url = fileCreationInformation.Url; 

    // Check url variable above. It should be set to value

    fileCreationInformation.Url = value; // This statement will not work since you are trying to set the value of the Shim and you need to use `ShimFileCreationInformation.AllInstances.UrlGet` to set property value for Shims
}