Category: Visual Studio 2012

Fakes Tutorial 3: Basic Shim Sample

To Begin with Basic example of Shim, We have 3 Projects and these are on same lines as that of previous blogpost “Basic Stub Sample“.

I’ve not used any System dll in this case so that it is easier to relate between differences in Stub and Shim. Projects are:

  1. BasicShim : This is simple class library with one method which is to be tested with Fakes Unit Testing
  2. ToBeShimmed : Contains code for an assembly which is referred in BasicShim Project and will be faked in unit test.
  3. BasicShimUnitTest: Unit test

 

Looking into Projects:

  1. ToBeShimmed

    Opposed to ToBeStubbed project this project does not contain an y Interface. There is just one Class “FetchRowCount”. This class will ideally contain logic which we need to fake.

Class

Here class FetchRowCount will be used in BasicShim class and method FetchNumberOfRows will be called.

 

  1. BasicShim

     

    In BasicShim Class we have method “MethodToTest” which we are interested in testing. In case of Shim we are not passing any Interface object to method, so in unit test instead of passing object we will fake the object at runtime.

     

    Class

  2. BasicShimUnitTest:

     

    Similar to previous example, firstly we’ll add reference to all the assemblies we need, Here it will be “BasicShim” for testing and “ToBeShimmed” for Faking.

     

    After that we’ll need to fake assembly TooBeShimmed for that

            Goto ToBeShimmed in Unit test project, -> References -> Right Click assembly to be faked. -> Click Add Fakes Assembly

     

    Test Method:

    In this sample First thing we’ll have to do is create a ShimContext, this ShimContext is used to fake the assemblies at runtime. Then we’ll set the fake behavior of Method.

    If it’s a static method then use <shim class>.<method>, if it is an instance method use <shim class>.AllInstances.<method> to set faking behavior.

    Now same as in any Object Oriented Paradigm, create object, call the method and get the return value.

    Finally we will assert it with our value i.e. “Average”.


     

    Above is the most basic unit test from fakes which can be done using “shim”.

    Keep reading for more posts around this.

Fakes Tutorial 2: Basic Stub Sample

This is in continuation to “Introduction to Microsoft Fakes

To Begin with Basic example of Stub, We have 3 Projects:

  1. BasicStub : This is simple class library with one method which is to be tested with Fakes Unit Testing
  2. ToBeStubbed : Contains code for an assembly which is referred in BasicStub Project and will be faked in unit test.
  3. BasicStubUnitTest: Unit test

 

Looking into Projects:

  1. ToBeStubbed

    This project Contains One Interface “IFetchRowCount” which is referred in Class “FetchRowCount”. This class will ideally contain logic which we need to fake

 

Interface

Class

Here class FetchRowCount will be used in BasicStub class and method FetchNumberOfRows will be called.

 

  1. BasicStub

     

    In BasicStub Class we have method “MethodToTest” which we are interested in testing. Here important part is that we are passing object “fetch” to method.

    If it’s not implemented like this it will be difficult to test it using stubs (Discussion on how to handle other scenarios are for other postsJ)

     

    Class

  2. BasicStubUnitTest:

     

    First thing to be done in Unit test is to add reference to all the assemblies we need, Here it will be “BasicStub” for testing and “ToBeStubbed” for Faking.

     

    After that we’ll need to fake assembly TooBeStubbed for that

            Goto ToBeStubbed project, -> References -> Right Click assembly to be faked. -> Click Add Fakes Assembly

     

    Test Method:

    Below you can see that in test we have Created the object of Stub class StubIFetchCount generated from interface, Set the return value of method and then passed it to the method to be tested.

    Finally we have asserted it with our value “Average”.


     

    Above is the most basic unit test from fakes which can be done using “stub”. It doesn’t cover much detail but gives overview of how stub works.

    Keep reading for more posts around this.

     

    Next: “Basic Shim Sample

Fakes Tutorial 1: Introduction to Microsoft Fakes

There has been a lots of buzz going around that there is no moles for VS 2012 and now we have to use Microsoft’s OWN unit testing framework.

So to clear that:

  1. MOLES WAS from Microsoft Only.
  2. Fakes is nothing but a differently packaged Moles J. At least from functionality perspective.
  3. You are welcome to use any other Unit testing framework if it fulfills your need and a compatible version is out there for VS 2012.

Now to begin with fakes

What is Fakes?

Fakes is isolation framework for replacing the part of code, which you don’t have control over (like webservices). Using fakes you can write your unit test and make your code believe that the values of uncontrolled part of code are coming from original source only.

Where to use it?

Let’s say we have a web service that counts the no. of rows in a database and send an Integer value to you. Depending on the integer value you show Less for value < 100, Average for value >=100 && value <1000 and More for value >=1000.

Now you can’t predict the value you will get after the service call for a particular condition as values in database constantly change.

In given scenario you will isolate that webservice, Mock it and make your code believe that the value its getting is from original service itself. Depending on Integer value you are giving while mocking you already know the output and you can Assert your unit test case against that.

 

Above example was one of the scenarios where you can use fakes, there can be many more scenarios like isolating your own code that you have written etc…

 

Some Fakes terms.

There are two different type of fakes that you can generate.

  1. Stubs: A stub is generally used to fake the code which is designed in a way that classes are inherited from interfaces.
  2. Shims: Shims are used to point your code at runtime to a method provided in test. So, shims can be used to modify behavior of assemblies.

 

What to use?

An excerpt from MSDN to explain all that’s there to know:

[quote]As a general guide, use stubs for calls within your Visual Studio solution, and shims for calls to other referenced assemblies. This is because within your own solution it is good practice to decouple the components by defining interfaces in the way that stubbing requires. But external assemblies such as System.dll typically are not provided with separate interface definitions, so you must use shims instead.[/quote]

New In Visual Studio 2012

Other than the new Look and feel of Visual Studio 2012 which some may like and some may not there have been various other changes.

Many changes like search bars, quick launch etc… are readily visible to the end user others for the performance improvement standpoint are not on UI level but still observable.

Look at given examples, you will find there many features which made their way into the Visual Studio from “Visual Studio Power Tools” and we are glad they made through.

  1. Collapse all is available out of the box.

    The most frustrating part in Visual Studio for large projects was that while if you had various projects in your solution window then opening and closing all those was a pain.

    Now you have a simple collapse all button available which can make the life a lot easier.


  2. Want to Find something that’s there in Project name, Type name, Data member. Just Search in solution explorer.

  3. Scope to this: Sometimes lots of projects in Solution Explorer make life difficult and confusing while working on a project. Now you can keep just one project in scope and focus on it.

            

    In above scenario you only have one project in focus, like wise you can do for folders as well. To go back to previous state just click little back or home button on top. If you are like me and like shortcuts. Use Ctrl + Left Arrow.

  4. Quick Launch: Speaking on Shortcuts: How would you add a new Project to your solution.

This way? Going to file then add new project

Or this Way? Right clicking on solution and add new project

These are old ways of Life. Try something New.

    Press Ctrl + Q and it will take you to Quick Launch bar on Top left, then start typing what you want to do . Good thing is that it even remembers what you did recently J

  1. As shown above Object Browser can be easily opened using Quick Launch

    And can be used to Navigate through Objects on your projects.

But now forget that as most of the functionality it supports can be done in solution explorer itself and that too in better segregated manner as you can see which object is in which file


  1. Handling many files in Visual Studio has always been challenging, but not anymore.

    You can pin various files together and they will always be first to be displayed In Top bar and you can always go through them quickly.

    Now I’ve opened another web.config but that comes after the pinned files

    The best part is that you can close the solution and reopen it, Pinned files will remember their state.

  2. Navigating through Error in code made easier by filters and search. J

    If you have error while building the solution and you want to filter on a particular document, project etc… life is easier now with little filter on the left.

            

    In above, Filtering it shows error only on current page I am working on.

    There are many more changes in Visual Studio. May be we’ll discuss those in another post.