Contents tagged with UnitTesting

  • The Visual Studio 2011 Fakes Framework

    372 comments

    I was just reading this blog post about Unit Testing Improvement in Visual Studio 2011 and noticed that there is something new called Fakes Framework.

    I didn’t get it first time so I had to read it again and also had to read this MSDN article to get my head around it. I mean I was getting so excited about it that I had to read it twice to make sure I have understood it properly. then I went like wow that’s great. now I can test old SharePoint code without TypeMock. Smile

    basically according to MSDN there are two new feature in the Fakes Framework:

    Stub

    types Stub types make it easy to test code that consumes interfaces or non-sealed classes with overridable methods. A stub of the type T provides a default implementation of each virtual member of T, that is, any non-sealed virtual or abstract method, property, or event. The default behavior can be dynamically customized for each member by attaching a delegate to a corresponding property of the stub. A stub is realized by a distinct type which is generated by the Fakes Framework. As a result, all stubs are strongly typed.

    Although stub types can be generated for interfaces and non-sealed classes with overridable methods, they cannot be used for static or non-overridable methods. To address these cases, the Fakes Framework also generates shim types.

    Shim

    types Shim types allow detouring of hard-coded dependencies on static or non-overridable methods. A shim of type T can provide an alternative implementation for each non-abstract member of T. The Fakes Framework will redirect method calls to members of T to the alternative shim implementation. The shim types rely on runtime code rewriting that is provided by a custom profiler.

    according to the blog post mentioned above the Stubs are not like Mocking framework but I am still not sure why I would need a Mocking framework when I can use Stub feature! will spend some more time on this and will update this post.

    but the Wow part for me was the Shim. it seems Microsoft Pex and Molls has given the required motivation for the VS team to include the Shim feature. it has always been a very difficult task to test non abstract classes like the ones provided in SharePoint. even using TypeMock or Pex was not an easy task.

    but Shim sounds very promising. although the syntax is not still quite easy to use-or at least I have not figured it out in last few hours- but it’s a great start.

    here is a small code sample which should throw an Application Exception if it’s first day of year 2000.

     

       1:  public static class Y2KChecker {
       2:      public static void Check() {
       3:          if (DateTime.Now == new DateTime(2000, 1, 1))
       4:              throw new ApplicationException("y2kbug!");
       5:      }
       6:  }

     

    lets say we want to make sure that the following code will throw an Application Exception if current date is 01/01/2000.  but to test this we need to provide an alternative implementation for DateTime.Now as DateTime.Now will always return current system date and time which would never satisfies the if condition.

    as you know we cannot Mock DateTime.Now with mocking framework and here is where Shim comes to picture to provide an alternative return value for DateTime.Now:

       1:  using (ShimsContext.Create()
       2:      // hook delegate to the shim method to redirect DateTime.Now
       3:      // to return January 1st of 2000
       4:      ShimDateTime.NowGet = () => new DateTime(2000, 1, 1);
       5:      Y2KChecker.Check();
       6:  }

     

    now I see the light at the end of all those old SharePoint code which are dying to be unit tested.

    Written by vahid

    Saturday, March 10, 2012 at 10:11 PM

    Tagged with ,