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.
Next, I’ll create the SaleAssociate class which contains a dependency of the interface and calls its “GetSalesOutcome().”
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.”
Now, I’ll start NUnit and execute my test, it passes!