How To Use The Isolated.Design Property

As you probably know by now, we’ve added a new Design property to the Isolated attribute.The Design property controls how the Isolate APsI work in different circumstances.

The default, implicit value, depends on the version of Isolator you’re using. In the free Basic version, the Design property is set to DesignMode.InterfaceOnly. In the Essential and Complete versions, it’s set to DesignMode.Pragmatic.

You can set the Design property explicitly, if you’re using a strict TDD development pattern. In that case, you want to work with interfaces or abstract classes, and want a notification when you step out of line.

In that case, you probably want to put it in the assembly level:

   1: [assembly: Isolated(Design=DesignMode.InterfaceOnly)]


and that’s it. Every time your tests use something like:

   1: Isolate.Fake.StaticMethods<SomeType>();

You’ll immediately get a DesignModeException reminding you about that line you’ve crossed.

This can work very well if you’re in control of everything. But if you’re using a framework or a third party, that uses static types you might need to change the behavior in that scope. So if , for example, you have a test that checks for DateTime.Now, and you need to fake it, use something like this on the specific test:

   1: [Isolated(Design = DesignMode.Pragmatic)]

   2: [TestMethod]

   3: public void MyTest()

   4: {

   5:    UnderTest tested = new UnderTest();

   6:    Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(2000, 1, 1));


   8:    tested.MethodCallingDateTime();

   9: }


PS did you know that IsolatedAttribute can be set on either assembly, class or method?

Gil Zilberfeld

Leave a reply

Required fields are marked (*)