Write Your Own RSpec Matchers
RSpecâs built-in matchers are flexible, but sometimes you have behavior patterns you specify multiple times in your code and the existing matchers donât quite cover it. Sometimes this is because the specification requires multiple steps, and sometimes itâs because the generic matcher doesnât quite match the codeâs intent.
RSpec provides tools for creating your own custom matchers to cover just such an eventuality. A basic matcher is really simple. Letâs say we wanted a custom matcher to measure project size in points. Rather than say expect(project.size).to eq(5) weâd say expect(project).to be_of_size(5). Itâs a little contrived, but work with me.
Normally custom matchers are placed in the spec/support ...
Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.