Chapter 18. Testing Entity Framework

 

This chapter covers

  • Unit-testing basics
  • Dependency isolation and mock objects
  • Persistence testing with Entity Framework

Testing is a critical part of the software development lifecycle—development can’t be considered complete until you’ve verified that the code you wrote works as expected. Unfortunately, testing code is rarely a trivial task for two main reasons:

  • Test complexity strongly depends on the complexity of the application itself. It tends to grow in a nonlinear manner as you add functionality, objects, tiers, or dependencies with external systems.
  • Fixing bugs or adding new features can potentially lead to regressions, harming your software stability and reliability. In these cases, you can’t be sure you didn’t damage existing functionality until you’ve reexecuted all the tests on them.

But what does “testing” a feature or a portion of code involve? It can mean launching your application and using UI to verify whether an order is correctly placed and that the stock of a given product is correctly adjusted, or it can mean building small console applications that invoke a method, so you can debug it and step through its code to check it behaves as expected. Or it can mean something smarter and automated, such as a durable and stable test suite that you can run on a daily basis (or after every build) to check that everything is working properly.

18.1. Unit tests at a glance

18.2. Writing a test suite in Visual Studio 2010

18.3. Isolating dependencies

18.4. Unit-testing the data access layer

18.5. Testing the persistence and retrieval of an entity

18.6. Summary