6

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?

Brett Janer
  • 517
  • 1
  • 4
  • 20

2 Answers2

1

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.

Monsignor
  • 2,671
  • 1
  • 36
  • 34
  • Ahh yes, this is what I've been looking for! Unfortunately, it does seem bugged. I've tried on my machine VS2013 and it doesn't work. Marking answered and taking the bug up with Microsoft. Thanks. – Brett Janer Jun 04 '14 at 19:55
0

Create a class that you will use as a superclass for all shims you write. In this class have a

protected static ShimBehavior Default = ShimBehavior.NotImplemented;

Then, make the constructor:

public ShimSuperClass
{
    InstanceBehavior = Default
}

Just make sure to call the base() constructor in all of your subclasses

Michael Kniffen
  • 346
  • 2
  • 7