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 |
<span id="lnum1" style="color: rgb(96, 96, 96);"> 1:</span> [assembly: Isolated(Design=DesignMode.InterfaceOnly)] |
and that’s it. Every time your tests use something like:
1 |
<span id="lnum1" style="color: rgb(96, 96, 96);"> 1:</span> 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 |
<span id="lnum1" style="color: rgb(96, 96, 96);"> 1:</span> [Isolated(Design = DesignMode.Pragmatic)] |
1 |
<span id="lnum2" style="color: rgb(96, 96, 96);"> 2:</span> [TestMethod] |
1 |
<span id="lnum3" style="color: rgb(96, 96, 96);"> 3:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> MyTest() |
1 |
<span id="lnum4" style="color: rgb(96, 96, 96);"> 4:</span> { |
1 |
<span id="lnum5" style="color: rgb(96, 96, 96);"> 5:</span> UnderTest tested = <span style="color: rgb(0, 0, 255);">new</span> UnderTest(); |
1 |
<span id="lnum6" style="color: rgb(96, 96, 96);"> 6:</span> Isolate.WhenCalled(() => DateTime.Now).WillReturn(<span style="color: rgb(0, 0, 255);">new</span> DateTime(2000, 1, 1)); |
1 |
<span id="lnum7" style="color: rgb(96, 96, 96);"> 7:</span>  |
1 |
<span id="lnum8" style="color: rgb(96, 96, 96);"> 8:</span> tested.MethodCallingDateTime(); |
1 |
<span id="lnum9" style="color: rgb(96, 96, 96);"> 9:</span> } |
PS did you know that IsolatedAttribute can be set on either assembly, class or method?