module Minitest::Rails::Expectations
Public Instance Methods
Checks the numeric difference between the return value of an expression as a result of what is evaluated.
lambda { User.create password: "valid" }.must_change "User.count" lambda { 3.times do User.create password: "valid" end }.must_change "User.count", 3
See also ActiveSupport::TestCase#assert_difference
# File lib/minitest/rails/expectations.rb, line 19 infect_an_assertion :assert_difference, :must_change
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="http://www.example.com">Apples</a>' link_to("Apples", "http://www.example.com").must_dom_equal apple_link
See also ActionController::TestCase#assert_dom_equal
See also ActionView::TestCase#assert_dom_equal See also ActionDispatch::IntegrationTest#assert_dom_equal
# File lib/minitest/rails/expectations.rb, line 392 infect_an_assertion :assert_dom_equal, :must_dom_equal
Expects that there is a tag/node/element in the body of the response that meets all of the given conditions. The conditions
parameter must be a hash of any of the following keys (all are optional):
-
:tag
: the node type must match the corresponding value -
:attributes
: a hash. The node's attributes must match the corresponding values in the hash. -
:parent
: a hash. The node's parent must match the corresponding hash. -
:child
: a hash. At least one of the node's immediate children must meet the criteria described by the hash. -
:ancestor
: a hash. At least one of the node's ancestors must meet the criteria described by the hash. -
:descendant
: a hash. At least one of the node's descendants must meet the criteria described by the hash. -
:sibling
: a hash. At least one of the node's siblings must meet the criteria described by the hash. -
:after
: a hash. The node must be after any sibling meeting the criteria described by the hash, and at least one sibling must match. -
:before
: a hash. The node must be before any sibling meeting the criteria described by the hash, and at least one sibling must match. -
:children
: a hash, for counting children of a node. Accepts the keys:-
:count
: either a number or a range which must equal (or include) the number of children that match. -
:less_than
: the number of matching children must be less than this number. -
:greater_than
: the number of matching children must be greater than this number. -
:only
: another hash consisting of the keys to use to match on the children, and only matching children will be counted.
-
-
:content
: the textual content of the node must match the given value. This will not match HTML tags in the body of a tag–only text.
Conditions are matched using the following algorithm:
-
if the condition is a string, it must be a substring of the value.
-
if the condition is a regexp, it must match the value.
-
if the condition is a number, the value must match number.to_s.
-
if the condition is
true
, the value must not benil
. -
if the condition is
false
ornil
, the value must benil
.# Expect that there is a “span” tag
must_have_tag
tag: “span”# Expect that there is a “span” tag with id=“x”
must_have_tag
tag: “span”, attributes: { id: “x” }# Expect that there is a “span” tag using the short-hand
must_have_tag
:span# Expect that there is a “span” tag with id=“x” using the short-hand
must_have_tag
:span, attributes: { id: “x” }# Expect that there is a “span” inside of a “div”
must_have_tag
tag: “span”, parent: { tag: “div” }# Expect that there is a “span” somewhere inside a table
must_have_tag
tag: “span”, ancestor: { tag: “table” }# Expect that there is a “span” with at least one “em” child
must_have_tag
tag: “span”, child: { tag: “em” }# Expect that there is a “span” containing a (possibly nested) # “strong” tag.
must_have_tag
tag: “span”, descendant: { tag: “strong” }# Expect that there is a “span” containing between 2 and 4 “em” tags # as immediate children
must_have_tag
tag: “span”,children: { count: 2..4, only: { tag: "em" } }
# Get funky: assert that there is a “div”, with an “ul” ancestor # and an “li” parent (with “class” = “enum”), and containing a # “span” descendant that contains text matching /hello world/
must_have_tag
tag: “div”,ancestor: { tag: "ul" }, parent: { tag: "li", attributes: { class: "enum" } }, descendant: { tag: "span", child: /hello world/ }
Please note: must_have_tag
and wont_have_tag
only work with well-formed XHTML. They recognize a few tags as implicitly self-closing (like br and hr and such) but will not work correctly with tags that allow optional closing tags (p, li, td). You must explicitly close all of your tags to use these assertions.
See also ActionController::TestCase#assert_tag
See also ActionView::TestCase#assert_tag See also ActionDispatch::IntegrationTest#assert_tag
# File lib/minitest/rails/expectations.rb, line 509
Expects that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that assert_redirected_to(controller: "weblog")
will also match the redirection of redirect_to(controller: "weblog", action: "show")
and so on.
# expect that the redirection was to the "index" action on the WeblogController must_redirect_to controller: "weblog", action: "index" # expect that the redirection was to the named route login_url must_redirect_to login_url # expect that the redirection was to the url for @customer must_redirect_to @customer # expect that the redirection matches the regular expression must_redirect_to %r(\Ahttp://example.org)
See also ActionController::TestCase#assert_redirected_to
See also ActionView::TestCase#assert_redirected_to See also ActionDispatch::IntegrationTest#assert_redirected_to
# File lib/minitest/rails/expectations.rb, line 86
Expects that the request was rendered with the appropriate template file or partials.
# expect that the "new" view template was rendered must_render_template "new" # expect that the exact template "admin/posts/new" was rendered must_render_template %r{\Aadmin/posts/new\Z} # expect that the layout 'admin' was rendered must_render_template layout: 'admin' must_render_template layout: 'layouts/admin' must_render_template layout: :admin # expect that no layout was rendered must_render_template layout: nil must_render_template layout: false # expect that the "_customer" partial was rendered twice must_render_template partial: '_customer', count: 2 # expect that no partials were rendered must_render_template partial: false
In a view test case, you can also expect that specific locals are passed to partials:
# expect that the "_customer" partial was rendered with a specific object must_render_template partial: '_customer', locals: { customer: @customer }
See also ActionController::TestCase#assert_template
See also ActionView::TestCase#assert_template See also ActionDispatch::IntegrationTest#assert_template
# File lib/minitest/rails/expectations.rb, line 123
Expects that the response is one of the following types:
-
:success
- Status code was in the 200-299 range -
:redirect
- Status code was in the 300-399 range -
:missing
- Status code was 404 -
:error
- Status code was in the 500-599 range
You can also pass an explicit status number like assert_response(501)
or its symbolic equivalent assert_response(:not_implemented)
. See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list.
# expect that the response was a redirection must_respond_with :redirect response.must_respond_with :redirect # expect that the response code was status code 401 (unauthorized) must_respond_with 401 response.must_respond_with 401
See also ActionController::TestCase#assert_response
See also ActionView::TestCase#assert_response See also ActionDispatch::IntegrationTest#assert_response
# File lib/minitest/rails/expectations.rb, line 62
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) { controller: 'home', action: 'index' }.must_route_for '/home' # Test a route generated with a specific controller, action, and parameter (id) { 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 { controller: 'store', action: 'index' }.must_route_for '/store' # Tests a route, providing a defaults hash {id: "9", item: "square"}.must_route_for 'controller/action/9', {controller: "controller", action: "action"}, {}, {item: "square"} # Tests a route with a HTTP method { controller: "product", action: "update", id: "321" }.must_route_for({ method: 'put', path: '/product/321' })
See also ActionController::TestCase#assert_routing
See also ActionView::TestCase#assert_routing See also ActionDispatch::IntegrationTest#assert_routing
# File lib/minitest/rails/expectations.rb, line 219 infect_an_assertion :assert_routing, :must_route_for
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 {controller: "items", action: "index"}.must_route_from "/items" # Tests that the list action is properly routed {controller: "items", action: "list"}.must_route_to "/items/list" # Tests the generation of a route with a parameter { controller: "items", action: "list", id: "1" }.must_route_from "/items/list/1" # Expects that the generated route gives us our custom route { controller: 'scm', action: 'show_diff', revision: "12" }.must_route_from "changesets/12"
See also ActionController::TestCase#assert_generates
See also ActionView::TestCase#assert_generates See also ActionDispatch::IntegrationTest#assert_generates
# File lib/minitest/rails/expectations.rb, line 148 infect_an_assertion :assert_generates, :must_route_to
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.
Example¶ ↑
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 end end
will pass, as will:
must_select "ol" do must_select "li", 8 end
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 ul.menu" # 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 end
See also ActionController::TestCase#assert_select
See also ActionView::TestCase#assert_select See also ActionDispatch::IntegrationTest#assert_select
# File lib/minitest/rails/expectations.rb, line 316
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" end must_select_email do items = must_select "ol>li" items.each do # Work with items here... end end
See also ActionController::TestCase#assert_select_email
See also ActionView::TestCase#assert_select_email See also ActionDispatch::IntegrationTest#assert_select_email
# File lib/minitest/rails/expectations.rb, line 339
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='http://www.w3.org/2005/Atom']" 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" end end end # 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" end end end
See also ActionController::TestCase#assert_select_encoded
See also ActionView::TestCase#assert_select_encoded See also ActionDispatch::IntegrationTest#assert_select_encoded
# File lib/minitest/rails/expectations.rb, line 379
Checks that the numeric result of evaluating an expression is not changed before and after invoking.
lambda { User.new }.wont_change "User.count"
See also ActiveSupport::TestCase#refute_difference
# File lib/minitest/rails/expectations.rb, line 30 infect_an_assertion :refute_difference, :wont_change
Checks that the numeric result of evaluating an expression is not changed before and after invoking.
orange_link = '<a href="http://www.example.com">Oranges</a>' link_to("Apples", "http://www.example.com").wont_dom_equal orange_link
See also ActionController::TestCase#refute_dom_equal
See also ActionView::TestCase#refute_dom_equal See also ActionDispatch::IntegrationTest#refute_dom_equal See also ActionController::TestCase#assert_dom_not_equal
See also ActionView::TestCase#assert_dom_not_equal See also ActionDispatch::IntegrationTest#assert_dom_not_equal
# File lib/minitest/rails/expectations.rb, line 409 infect_an_assertion :refute_dom_equal, :wont_dom_equal
Identical to must_have_tag
, but asserts that a matching tag does not exist. (See must_have_tag
for a full discussion of the syntax.)
# Expect that there is not a "div" containing a "p" wont_have_tag tag: "div", descendant: { tag: "p" } # Expect that an unordered list is empty wont_have_tag tag: "ul", descendant: { tag: "li" } # Expect that there is not a "p" tag with between 1 to 3 "img" tags # as immediate children wont_have_tag tag: "p", children: { count: 1..3, only: { tag: "img" } }
See also ActionController::TestCase#refute_tag
See also ActionView::TestCase#refute_tag See also ActionDispatch::IntegrationTest#refute_tag See also ActionController::TestCase#assert_no_tag
See also ActionView::TestCase#assert_no_tag See also ActionDispatch::IntegrationTest#assert_no_tag
# File lib/minitest/rails/expectations.rb, line 533