Testing with Mock Objects
Mock objects simulate real-world objects that are external to your application. During development and testing, mock objects let you isolate your application from objects in the outside world with which your application would normally collaborate. Eliminating reliance on this external code leaves only the code that you need to test.
Mock objects:
Offer better control when unit testing because you have complete control of the mock object.
Improve test performance dramatically by replacing slow external dependencies.
Simplify testing by letting you easily simulate states that are difficult or time-consuming to reproduce in the outside world.
Raise any type of exception on demand, allowing you to test these conditions in a single test cycle.
The downside of mock objects is the effort required to identify and program their behavior to accurately simulate the behavior of the real-world object. Fortunately, as the use and popularity of mock objects increases, libraries of reusable mock objects are becoming available that might meet your requirements.
To use mock objects for testing, you must design an interface for the real-world object and implement that interface for both test and production code. Using an interface to represent the object allows you to use the mock object in debug code and the real object in production code.
NUnit 2.2 adds a lightweight mock object facility that allows you to:
Dynamically create a mock implementation of any interface or marshal by ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access