Improve your tests with Assert Object Pattern
Tags: dotnet, csharp, tests, testing
Published: 2022-07-29 16:50:18

---
title: Improve your tests with Assert Object Pattern
published: true
date: 2022-07-29 16:42:34 UTC
tags: dotnet,csharp,tests,testing
canonical_url: https://www.codejourney.net/2020/11/improve-your-tests-with-assert-object-pattern/
---

Today I’d like to share with you a very interesting concept in software testing – Assert Object pattern. It makes the _Assert_ part of a test much simpler and more readable. Let’s dive right into it !😉

## The Asserts Hell

Let’s consider the following unit test:

{% embed https://gist.github.com/dsibinski/f8fea58079d68dd8ac0d194a33964e1b %}

What’s wrong here? _Given_ and _When_ sections are great single-liners. We know straightaway what’s the input and the action executed. However, _Then_ block is too complex. It’s hard to figure out, just passing quickly through this test, what is _expected_. This case is even not that bad thanks to the usage of [FluentAssertions](https://fluentassertions.com/).

I spend a lot of time writing tests, but I spend even more time reading them. I always hope that I will find these _idyllic_ tests that act as code documentation… 🧐

However, let’s go step by step. Some time ago I attended a software testing course. That’s where I discovered the solution to _The Asserts Hell_.

## Assert Object for Better Asserts

Assert Object pattern is what solves our problem. The idea is to create an _Assert_ class which wraps the original object being tested. In our case, the tested object is of **Product** class, so our assert class will be called **ProductAssert**.

Let’s see the implementation:

{% embed https://gist.github.com/dsibinski/10a3c47a7e997eed29fabcb07914786b %}

As you can see, we simply moved the asserts from our test’s _Then_ section into **ProductAssert** class’s methods. Additionally, we always return *this* from each asserting method, which allows chaining functions calls.

We can now use such assert object in our unit test:

{% embed https://gist.github.com/dsibinski/e0a649e6134b9c6b389d3419db62f92a %}

You must agree, it’s much more readable now !😉

We can of course improve it even more, for example by wrapping these 3 assertion methods (**BeAvailable()**, **HaveDiscount()** and **Cost()**) into a single one. We can also easily create an [extension method](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) for the **Product** class itself, so the assert object doesn’t have to be instantiated in our unit test. I could even add an extension to _FluentAssertions_ library.

You can find complete source code used as examples in this article [here](https://github.com/dsibinski/Playground/tree/main/Playground/Playground.Tests).

## Assert Object – summary

I personally find Assert Object a very useful tests code refactoring method. As a programmer, you spend most of your time not on writing, but on reading the source code. Clear and simple tests can make your (and your colleagues’) life much better !😉

Of course, the Assert Object pattern should not be your default way of asserting. If you can complete your test with one or two assertions, then it’s probably better to keep it without wrapping into any additional objects. Having too many _assert_ statements might also mean that the objects being tested are poorly designed. However, as we often work with legacy code which we can’t easily change or refactor, it might be a good solution.

What patterns for writing better tests do you use? Share your tips in the comments!

The post [Improve your tests with Assert Object Pattern](https://www.codejourney.net/2020/11/improve-your-tests-with-assert-object-pattern/) appeared first on [CodeJourney.net](https://www.codejourney.net).