While working on the Stride integration for GitLab, I found myself with the need of matching some complex and repetitive JSONs like the one from below.
Writing a matcher for this JSON got pretty hairy and not very readable, but RSpec has composable matchers that can be combined to express the exact details of the expectations. Let’s see how a spec will look with these matchers.
And all we need now is to define those matchers. Only one small problem with this strategy: there isn’t any obivious documentation on how to do it. After browsing the code, we find our way in some comments from the matchers file.
Include {RSpec::Matchers::Composable} in your custom matcher to make it support
being composed (matchers defined using the DSL have this included automatically).
So it’s just a regular matcher that includes RSpec::Matchers::Composable. And here they are.