Question

Is it possible to shim a static property in a static class using Microsoft Fakes framework?

If not, Why would a stub work, but not a shim?

Here is my class code:

public static class Globals
{
    public static List<string> greatStations = new List<string>();
}

Here is my test code:

[TestMethod]
public void TestMethod1()
{
    using (ShimsContext.Create())
    {
        var shim = new Fakes.ShimGlobals { };
        Fakes.StubGlobals.greatStations = new List<string>();
        // Next line complains that ShimGlobals does not contain a definition for 'greatStations'
        Fakes.ShimGlobals.greatStations = new List<string>(); 
    }
}
Was it helpful?

Solution

With Fakes, you can stub virtual properties and methods (of classes and interfaces). You can shim non-virtual properties and methods (instance and static). Fields don't need to be shimmed or stubbed because they can be set directly, as shown below.

Globals.greatStations = new List<string>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top