Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# This is a list of matchers that we use in our RSpec tests at Causes.
# *** CAPYBARA MATCHERS ***
#
# To check for content on a page, don't use `include`, use `have_content`:
response.body.should have_content 'A header'
# As opposed to `include`, this Capybara matcher will only look for content
# (ignoring tags and meta-info etc). And the error message is slightly easier to
# read.
# To check for a button:
response.body.should have_button 'Take action'
# To check for a link:
response.body.should have_link(campaign.title, href: campaign_path(campaign))
# To check for a checked checkbox field:
response.body.should have_checked_field '#id-of-the-field'
response.body.should have_checked_field 'The label'
# To check for elements with a css matcher:
response.body.should have_css 'input[type="text"]'
# *** OUR OWN CUSTOM MATCHERS ***
#
# To check for a meta tag:
response.body.should have_meta_tag(name: 'robots', content: 'noindex')
# To check that a link has certain parameters:
link.should have_params(page: 1, sort_by: 'name')
# To make sure that a block does not make database queries:
expect { subject }.to_not make_database_queries
# To check for exactly 2 queries:
expect { subject }.to make_database_queries(count: 2)
# To verify that an email was sent:
expect { subject }.to send_base_email
# To verify that an email was not sent:
expect { subject }.to_not send_base_email
# To verify that a particular email was sent:
expect { subject }.to send_base_email(mailer_class: UserMailer,
mailer_action: :welcome_user,
recipient: user)
# *** RAILS MATCHERS ***
#
# To check that a certain template was rendered:
expect { subject }.to render_template 'shared/campaign_card'
# To make sure that a redirect happens:
subject.should redirect_to campaign_path(campaign)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment