---
title: "Discussion on \"Unit Tests Done Right\" | Hashnode"
description: "Discussion on \"Unit Tests Done Right\". Write clean and rock-solid unit tests in .NET"
url: "https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Unit Tests Done Right](https://zagosk.in/unit-tests-done-right?utm_source=hashnode&utm_medium=feed)

Write clean and rock-solid unit tests in .NET

May 1, 2022 · 7 min read · zagosk.in  
Updated Apr 30, 2023  
Read the article

-   [#net](https://hashnode.com/tag/net)
-   [#unit-testing](https://hashnode.com/tag/unit-testing)
-   [#testing](https://hashnode.com/tag/testing)
-   [#csharp](https://hashnode.com/tag/csharp)

## Written by

[Aleksei Zagoskin](https://hashnode.com/@azagoskin)

@azagoskin · The Netherlands 🇳🇱

  
[View profile](https://hashnode.com/@azagoskin)

Software Architect, Staff Engineer

Software Architect, Staff Engineer | .NET, C#, microservices | Design and create scalable, high-load, fault tolerant and distributed backend services and Web APIs 👨🏼‍💻

## Responses 5

Markdown supported · ⌘↵ to post

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jun 28, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2ba9c2f205e7cdb18521)

What tool you are using to check test coverage? You've shared a screenshot with us but not its name :)

[Aleksei Zagoskin](https://hashnode.com/@azagoskin)

[Jul 8, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2ba9c2f205e7cdb18521#reply-62c86c6155f37390f97ca3d1)

Ah, it's Rider, it offers this stuff out of the box. I believe Resharper has it as well.

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jun 28, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2b657131dfe7a8b59651)

When you check an exception message do you think it might be worth using a constant template for the error message to don't break a test just because you have changed the message, or better to have the test broken on each change of the message and amend it accordingly?

[Aleksei Zagoskin](https://hashnode.com/@azagoskin)

[Jul 8, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2b657131dfe7a8b59651#reply-62c86c3655f37390f97ca3cd)

Good call. Sometimes I don't check the exception message at all, just exception type and properties (for custom exceptions).

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jun 28, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2af67131dfe7a8b5964b)

Test Strategy for Example 2 maybe should contain checking that we don't change any other user's entity field but only password. But this could mess up the test. What do you think? What is your assessing path to decide whether include or not anything to test?

[Aleksei Zagoskin](https://hashnode.com/@azagoskin)

[Jul 8, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2af67131dfe7a8b5964b#reply-62c86bfce62bf98e0fb06148)

We may consider these things for testing:

1.  Happy path e.g. return value is correct / expected exception gets thrown / messages get published etc.
2.  All if-else-switch branches.
3.  Edge cases such as awkward input parameters (int.MaxValue/MinValue/0, long strings, nulls, empty strings etc).
4.  The order of method calls (e.g. validation goes before we save data to the database).

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jun 28, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2a697131dfe7a8b5963b)

I know it is out of the scope of the topic but I'd like to know your opinion, what is the strategy of using cancellation tokens in your examples. I guess the `UserManager` is invoked by an action (as part of a controller) and then from `UserManager` you call a repo and pass ct to it. Inside repo you pass ct to linq async operation. What is the benefit to do that? What is the source of the ct? Who and why might cancel the token in the presented chain of calls?

[Aleksei Zagoskin](https://hashnode.com/@azagoskin)

[Jul 8, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2a697131dfe7a8b5963b#reply-62c86ad555f37390f97ca391)

Good point! Your assumption is absolutely correct, cancellation tokens come from controllers and do the cancellation, for instance, if/when a connection between the client and the server is interrupted. In these cases we may want to cancel "get" queries while let others finish.

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jul 11, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2a697131dfe7a8b5963b#reply-62cc566e2ff140b8fe05b022)

[Alexey Zagoskin](https://hashnode.com/@azagoskin) yes, agree, it is good to cancel a token when/if a connection is interrupted, but I've never used this before. Could you please refer to a web page or example about that, I mean how to detect the interruption and react.

[Roman Izosimov](https://hashnode.com/@RomanIzosimov)

[Jun 28, 2022](https://hashnode.com/posts/unit-tests-done-right/626ef1d7a2d355d4df441063/comment/62bb2858c2f205e7cdb1849b)

Hi Alexey, thank you a lot for the knowledge sharing and insides, it is very interesting, keep going!
