Rails Matchers Cheat Sheet
In addition to these spec types, rspec-rails provides some Rails-specific matchers. Some of these are available to any spec once you’ve required rails_helper; others are just for certain spec types.
Matcher | Passes if… | Available in… |
---|---|---|
be_a_new(model_class) | record.is_a(model_class) && record.new_record? | All spec types |
be_a_new(model_class).with(attribute: ’value’) | record.is_a(model_class) && record.new_record? && record.attribute == ’value’ | All spec types |
be_new_record | record.new_record? | All spec types |
be_valid | record.valid? | All spec types |
have_been_enqueued.with(some_args) | Job was enqueued with matching arguments | All spec types |
have_enqueued_job(job_class).with(some_args), enqueue_job(job_class).with(some_args) | Code in an expect block ... |
Get Effective Testing with RSpec 3 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.