Microsoft Fakes and Shims

This is pretty cool that you can use this technique to test around .net framework items like this

// Code under test:
public int GetTheCurrentYear()
return DateTime.Now.Year;
public class TestClass1
public void TestCurrentYear()
int fixedYear = 2000;

// Shims can be used only in a ShimsContext:
using (ShimsContext.Create())
// Arrange:
// Shim DateTime.Now to return a fixed date:
System.Fakes.ShimDateTime.NowGet =
() =>
{ return new DateTime(fixedYear, 1, 1); };

// Instantiate the component under test:
var componentUnderTest = new MyComponent();

// Act:
int year = componentUnderTest.GetTheCurrentYear();

// Assert:
// This will always be true if the component is working:
Assert.AreEqual(fixedYear, year);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s