When we write clean Ruby code, we try to pull out methods with descriptive names that do small amounts of work. It’s possible to do the same in RSpec, just as we would in a less “fluent” test framework like Ruby’s standard testing library Minitest.
RSpec’s describe
and context
methods define anonymous classes that behave like any other Ruby class as far as scope. Nested blocks even inherit methods from their containers and can use super
. Each it
block is more like a method, creating an instance of its outer describe
/ context
and executing in that scope.
With this, we can extract pieces of logic, share them between multiple specs, give them descriptive names, and call them from within it
blocks. This leads to descriptive tests that don’t suffer from the Mystery Guest problem: when reading tests, we can’t understand “the connection between fixture and verification logic because it is done outside of the test method.”
Top comments (0)