RSpec matchers let you abstract away common assertions in your test code.
For example, we recently had a spec file with a bunch of lines that looked like this:
1
|
|
Which tests if the excel file we’re generating (using axlsx) includes Foo
in the header row.
That isn’t very neat. What if we replace it with this?
1
|
|
That looks a lot better. We can implement this kind of abstraction using custom RSpec matchers.
The matcher for this is as simple as:
1 2 3 4 5 |
|
RSpec passes in the expected and actual values to these blocks, and our code has to return a boolean representing the result of the assertion.
Now what about assertions that look like this?
1 2 3 |
|
The row that we’re checking changes for each assertion. Of course, we could create a different matcher for each of these cases, but there’s a better way.
1 2 3 |
|
RSpec lets you chain custom matchers.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
We first store the argument passed in to in_row
as an instance variable, and then access it in the main have_cell
matcher.
The example also includes a custom error message handler, which properly formats an error message if the assertion fails.