I'm here today to present the case against a particular piece of NUnit's fluent syntax. But before I do, let's set up a concrete example, something that gives the test meaning. Instead of just writing something down in boring old plain text, I've sloppily remixed a work of art I found via an image search and retitled it "Rebellion Against the (overuse of unrelated, Creative) Commons(-licensed images)!":
Solid. Requests for ~/Default.aspx should redirect to the Home controller. Let's get on with the show.
My Gripe: Assert.That syntax
Here is a comparison of the Assert.That syntax, the traditional Assert.AreEqual syntax, and the syntax provided by MSpec's NUnit extensions (MSpec isn't the only framework with these extensions, it's just the one of which I'm familiar):
I don't like the Assert.That syntax, in this scenario.
Look at all that. The new syntax is just...ugh.
I've read elsewhere that it's good because it reads like a sentence. Well, to shock you into elevated awareness, may I jog your memory of something else that reads a lot like a sentence? In case you didn't dare hover over that link, allow me to properly title it:
Check out the action in the linkage section!
Rock on, COBOL.NET!
Okay, conflating "reads like a sentence" to COBOL is a sucker punch, it's unfair. Let's do this by the numbers.
Comparing the three ways to do the same thing
In all cases, the fewer, the better.
|Syntax||Total chars||Chars used by test syntax (by my measure)||Total times Intellisense was necessary*||Of these, times Intellisense couldn't help (e.g. Assert, Is)|
* "Intellisense is necessary" is roughly defined as "any point at which you can use Intellisense." Definition is left purposefully imprecise.
I could go on for a bit about this, but I'll let the fancy HTML table do most of the talking. If there are any takeaways from this oversized-for-its-topic post, let them be:
- The fewer magic syntax words I have to learn (e.g. Assert, Is), the easier a framework is to learn. By this measure the MSpec extensions are the best, and the Assert.That syntax the worst.
- The fewer characters typed, the easier a framework is to use. Terseness is better when it doesn't impact learnability.
- "Reads like a sentence" is presumably the means to achieve some other goal, not a goal in and of itself. If your fluent syntax doesn't help achieve...whatever that other goal is, reconsider trying to make your fluent interface read like a sentence.
Related counter-point I don't care about today:
- The fewer extension methods attached to "object," the better.
NOTE: test frameworks and mocking frameworks get a pass from me because they are supposed to work against any object.
Final note: Better vs Best
I'm complaining today specifically about NUnit's Assert.That(actual, Is.EqualTo(expected)) syntax. I'm not down on the Assert.That() syntax as a whole, just the most commonly used method. And maybe that's what's bugging me--Assert.That has a lot of great stuff in there, allowing fuzzier comparisons beyond simply .AreEqual(), but the most commonly-used scenario is measurably worse than the old syntax.
And the Assert.That(actual, Is.EqualTo(expected)) syntax isn't the worst thing ever. It's not the end of the world. But shouldn't it be better than the thing it's replacing, not worse?