4

I am writing some unit test cases using fakes framework. I am using an object ShimFileCreationInformation from Microsoft.SharePoint.Client.Fakes namespace. Now, I pass this object to a function. Inside the function, I am trying to assign a value to the Url property.

fileCreationInformation.Url = value;

But even though the value is present, nothing gets assigned to Url properly and it remains null. Is there any workaround for this problem? To make things worse, there is not documentation available on ShimFileCreationInformation object.

Code sample:

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

SomeFunction :

public void SomeFunction(FileCreationInformation fileCreationInformation)
{
     fileCreationInformation.Url = value; // This statement had so effect on fileCreationInformation.Url
}
Aditi
  • 1,188
  • 2
  • 16
  • 44

1 Answers1

0

fileCreationInformation.Url = value;

Setting the value directly as above will not work since you are setting the value of the Shim and not the actual object. You need to use ShimFileCreationInformation.AllInstances.UrlGet so thay whenever the Url Get is called it will return the value you specify.

Your code should look something like below:

[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
}
Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39