Unit Testing Entity Framework with Typemock

Unit testing Entity Framework code that involves complex operations can be quite challenging. Luckily, Typemock, an advanced isolation framework, empowers you to effectively test your Entity Framework code while maintaining control over database interactions.

In this blog post, we will delve into unit testing Entity Framework code faking DbSet. We’ll focus on a scenario where we retrieve all CartItems from a database, perform a checkout operation, and calculate the total sum to pay.

Setting Up the Test Environment

Before we proceed, make sure you have integrated Typemock into your project. If you haven’t done so yet, install the Typemock NuGet package:

Install-Package Typemock.Isolator

With TypeMock at your disposal, let’s dive into testing your Entity Framework code.

Unit Testing Checkout and Sum Calculation

Consider a scenario where we have a Cart model containing items represented by a DbSet<CartItem> within a sample MyDatabase class.

Now, let’s test the logic into a method that retrieves all CartItems from the database, performs a checkout, and calculates the total sum to pay.

Let’s write a test to verify that the PerformCheckout method accurately calculates the total sum to pay.

In this test, we’re using TypeMock’s WillReturnCollection API to simulate the behavior of a DbSet<CartItem> containing two items. We’re then testing the PerformCheckout method of the CartProcessor class to ensure that it correctly calculates the total sum based on the prices of the cart items.

Advantages of Using TypeMock’s WillReturnCollection API

TypeMock’s WillReturnCollection API offers several benefits for testing Entity Framework code with complex operations:

  1. Behavior Simulation: The API allows you to simulate the behavior of a DbSet with a collection of items, which is essential for complex scenarios.
  2. Precise Verification: TypeMock provides accurate verification of specific methods called with the correct data, ensuring the reliability of your tests.
  3. Scenario Customization: You can easily customize fake object behavior to mimic various scenarios, making testing complex operations easier.
  4. Enhanced Test Execution Speed: Tests run faster without the overhead of real database connections, improving overall test suite efficiency.


Unit testing Entity Framework code involving complex operations, such as checkout and sum calculation, becomes streamlined and efficient with Typemock’s WillReturnCollection API. By simulating behavior and managing dependencies, Typemock empowers developers to write comprehensive tests that cover intricate scenarios without dealing with the intricacies of database interactions.

In this blog post, we’ve demonstrated how to use Typemock’s WillReturnCollection API with AsQueryable to effectively test a scenario involving checkout and sum calculation. As you explore more elaborate scenarios and use cases, you’ll find that TypeMock equips you with the tools needed to create comprehensive and reliable unit tests for your Entity Framework code.