module Minitest::Rails::Expectations::ActionDispatch

This exists as a module to allow easy mixing into classes other than ActionDispatch::IntegrationTest where you might want to do job testing.

Public Instance Methods

must_dom_equal(expected, message = nil) click to toggle source

Checks that two HTML strings are equivalent. That they contain the same elements and attributes with the associated values. Checks the numeric difference between the return value of an expression as a result of what is evaluated.

apple_link = '<a href="">Apples</a>'
value(link_to("Apples", "")).must_dom_equal apple_link

See also ActionView::TestCase#assert_dom_equal See also ActionDispatch::IntegrationTest#assert_dom_equal

# File lib/minitest/rails/expectations/action_dispatch.rb, line 272
infect_an_assertion :assert_dom_equal, :must_dom_equal
must_route_for(path, defaults={}, extras={}, message=nil) click to toggle source

Expects that path and options match both ways; in other words, it verifies that path generates options and then that options generates path. This essentially combines assert_recognizes and assert_generates into one step.

The extras hash allows you to specify options that would normally be provided as a query string to the action. The message parameter allows you to specify a custom error message to display upon failure.

# Expect a basic route: a controller with the default action (index)
value({ controller: 'home', action: 'index' }).must_route_for '/home'

# Test a route generated with a specific controller, action, and parameter (id)
value({ controller: 'entries', action: 'show', id: 23 }).must_route_for '/entries/show/23'

# Expect a basic route (controller + default action), with an error message if it fails
value({ controller: 'store', action: 'index' }).must_route_for '/store'

# Tests a route, providing a defaults hash
value({id: "9", item: "square"}).must_route_for 'controller/action/9', {controller: "controller", action: "action"}, {}, {item: "square"}

# Tests a route with a HTTP method
value({ controller: "product", action: "update", id: "321" }).must_route_for({ method: 'put', path: '/product/321' })

See also ActionView::TestCase#assert_routing See also ActionDispatch::IntegrationTest#assert_routing

# File lib/minitest/rails/expectations/action_dispatch.rb, line 103
infect_an_assertion :assert_routing, :must_route_for
must_route_from(expected_path, defaults={}, extras = {}, message=nil) click to toggle source

Expects that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.

The defaults parameter is unused.

# Expects that the default action is generated for a route with no action
value({controller: "items", action: "index"}).must_route_from "/items"

# Tests that the list action is properly routed
value({controller: "items", action: "list"}).must_route_to "/items/list"

# Tests the generation of a route with a parameter
value({ controller: "items", action: "list", id: "1" }).must_route_from "/items/list/1"

# Expects that the generated route gives us our custom route
value({ controller: 'scm', action: 'show_diff', revision: "12" }).must_route_from "changesets/12"

See also ActionView::TestCase#assert_generates See also ActionDispatch::IntegrationTest#assert_generates

# File lib/minitest/rails/expectations/action_dispatch.rb, line 34
infect_an_assertion :assert_generates, :must_route_to
must_select(*args, &block) click to toggle source

An expectation that selects elements and makes one or more equality tests.

If the first argument is an element, selects all matching elements starting from (and including) that element and all its children in depth-first order.

If no element if specified, calling must_select selects from the response HTML unless must_select is called from within an must_select block.

When called with a block must_select passes an array of selected elements to the block. Calling must_select from the block, with no element specified, runs the expectation on the complete set of elements selected by the enclosing expectation. Alternatively the array may be iterated through so that must_select can be called separately for each element.


If the response contains two ordered lists, each with four list elements then:

must_select "ol" do |elements|
  elements.each do |element|
    must_select element, "li", 4

will pass, as will:

must_select "ol" do
  must_select "li", 8

The selector may be a CSS selector expression (String), an expression with substitution values, or an HTML::Selector object.

Equality Tests

The equality test may be one of the following:

  • true - Assertion is true if at least one element selected.

  • false - Assertion is true if no element selected.

  • String/Regexp - Assertion is true if the text value of at least one element matches the string or regular expression.

  • Integer - Assertion is true if exactly that number of elements are selected.

  • Range - Assertion is true if the number of selected elements fit the range.

If no equality test specified, the expectation is true if at least one element selected.

To perform more than one equality tests, use a hash with the following keys:

  • :text - Narrow the selection to elements that have this text value (string or regexp).

  • :html - Narrow the selection to elements that have this HTML content (string or regexp).

  • :count - Assertion is true if the number of selected elements is equal to this value.

  • :minimum - Assertion is true if the number of selected elements is at least this value.

  • :maximum - Assertion is true if the number of selected elements is at most this value.

If the method is called with a block, once all equality tests are evaluated the block is called with an array of all matched elements.

# At least one form element
must_select "form"

# Form element includes four input fields
must_select "form input", 4

# Page title is "Welcome"
must_select "title", "Welcome"

# Page title is "Welcome" and there is only one title element
must_select "title", {count: 1, text: "Welcome"},
    "Wrong title or more than one title element"

# Page contains no forms
must_select "form", false, "This page must contain no forms"

# Test the content and style
must_select "body div.header"

# Use substitution values
must_select "ol>li#?", /item-\d+/

# All input fields in the form have a name
must_select "form input" do
  must_select "[name=?]", /.+/  # Not empty

See also ActionView::TestCase#assert_select See also ActionDispatch::IntegrationTest#assert_select

# File lib/minitest/rails/expectations/action_dispatch.rb, line 199
must_select_email(&block) click to toggle source

Extracts the body of an email and runs nested expectations on it.

You must enable deliveries for this expectation to work, use:

 ActionMailer::Base.perform_deliveries = true

must_select_email do
  must_select "h1", "Email alert"

must_select_email do
  items = must_select "ol>li"
  items.each do
     # Work with items here...

See also ActionView::TestCase#assert_select_email See also ActionDispatch::IntegrationTest#assert_select_email

# File lib/minitest/rails/expectations/action_dispatch.rb, line 221
must_select_encoded(element = nil, &block) click to toggle source

Extracts the content of an element, treats it as encoded HTML and runs nested expectation on it.

You typically call this method within another expectation to operate on all currently selected elements. You can also pass an element or array of elements.

The content of each element is un-encoded, and wrapped in the root element encoded. It then calls the block with all un-encoded elements.

# Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
must_select "feed[xmlns='']" do
  # Select each entry item and then the title item
  must_select "entry>title" do
    # Run expectations on the encoded title elements
    must_select_encoded do
      must_select "b"

# Selects all paragraph tags from within the description of an RSS feed
must_select "rss[version=2.0]" do
  # Select description element of each feed item.
  must_select "channel>item>description" do
    # Run expectations on the encoded elements.
    must_select_encoded do
      must_select "p"

See also ActionView::TestCase#assert_select_encoded See also ActionDispatch::IntegrationTest#assert_select_encoded

# File lib/minitest/rails/expectations/action_dispatch.rb, line 260
wont_dom_equal(expected, message = nil) click to toggle source

Checks that the numeric result of evaluating an expression is not changed before and after invoking.

orange_link = '<a href="">Oranges</a>'
link_to("Apples", "").wont_dom_equal orange_link

See also ActionView::TestCase#refute_dom_equal See also ActionDispatch::IntegrationTest#refute_dom_equal See also ActionView::TestCase#assert_dom_not_equal See also ActionDispatch::IntegrationTest#assert_dom_not_equal

# File lib/minitest/rails/expectations/action_dispatch.rb, line 287
infect_an_assertion :refute_dom_equal, :wont_dom_equal