TDD: Mocking with Dummy Objects

Please see my other Test Driven Development articles.

Using custom objects in place of a mocking framework.

When it’s desired to avoid the overhead of a mocking framework and its functionality isn’t required, developers may elect to provide their own dummy objects instead.
In this article I will illustrate a simple example of writing my own dummy object which are mocks that represent an external resource, in this case a database call to return Sale associates’ sales figures.

First, I’ll begin by creating the interface to represent the database call.

1-mocking-with-dummy-objects

Next, I’ll create the SaleAssociate class which contains a dependency of the interface and calls its “GetSalesOutcome().”

2-mocking-with-dummy-objects

Now I’m ready to create my test class which implements the interface and and then another class which creates objects to call “GetSalesOutcome().”
My test first instantiates an object from the class “TestDependency” which implements the interface’s function.
Then, I instantiate an objects while passing to it my TestDependency object.
To configure my test, I’ll first setup a parameter and what I expect the test to return – what constitutes a “Pass.”

Finally, I call the function and pass its results to “Assert.”

3-mocking-with-dummy-objects

Now, I’ll start NUnit and execute my test, it passes!

4-mocking-with-dummy-objects

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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