O'Reilly logo

Professional Test-Driven Development with C#: Developing Real World Applications with TDD by Jeff McWherter, James Bender

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5

Mocking External Resources

WHAT'S IN THIS CHAPTER?

  • Why statically binding components creates brittle systems
  • How the dependency injection pattern can help you by dynamically binding these components
  • How a dependency injection framework can help you manage the dependencies within your application
  • How to effectively mock your application's data access code with the repository pattern

Most applications you'll write as a business application developer are composed of various components (some you'll create; others will come from a third party) that are combined to perform a task. Sometimes these components represent external resources, such as a database, a web service, a file system, or even a physical device. These external resources are required by the components in your application, yet you still need to be able to test the components in isolation, away from the external resources. Developers practicing TDD do this by providing stand-ins for these external resources at runtime. These stand-ins are called mocks.

To use mocks, and to truly isolate the individual components under test, you need an alternative to statically binding the individual components. The dependency injection pattern can be used to inject the concrete implementations of these components at runtime or during testing. Dependency injection frameworks help you manage the web of dependencies and ensure that the correct concrete implementation is provided based on the context your component is being used in. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required