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.
1 2 3 4 5 |
public class MyDatabase { public virtual DbSet<CartItem> CartItems { get; set; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class CartProcessor { public decimal PerformCheckout(MyDatabase database) { var cartItems = database.CartItems.ToList(); // Perform checkout logic here decimal totalSum = CalculateTotalSum(cartItems); return totalSum; } private decimal CalculateTotalSum(List<CartItem> cartItems) { // Calculate and return the total sum logic here // This could involve summing up item prices, taxes, etc. return cartItems.Sum(item => item.Price); } } |
Let’s write a test to verify that the PerformCheckout
method accurately calculates the total sum to pay.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[TestClass, Isolated] public class CartProcessorTests { [TestMethod] public void PerformCheckout_WithFakeDbSet_ReturnsSum() { // Arrange var database = Isolate.Fake.Instance<MyDatabase>(); var cartItemsData = new List<CartItem> { new CartItem { Id = 1, Price = 10 }, new CartItem { Id = 2, Price = 20 } }; // with this simple line we fake the real DbSet // no need to inject a different type // Note: the use of AsQueryable Isolate.WhenCalled(() => database.CartItems).WillReturnCollection(cartItemsData.AsQuertable()); var cartProcessor = new CartProcessor(); // Act var totalSum = cartProcessor.PerformCheckout(database); // Assert Assert.AreEqual(30, totalSum); } } |
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:
- Behavior Simulation: The API allows you to simulate the behavior of a
DbSet
with a collection of items, which is essential for complex scenarios. - Precise Verification: TypeMock provides accurate verification of specific methods called with the correct data, ensuring the reliability of your tests.
- Scenario Customization: You can easily customize fake object behavior to mimic various scenarios, making testing complex operations easier.
- Enhanced Test Execution Speed: Tests run faster without the overhead of real database connections, improving overall test suite efficiency.
Conclusion
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.