Skip to content

Instantly share code, notes, and snippets.

@trotzig
Last active June 12, 2021 01:32
Show Gist options
  • Save trotzig/10290090 to your computer and use it in GitHub Desktop.
Save trotzig/10290090 to your computer and use it in GitHub Desktop.

Revisions

  1. Henric Trotzig revised this gist Apr 9, 2014. 1 changed file with 64 additions and 1 deletion.
    65 changes: 64 additions & 1 deletion RSpec cheat sheet
    Original file line number Diff line number Diff line change
    @@ -1 +1,64 @@
    #
    # 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)
  2. Henric Trotzig created this gist Apr 9, 2014.
    1 change: 1 addition & 0 deletions RSpec cheat sheet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #