[LRUG] A question on DRYness on testing methods that get / set state
James Adam
james at lazyatom.com
Wed Nov 23 00:40:39 PST 2011
On 23 Nov 2011, at 08:32, Graham Ashton wrote:
> On 23 Nov 2011, at 08:10, Matthew Rudy Jacobs <matthewrudyjacobs at gmail.com> wrote:
>
>> You can structure the naming of your tests in an infinite number of ways but these are the behaviours I'd care about.
>> If I don't PUT anything into the box, I GET nothing out.
>> If I PUT something in the box I GET out the same thing
>> If I PUT something in the box and try to GET it multiple times, I always GET the same thing
>> If I PUT multiple things into the box, I only ever GET the most recent.
> Do you mean you'd test all those behaviours, irrespective of how you implement it?
>
> I only ask as I just test things that I think might not work, in the context of how I've implemented it. i.e. My decisions are informed by my knowledge of the implementation.
>
> So if we were using attr_* to implement the feature (I realise it's a contrived example) I'd skip the test entirely as I'd just be testing Ruby (which is pointless). If I wrote my own getter and setter and implemented them the same way as attr_* does it, I'd only need to test behaviour 2. Checking 1, 3 and 4 would introduce cruft.
The problem with testing this way is that it doesn't help you refactor. If you were using attr_* ans so didn't right any tests, and then some enterprising soul removes your attr_* line, your test suite (or certainly at least your unit test) will still pass, giving a false positive result.
Asserting against behaviour means you're free to change the underlying implementation with reassurance that the "API" your object exposes never breaks. Comprehensive tests like those above are the price you pay for the freedom to refactor with impunity.
Using attr_* as the basis for this kind of discussion is, I suspect, a bit distracting since the behaviour is probably trivial compared to other aspects of most code, but the principles still technically apply I think.
- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20111123/8a284a76/attachment-0003.html>
More information about the Chat
mailing list