Search code examples
c#.netmicrosoft-fakes

Can I Change Default Behavior For All Shims In A Test


I'm currently writing unit tests using Microsoft Fakes framework. I've noticed that if I don't supply a delegate for a given property, the default value is returned. However, when initializing my shim I change this behavior to NotImplemented.

var myShim = new ShimMyClass
{
    InstanceBehavior = ShimBehaviors.NotImplemented
}

This is the given behavior I want for all my shims I define. I'd like to be able to set this default at a more global level, instead of having to remember to do it for every shim I create. Is this possible?


Solution

  • According to documentation, the following code snippet could help you:

    using (ShimsContext.Create())
    {
         ShimBehaviors.Current = ShimBehaviors.NotImplemented;
    
         var myShim = new ShimMyClass
         { 
             ... 
         }
    }
    

    However, it doesn't work on my machine with VS2012. Perhaps it is a bug.