Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

TDD :: TypeMock :: Reference

Posted by Paul on November 3, 2010

Test Driven Development (TDD)

TDD confines to be a good agile methodology.  It will give the long term advantage of maintainability to the code that will give confidence while refactoring.  I believe if properly used there is no much time cost incurred in using TDD, because I a big fan of TDD .

Advantages of TDD

Although it might incur some investment to do TDD in the long run it is definitely advantage especially in product development.  If properly written it should cover all the user code in the application thus making sure changes can be carried with confidence and manageable.  In the long run it will save time and effort multiple times and speeds up the productivity.  Thanks to the creators of TDD.


Mocking of code is required while implementing TDD.  There are multiple scenarios we encounter for mocking.

  • When a method is calling a User Interface element
  • When a method is calling a Web Service
  • When a method is calling another method in same class
  • When a method is calling another method in different class

I will show some TDD scenarios that many times we encounter.  It is generally true that whenever the method is going out of its boundary we should mock that.

Note: All the tests are based on TypeMock which is a licensed software.  You need to refer to TypeMock.dll and TypeMock.ArrangeActAssert.dll

Example1: Simple TDD

public class User


    public bool IsUserLicensed()


        DialogResult result = MessageBox.Show("Have License?", "Prompt", MessageBoxButtons.YesNo);


        if (result == DialogResult.Yes)

            return true;


            return false;



The Test method with mocking would look like:


public void Test_User_HasLicense()


    User target = new User();

    Isolate.WhenCalled(() => MessageBox.Show(string.Empty)).WillReturn(DialogResult.Yes);


    bool result = target.HasLicense();


    Assert.AreEqual(true, result);


Example2: Testing Method which is calling another Private/Public method

In the previous example MessageBox.Show() was the call violating the scope of method IsUserLicensed().  So we mocked it and it was easier as the MessageBox class was accessible from the test method.

But things won’t be the same.  You might be needed to mock private methods too.  In this case we run into a problem like the method would be not accessible outside the class.

There are two ways of solution for this:

1) Create Private Accessor through Visual Studio, specify the method name as it is

2) Pass the method name as string in mocking

We can try with the second way – passing method name as string

public string GetCitizenship() // Public methods


    string citizenship = GetCitizenshipFromDb();


    return citizenship;



private string GetCitizenshipFromDb() // Private Method, we have to mock this method


    return "USA";


The mocking code for the above would look like:


public void GetCitizenshipTest()


    User user = new User();


    Isolate.NonPublic.WhenCalled(user, "GetCitizenshipFromDb").WillReturn("USA");


    string result = user.GetCitizenship();


    Assert.AreEqual("USA", result);


 Example3: Mocking a property which do not have setter

The source code will look like:

public bool IsGetterOnlyPropertyValid()


    return this.IHaveOnlyGetter == "ValidText";



private string _iHaveOnlyGetter;


public string IHaveOnlyGetter


    get { return _iHaveOnlyGetter; }

    // set; – No Setter here


From the above IsGetterOnlyPropertyValid()we can see that inorder to return true, the property IHaveOnlyGetter should be “ValidText”.  We cannot set the property from the test method as there is no setter for the property.

The solution would be a different style as given below.


public void IsGetterOnlyPropertyValidTest()


    User target = new User();


    Isolate.WhenCalled(() => target.IHaveOnlyGetter).WillReturn("ValidText");


    bool result = target.IsGetterOnlyPropertyValid();


    Assert.AreEqual(true, result);



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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s