Pages

Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Sunday, January 20, 2013

Fakes Framework Replace Function Implementation

Visual Studio 2012 introduces Fakes Framework. Currently I need to find a testing framework and Fakes is one of my choice. I cannot find any good tutorial, hopefully this one can give a quick start.

First the C# code for testing is:

 public interface IStock  
   {  
     int F();  
     int F1(int i);  
   }  
   public class Stock : IStock  
   {  
     public int F()  
     {  
       return 1;  
     }  
     public int F1(int i)  
     {  
       return i + 1;  
     }  
   }  

After creating the Test Project from Visual Studio, you can replace the F1 implementation with a new function.

 using (var context = ShimsContext.Create())  
       {  
         System.Fakes.ShimDateTime.NowGet = () => new DateTime();  
         var dt = DateTime.Now;  
         var shim = new ClassLibrary1.Fakes.ShimStock();  
         shim.F = () => 7;  
         var x = shim.Instance.F(); //x is 7 instead of 1  
       }