Should I Describe Methods in RSpec?
I’ve recently been learning RSpec after spending most of my career using the xUnit family of testing frameworks, including minitest.
Something that seems to be
common,
recommended
practice with RSpec is to describe
individual methods using describe '#instance_method'
or describe
'.class_method'
. I can see this being quite useful when writing
specs for an API, but for other kinds of code, I wonder if that
practice leads to some code smells.
Take these comments with a grain of salt, because I’m still learning RSpec and haven’t yet used it for anything other than code katas. These are just initial impressions that I’m curious about.
When I write specs for the individual methods of the class I’m testing, I find that I’m no longer thinking about the behavior of my object as a whole, but rather focusing on just that one method.
I can see that testing individual methods would help identify
Single Responsibility Principle
(SRP) violations at the method level. If there are too many specs
within the describe
block for a method, that’s a good sign that the
method is doing too much. However, I wonder if this practice hides
SRP violations at the class level. When the specs for the class are
broken up into method-level blocks, I think it becomes harder to see
that the class is doing too many things. I’m focusing on the trees
(methods) and not noticing that the forest (class) is getting out of
hand.
Some of these issues are addressed in the discussion of one of the recommendations I linked to above, but I’d be interesting in hearing other thoughts on this.
What say you? Am I just off-base here, or is this something I should watch for when I use RSpec? Is it a good idea to describe individual methods, or is it better to focus only on class-level responsibilities?