Correction: dSpec does have the optional Message parameter
In my last post, I incorrectly said that dSpec doesn’t have the Message parameter. Jody corrected me. Here’s the syntax:
Specify.That(FRate, 'Tax rate').Should.Equal(0.10);
I kinda like this syntax — it puts the label right next to the thing it’s labeling, rather than putting the label last like DUnitLite does now. What do you guys think? Which of the following syntaxes would be best for DUnitLite?
Specify.That(FRate, Should.Equal(0.10), 'Tax rate'); // (a)
Specify.That(FRate, 'Tax rate', Should.Equal(0.10)); // (b)
Specify.That('Tax rate', FRate, Should.Equal(0.10)); // (c)
Currently DUnitLite uses (a). (b) is more like dSpec.
The advantage to (c) would be when your expressions are long enough to line-wrap. If the label is likely to be shorter than the expression being tested (and, therefore, more likely to fit on the first line without wrapping), then this kind of formatting might work really well:
Specify.That('State tax rate',
TInvoiceCalculator.CreateForState(FCustomer.State).TaxRate,
Should.Equal(0.10));
But without wrapping, having a label as the first parameter might get in the way of readability. I’m not sure.
Which syntax seems most readable to you?
May 21st, 2007 at 3:57 pm
The reason that dSpec can do it that way, though, is that the conditions are specified with later method calls. If you’re going to put the message in the middle of an argument list, then it will become impossible for that message to be optional.
Considering that well-written tests frequently don’t need assertion messages, it would be a real hassle to always have to provide them.
May 22nd, 2007 at 1:14 am
what do you think about this syntax
Specify(’Tax rate’).That(FRate, Should,Equal(0.10));
Ciao Heinz Z.
May 23rd, 2007 at 3:27 pm
Sam: Of course I wouldn’t make Message a required parameter — most of the time you don’t need it. I would overload the method so you could either pass it or not. And that’s easy whichever of the three syntaxes I use.
May 23rd, 2007 at 3:30 pm
Heinz: I don’t really care for that syntax, for two reasons: it’s less readable (it’s not worded the way you would actually write a sentence); and it would require Specify to be a unit procedure, rather than a class (and I prefer to avoid unit procedures if I possibly can).