3 Types of Unit Tests (for C# Developers)
You can group your unit tests into 3 types. Knowing those 3 types will guide you to simple and maintainable tests.
Those types are based on the outcome the test wants to assert.
They are Result, State, and Component Interaction.
Let's see it in action.
A Result Test is when a test asserts a behavior based on the result value or exception.
This is the most common type of test.
public void Result()
A State Test is when to assert a behavior after calling a method, needs to assert state changes on an object.
It's usual to see this type of test when facing strong domain models.
public void State()
var order = new Order();
order.AddProduct(new Product("Shoes", 50), 2);
order.AddProduct(new Product("T-Shirt", 10), 1);
3️⃣ Component interaction
A Component Interaction Test is when the outcome of a behavior it's indirectly observed by observing the impact on external dependencies.
This type of test is common when testing Services or Command Handlers that need to call underlying data stores.
public async Task ComponentInteraction()
var fakeOrderRepository = new FakeOrderRepository();
var service = new OrderService(fakeOrderRepository);
var order = new Order(2);
await service.ProcessNewOrder(order, default);
var result = await fakeOrderRepository.GetAsync(order.Number);
But Component Interaction Tests can be dangerous. So, I highly recommend checking this article.
Let me know what you think about it. Follow me on Twitter (@gsferreira), and let's keep in touch.
Just keep things Simple 🌱