Shim and InstanceBehavior fallthrough to isolate part of the SUT

I’ve dealt in a previous post with the new Shim library in Vs11 that permits you to test “difficult to test code” and I showed a really simple example on how to use Shim to isolate the call to DateTime.Now to simulate passing time in a Unit Test. Now I want to change a little bit the perspective of the test, in the test showed in previous post I simply exercise the sut calling Execute() a couple of time, simulating the time that pass between the two calls. Here is the test

[Fact]
public void Verify_do_not_execute_task_if_interval_is_not_elapsed()
{
    using (ShimsContext.Create())
    {
        Int32 callCount = 0;
        PerformHeavyTask sut = new PerformHeavyTask(10, () => callCount++);
        DateTime startDate = new DateTime(2012, 1, 1, 12, 00, 00);
        ShimDateTime.NowGet = () =>  startDate;
        sut.Execute();
        ShimDateTime.NowGet = () => startDate.AddMinutes(9);
        sut.Execute();
        Assert.Equal(1, callCount);
    }
}

I can also change the point of view and write a test that uses a shim to isolate the SUT, this is a less common scenario but it can be also really interesting, because it shows you how you can write simple White box Unit Tests isolating part of the SUT. The term White Box is used because this kind of Unit Test are created with a full knowledge of the internal structure of the SUT, in my situation I have a private method called CanExecute() that return true/false based on the interval of time passed from the last execution and since it is private it makes difficult for me to test the SUT.

private Boolean CanExecute() {

    return DateTime.Now.Subtract(lastExecutionTime)
        .TotalMinutes >= intervalInMinutes;
}

But I can create a Shime on the SUT and isolate calls to the CanExecute(), making it return the value I need for the test, here is an example

[Fact]
public void Verify_can_execute_is_honored()
{
    using (ShimsContext.Create())
    {
        Int32 callCount = 0;
        PerformHeavyTask sut = new PerformHeavyTask(10, () => callCount++);
        ShimPerformHeavyTask shimSut = new ShimPerformHeavyTask(sut);
        shimSut.InstanceBehavior = ShimBehaviors.Fallthrough;
        shimSut.CanExecute = () => false;
        sut.Execute();
        Assert.Equal(0, callCount);
    }
}

To write this test I’ve added another fake assembly on the assembly that contains the PerformHeavyTask class to create shim for the SUT. This test basically create a ShimPerformHeavyTask (a shim of my SUT) passing an existing instance to the SUT to the constructor of the shim, then I set the InstanceBehavior to ShimBehaviors.Fallthrough to indicate to the Shim Library to call original SUT method if the method was not isolated. At this point I can simply isolate the call to the CanExecute() private and non-virtual method, specifying to the shim to return the value false, then I call the Execute() method and verify that the heavy task is not executed.

This test shows how to create a shim of the SUT to isolate calls to its private methods, thanks to the Fallthrough behavior; if you forget to change the InstanceBehavior the test will fail with an exception of type ShimNotImplementedException, because the default behavior for a Shim is to throw an exception for any method that is not intercepted. Thanks to shim library you can simply isolate every part of the SUT, making easier to write Unit Test for classes written with no TDD and no Unit Testing in mind.

Gian Maria.

Using Shims in Visual Studio 11 to test untestable code.

I always strive myself to write testable code, but it is not always easy, especially if you do not follow TDD red-green-refactor mantra. Code written without Unit Testing in mind is usually not so easy to test and when is time to modify code written by other, if you want to create a safety net of Unit Tests to avoid breaking code it is usually an hard task. The main problem is hardcode dependency from static functions because you cannot test a single function or method in isolation, forcing you to use pattern like Back door manipulation that makes test difficult to read, write and maintain. Lets take this code as example

    public class PerformHeavyTask 
    {
        private DateTime lastExecutionTime = new DateTime(1900, 1, 1);

        private Int32 intervalInMinutes;

        private Action task;

        public PerformHeavyTask(Int32 intervalInMinutes, Action task) {

            this.intervalInMinutes = intervalInMinutes;
            this.task = task;
        }

        private Boolean CanExecute() {

            return DateTime.Now.Subtract(lastExecutionTime).TotalMinutes >= intervalInMinutes;
        }

        public void Execute() {
            if (CanExecute())
            {
                lastExecutionTime = DateTime.Now;
                task();
            }
        }
    }

This is a simple and stupid example class that is used to execute an action no more than once every X minutes. The main problem is dependency from the DateTime.Now static function because it create a dependency to the concept of “passing time” that is almost impossible to manage in a test. Suppose that the logic of CanExecute method is complex and depends not only from DateTime.Now or uses some strange algorithm based on time, how can you write a unit test that is capable of testing this algorithm simulating the time that pass? The only solution is being able to change at runtime the behavior of the DateTime.Now static function, a trick that is possible using various external library, but that is now natively available in Visual Studio 11.

This functionality is derived from Pex and Moles project, but now it is fully integrated in VS11, with small differences. First of all you need to right click the reference to the system assembly and choose to Add Fakes Assembly; this creates a special folder called Fakes, with a single file called system.fakes.

26-04-2012 19-40-41

Figure 1: Adding a fake assembly for the System assembly.

Now you can write this simple test that is capable of changing the value of the DateTime.Now static property at runtime

[Fact]
public void Verify_do_not_execute_task_if_interval_is_not_elapsed()
{
    using (ShimsContext.Create())
    {
        Int32 callCount = 0;
        PerformHeavyTask sut = new PerformHeavyTask(10, () => callCount++);
        DateTime startDate = new DateTime(2012, 1, 1, 12, 00, 00);
        ShimDateTime.NowGet = () =>  startDate;
        sut.Execute();
        ShimDateTime.NowGet = () => startDate.AddMinutes(9);
        sut.Execute();
        Assert.Equal(1, callCount);
    }
}

As you can see this code is written in xUnit because VS11 is capable of running Unit Test from external framework if there is the corresponding adapter, so I’m not restricted to use MSTest if I want to use some UnitTesting specific feature of Visual Studio. The first interesting line is the ShimsContext.Create() call, used to create a scope where we can use Shims and isolate calls to non-virtual functions. The concept of Isolation is being able to change how a static property or method behave without the need to change the original code. As you can see I create an instance of the PerformHeavyTask class, with 10 minutes interval and with a function that basically only increment a local variable to have a count of how many times the function is executed. Now to test that the function is not called a second time if not enough time is passed I need to isolate the call to DateTime.Now to return predetermined values.

The line ShimDateTime.NowGet = … permits me to intercepts the getter of the static Now property of DateTime Class and specify the function that will be used instead of standard getter. Shim library works with some convention and since I’ve created a Fake Assembly of the standard System reference, the Shim library will create for me a Shim class for every type in the Assembly prepending the world Shim to the original name. Since I need to change the behavior of the Now static property of DateTime class, I need to use the ShimDateTime class (created for me with the Add fakes assembly command) and use the NowGet property to isolate the Getter of the static property Now. To isolate the getter I simply specify the lambda function to call whenever any code calls DateTime.Now.

The test simple calls the Execute() method the first time, then change isolation function to returns a DateTime that is 9 minutes greater than the previous value, finally I call the Execute() method again and verify that the taks passed to PerformHeavyTask was executed only 1 time, because when I invoked Execute() the second time not enough time has passed and the function should not be executed.

Gian Maria.

VS11 gives you a fantastic XAML Editor

XAML editing in Visual Studio 2010 is not one of the most exiting experience you can have, it works, but compared to Blend it is really poor. One of the thing I really do not like is the lack of information on design time data. Since I really develop every XAML application with MVVM or MVVM like approach, I always have design time data and I’d like Visual Studio to be able to use it during binding operation.

You should be happy because VS11 incorporates a Blend Like designer, that permits you a really improved XAML editing experience. Suppose you insert a Grid inside the XAML page and want to bind to a property of corresponding ViewModel that contains all Customers returned from a search. You should simply select the DataGrid, push the little square related to the ItemsSource property and choose Create Data Binding

image

Figure 1: Blend-like interface in VS11 editor

Now a really nice UI appears that lists all the properties of the ViewModel, so you can immediately choose the one you need. The nice part is that VS11 now shows you the full name of the ViewModel and permits you even to choose Value Converter if you need.

image

Figure 2: Choose the property of the VM you want to bind to and a ValueConverter if needed.

The interesting aspect is that you can choose from different type of Binding type, as shown in Figure 3.

image

Figure 3: Binding type

You can easily create standard binding based on FindAncestor, previous data and many others. The really interesting part is that, once you bound the ItemsSource property of DataGrid to the Clients property of the view model, you can simply add a DataGridTextColumn and again choose the property of the Client object to bind to directly in the Designer.

image

Figure 4: The designer understand the type of each Item and permits you to choose the property to bind to

This makes really simple to develop a nice XAML interface directly from VS11 editor.

Gian Maria.

Interesting links for Visual Studio 11 and TFS 11

Yesterday Microsoft released a really huge wave of tools as well as the consumer preview of the upcoming operating system Windows 8. For Developers there are a lots of news to learn, especially on the new version of Visual Studio called VS11, so I decided to create a blog post where I’ll try to list all interesting links related to VS11 Beta. I’ll try to keep this post updated

Where to download

Primary download location for Visual Studio Alm11 (VS + TFS):
Virtual Machine by Brian Keller with everything installed and hands on labs:
Windows 8 Server Consumer Preview
Windows 8 Consumer Preview

Upgrade process

Updating an Upgraded Team Project to Access New Features
Upgrade TFS 2010 to TFS11 beta
Upgrade TFS 2010 to TFS 11 beta step by step and it’s prerequisites
Another Upgrade guide from NorthWest Cadence

Licensing

Even better access to Team Foundation server

Learning Resources

Frequently Asked Questions
Visual studio 11 Beta Product guide

Welcome to Visual Studio 11 ALM Rangers Readiness Beta WAVE ( a lot of codeplex solutions to obtain the maximum from TFS11)
What’s new for Microsoft Test Manager in Visual Studio 11 Beta (a good overview of improvement for tester in VS11)
What’s new in VS-TFS 11 – Part 1
Unit Test Plugin List (a list of the unit test adapter to run xUnit and Nunit test inside the new test runner)
Pratical Kanban Guidance for TFS11 by ALM Rangers
Working on multiple project how can I see what I need to do at a glance
Visual Studio 11 Beta – What is new in Coded UI Test?
Upcoming Free Webinars and Workshops from Imaginet
Visual Studio 11 Beta Testing Documentation
Tfs 11 Process Template customization guidance
New Videos: Upgrading to Team Foundation Server 11
What’s new in Visual Studio 11 Beta Unit Testing
Update or redirect custom assemblies

Some blog post from Northwest Cadence

Gian Maria

Visual Studio 11 beta is out.

Finally the beta version of Visual Studio 11 is out, toghter with TFS 11 and windows 8 consumer preview. You can find informations and links to download here, you can also find standalone installer for various components of Visual Studio ALM at this address. All download can be found at the home page of the Visual Studio 11 Beta downloads.

For those ones that wants to read information about this new version there is a nice PDF full of information at this address ( http://go.microsoft.com/fwlink/?linkid=243994 ). You can find some nice video from Nortwest Cadence

If you are interested I suggest you to follow the Visual Studio Blog (http://blogs.msdn.com/b/visualstudio/archive/2012/02/29/visual-studio-11-beta-available-now.aspx).

As usual, mister Brian Keller published a virtual machine with all this goodness preinstalled and configured, with hands on labs and more, you can find everything you need here (http://blogs.msdn.com/b/briankel/archive/2011/09/16/visual-studio-11-application-lifecycle-management-virtual-machine-and-hands-on-labs-demo-scripts.aspx)

Happy VS11

Gian Maria