Wincent Colaiuta
2007-May-19 18:00 UTC
[rspec-users] Reflections on and questions about the describe/it syntax
I''m trying to wrap my head around the new describe/it syntax.
First, a few of observations:
- the "describe" word itself encourages you to think about contexts
as things/nouns; this is reinforced by the way "script/generate
rspec_model" and friends by default generate a template spec of the
form "describe ClassName do ... end", because classes so often
represent "things" (even if they are abstract things like
relationships)
- the ability to follow a bare class name with a qualifying string --
example: ''describe Stack, "(with one item)" do'' --
further extends
this pattern to encourage you to think of your contexts not only as
"things", but as "things" with a certain state
- finally, the "it" syntax used for the examples, almost always
followed by the word "should", encourages you to think about specific
behaviours within the context, which I guess is what the goals of
RSpec are all about
In many cases this works great and I have little trouble mapping the
desired behaviours to the equivalent RSpec lingo. RSpec "steers" me
towards writing specs along these lines:
- User (without a login name)
- should be invalid
- User (with a non-unique login name)
- should be invalid
- User (with login name less than three characters long)
- should be invalid
But I start to feel uncomfortable with the repetition after a while,
and I am not sure I am doing the right thing. What I really want to
say is:
- User
- should have a valid login name
- login name should be present
- login name should be unique
- login name should be three or more characters long
- etc
But if I start writing my specs like that I am getting further away
from the one-spec-per-example ideal that some people argue for. Is
this a bad thing? Which is clearer? "Clarity is king", after all. I
also wonder about the difference between:
- User (with a login name containing a space)
- should be invalid
And:
- User
- should be invalid if login name contains a space
Should I be trying to keep my "it" examples totally free of
conditional language? (eg. "should be something if something"). I
personally think the conditional form reads more clearly.
Basically, I am trying to go with the flow and work in the way that
RSpec expects me to work. I figure if I do that then things will work
out more easily for me. Words of wisdom, anyone?
Best wishes,
Wincent
