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