Unit testing is best done with BDD in mind.
BDD considers "stories" described in Gherkin (a DSL). Stories have the template:
As a
(user)In order to
(achieve some end)I want to
(something to realize that end)
Then we create scenarios which would happen in this story.
BDD scenarios consist of three components:
- Given [context]
- When [event]
- Then [outcome] (Reference 4)
We can add more using And
.
The purpose of describe
is to wrap a set of tests against one functionality
while context
is to wrap a set of tests against one functionality under the same state.
Be clear about what method you are describing.
For instance, use the Ruby documentation convention of .
(or ::
)
when referring to a class method's name and #
when referring to an instance method's name.
# bad
describe "Launch the rocket!" do
...
end
# good
describe Rocket do
describe "#launch" do
# tests for the instance method
...
end
end
Recall, a class method in Ruby is analogous to a static method in Java, an instance method in Ruby is analogous to a non-static method in Java.
- Better Specs
- Dan North's What's in a Story?
Writing an adventure game in Ruby:
https://practicingruby.com/articles/adventure-in-prototypes