I’m converting my Niche CommandLine library across to use Fluent Assertions. This means updating well over a hundred unit tests to use the new style. Fortunately, Resharper makes this a non-tedious task.

The project currently uses NUnit assertions in this style:

Assert.That(dashedName, Is.EqualTo(string.Empty)); 

With Fluent Assertions, I want this to be rewritten as:


The change is very mechanical, and doing it by hand would be both tedious and error prone.

Instead of making this change the hard way, by hand, I decided to learn and use Resharper’s support for custom code inspection patterns - found in Visual Studio at Resharper | Options | Code Inspection | Custom Patterns. Here’s the first one I defined successfully:

The top part of this dialog specifies the pattern to find - in this case, a pattern that matches any strict equality assertions. The placeholders $value$ and $expected$ are defined on the right-hand side; both are defined as expressions. The bottom part defines the replacement pattern and reuses the placeholders.

When I first created this pattern, nothing happened, and I was convinced I was (still!) doing it wrong.

Turns out that there’s another step that’s important - you need to turn the patterns on by selecting their severity - I’ve set mine to error so that I don’t miss any of the fixes.

As you can see, I’ve also defined a few other patterns that represent common assertion styles.

// Patterns
Assert.That($expression$, Is.Null);
Assert.That($expression$, Is.True);
Assert.That($expression$, Is.False);

// Replacements

With these patterns, Resharper almost does the work for me, highlighting code that needs changing and providing the exact replacement required:

In their famous book The Pragmatic Programmer, Andy Hunt and David Thomas talk about the importance of learning to use your tooling well, of working smarter instead of harder.

Leveraging Resharper’s support for custom inspection patterns made it possible for me to convert the assertion style of this project in a fraction of the time with far fewer errors, even allowing for the time it took to learn.

What features are there in the tools you use every day that might make you more effective if you learned to use them?


blog comments powered by Disqus
Next Post
Implementing the Singleton Pattern  15 Jul 2017
Prior Post
Getting Started with FsCheck  01 Jul 2017
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
July 2017