Chapter 5. Isolation (mocking) frameworks

 

This chapter covers

  • Understanding isolation frameworks
  • Using NSubstitute to create stubs and mocks
  • Exploring advanced use cases for mocks and stubs
  • 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 an isolation framework—a reusable library that can create and configure fake objects at runtime. These objects are referred to as dynamic stubs and dynamic mocks.

We’ll begin with an overview of isolation frameworks (or mocking frameworks—the word mock is too overloaded already) and what they can do. I call them isolation frameworks because they allow you to isolate the unit of work from its dependencies. We’ll take a closer look at one specific framework: NSubstitute. You’ll see how you can use it to test various things and to create stubs, mocks, and other interesting things.

But NSubstitute (NSub for short) isn’t the point here. While using NSub, you’ll see the specific values that its API promotes in your tests (readability, maintainability, robust long-lasting tests, and more) and find out what makes an isolation framework good and, alternatively, what can make it a drawback for your tests.

5.1. Why use isolation frameworks?

 
 

5.2. Dynamically creating a fake object

 
 

5.3. Simulating fake values

 
 

5.4. Testing for event-related activities

 
 
 

5.5. Current isolation frameworks for .NET

 
 
 

5.6. Advantages and traps of isolation frameworks

 

5.7. Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest