Using Mock Objects in C#

When you need to perform unit testing in an independent way, you use mock objects. Mock objects are simulated objects that mimic the behavior of real objects in controlled ways. In this excerpt of their book “Pragmatic Unit Testing in C# with NUnit”, Andy Hunt, Dave Thomas and Matt Hargett explain how to avoid coupling when you build your C# unit tests.

Instead of testing against the real code, you can use a mock object that is kind of close to the real code but easier to work with for your unit testing objectives. The explain that mock objects are useful when:
* The real object has nondeterministic behavior
* The real object is difficult to set up
* The real object has behavior that is hard to trigger
* The real object is slow.
* The real object does not yet exist

They discuss the differences betweens stubs, fakes and mock objects, explaining for each category when and how you should use them.

Read the complete article on
Find the book home page on