Microsoft Fakes
The fakes framework can be used to test any .NET method. Microsoft fakes mocks objects with no preexisting interfaces.
The fakes framework actually provides two ways to create mock objects.
- Shims
- Stubs
Getting started with Sims
Create a new class library in visual studio 2012. And add the class CartToShim
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace FakingExample
{
public class CartToShim
{
publicint CartId { get; set; }
publicint UserId { get; set; }
public ReadOnlyCollection<CartItem> CartItems { get; set; }
private List<CartItem> cartItems = newList<CartItem>();
public DateTime CreateDateTime { get; set; }
public CartToShim(int cartId, int userId)
{
CartId = cartId;
UserId = userId;
CreateDateTime = DateTime.Now;
CartItems = new ReadOnlyCollection<CartItem>(cartItems);
}
public void AddCartItem(int productId)
{
var cartItemId = DataAccessLayer.SaveCartItem(CartId, productId);
cartItems.Add(new CartItem(cartItemId, productId));
}
}
}
Add CartItem class
namespace FakingExample
{
public classCartItem
{
public int CartItemId { get; set; }
public int ProductId { get; set; }
public CartItem(int cartItemId, int productId)
{
CartItemId = cartItemId;
ProductId = productId;
}
}
}
Add the DataAccessLayer class
using System.Data;
using System.Data.SqlClient;
namespaceFakingExample
{
publicstaticclassDataAccessLayer
{
public static int SaveCartItem(int cartId, int productId)
{
using (var conn = newSqlConnection(“”))
{
var cmd = newSqlCommand(“InsCartItem”, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(“@CartId”, cartId);
cmd.Parameters.AddWithValue(“@ProductId”, productId);
conn.Open();
return (int)cmd.ExecuteScalar();
}
}
}
}
Add unti test project to the solution
In the untitest project, add the FakingExample project
Now right click on the FakingExample reference in the unit test project and select “Add Fakes Assembly”
This will create the following references

Now we are ready to do faking.
Add CartToShimTests test class to the test project
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FakingExample.Tests
{
[TestClass]
public class CartToShimTests
{
[TestMethod]
public void AddCartItem_GivenCartAndProduct_ThenProductShouldBeAddedToCart()
{
using (ShimsContext.Create())
{
int cartItemId = 42, cartId = 1, userId = 33, productId = 777;
Fakes.ShimDataAccessLayer.SaveCartItemInt32Int32 = (c, p) => cartItemId;
var cart = new CartToShim(cartId, userId);
cart.AddCartItem(productId);
Assert.AreEqual(cartId, cart.CartItems.Count);
var cartItem = cart.CartItems[0];
Assert.AreEqual(cartItemId, cartItem.CartItemId);
Assert.AreEqual(productId, cartItem.ProductId);
}
}
}
}
Now run the unti test. Notice that our test completely skipped over the SaveCartItem in DataAccessLayer, otherwise we would have received exception saying invalid connection string.
In my next post I will show the Stubs example.
Thanks
Vinay