Chapter 5. Isolation (mock object) frameworks

 

This chapter covers

  • Understanding isolation frameworks
  • Defining fake objects
  • Using Rhino Mocks to create stubs and mocks
  • Surveying advanced use cases for mocks and stubs
  • Exploring arrange-act-assert and record-and-replay syntax
  • Avoiding common misuses of isolation frameworks

In the previous chapter, we looked at writing mocks and stubs manually and saw the challenges involved. In this chapter, we’ll look at some elegant solutions for these problems in the form of a mock object framework—a reusable library that can create and configure stub and mock objects at runtime. These objects are usually referred to as dynamic stubs and dynamic mocks.

We’ll start this chapter off with an overview of mock object frameworks (or isolation frameworks—the word mock is too overloaded already) and what they can do for us. We’ll then take a closer look at one specific framework: Rhino Mocks. We’ll see how we can use it to test various things and to create stubs, mocks, and other interesting things.

Later in this chapter, we’ll contrast Rhino Mocks with other frameworks currently available to .NET developers, and we’ll finish with a list of things to watch out for when using such frameworks in your tests.

Let’s start at the beginning: What are isolation frameworks?

5.1. Why use isolation frameworks?

We’ll start with a basic definition.


Definition

5.2. Dynamically creating a fake object

5.3. Strict versus nonstrict mock objects

5.4. Returning values from fake objects

5.5. Creating smart stubs with an isolation framework

5.6. Parameter constraints for mocks and stubs

5.7. Testing for event-related activities

5.8. Arrange-act-assert syntax for isolation

5.9. Current isolation frameworks for .NET

5.10. Advantages of isolation frameworks

5.11. Traps to avoid when using isolation frameworks

5.12. Summary