[rspec-users] How to spec code with multiple (interacting) paths
jerry.west at ntlworld.com
Tue Feb 20 08:05:06 EST 2007
I doubt there is an ideal solution. You might try to encapsulate the
different behaviours in different classes (each spec'd separately).
Where there are true alternatives, you could use SimpleDelegator to
delegate to the appropriate class depending on your flag. No less work
but conceptually cleaner perhaps.
Alternatively (better?) work to eliminate the flag. especially if it's a
do / don't do choice. If you don't need to do something, don't call the
code. Push the decision and hence the spec up a level. The calling
routine should not do anything if the flag is false. This is the
boundary case of delegation: there's no need to spec a class that
doesn't do anything.
Lastly, you might think of the spec from the point of view of the flag,
not the object. In other words, the customer wants something to happen
under these circumstances and these other things to happen under those
circumstances. Write your code to match these specs and refactor the
commonality into shared routines, using your specs to increase your
confidence that you haven't introduced (too many!) bugs. BTW, it's
almost always possible to change code somewhere and still pass the spec
- the trick is to write the spec before you change the code! BDD isn't
a guarantee of correctness, merely an aid to design and coding that
increases your confidence that your code will do (only) what is required.
> I can't find an ideal solution to this situation, but unfortunately
> it crops up quite often for me. Has anyone got any experiences or
> opinions that will help?
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users