Module: MiniTest::Rails::Expectations
- Included in:
- Object
- Defined in:
- lib/minitest/rails/expectations.rb
Instance Method Summary collapse
-
#assert_blank ⇒ Object
Checks if an expression is blank.
-
#assert_difference ⇒ Object
Checks the numeric difference between the return value of an expression as a result of what is evaluated.
-
#assert_dom_equal ⇒ Object
Checks that two HTML strings are equivalent.
-
#assert_generates ⇒ Object
Expects that the provided options can be used to generate the provided path.
-
#assert_present ⇒ Object
Checks if an expression is present.
-
#assert_recognizes ⇒ Object
Expects that the routing of the given
pathwas handled correctly and that the parsed options (given in theexpected_optionshash) matchpath. -
#assert_routing ⇒ Object
Expects that path and options match both ways; in other words, it verifies that
pathgeneratesoptionsand then thatoptionsgeneratespath. -
#refute_blank ⇒ Object
Checks if an expression is not blank.
-
#refute_difference ⇒ Object
Checks that the numeric result of evaluating an expression is not changed before and after invoking.
-
#refute_dom_equal ⇒ Object
Checks that the numeric result of evaluating an expression is not changed before and after invoking.
-
#refute_present ⇒ Object
Checks if an expression is not present.
Instance Method Details
#assert_blank ⇒ Object
Checks if an expression is blank. Passes if actual.blank? is true.
This expectation is deprecated.
Use the following to check for blank? instead:
actual.must_be :blank?
The deprecated expectation can be called like this:
"".must_be_blank
nil.must_be_blank
[].must_be_blank
{}.must_be_blank
See also ActiveSupport::TestCase#assert_blank
:method: must_be_blank :args: message = nil
27 |
# File 'lib/minitest/rails/expectations.rb', line 27 infect_an_assertion :assert_blank, :must_be_blank, :unary |
#assert_difference ⇒ Object
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
:method: must_change :args: expression, difference = 1, message = nil
107 |
# File 'lib/minitest/rails/expectations.rb', line 107 infect_an_assertion :assert_difference, :must_change |
#assert_dom_equal ⇒ Object
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
:method: must_dom_equal :args: expected, message = nil
480 |
# File 'lib/minitest/rails/expectations.rb', line 480 infect_an_assertion :assert_dom_equal, :must_dom_equal |
#assert_generates ⇒ Object
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
:method: must_route_from :call-seq: options.must_route_from(expected_path, defaults={}, extras = {}, message=nil)
236 |
# File 'lib/minitest/rails/expectations.rb', line 236 infect_an_assertion :assert_generates, :must_route_to |
#assert_present ⇒ Object
Checks if an expression is present. Passes if actual.present? is true.
This expectation is deprecated.
Use the following to check for present? instead:
actual.must_be :present?
The deprecated expectation can be called like this:
"here".must_be_present
Object.new.must_be_present
[1,2,3].must_be_present
{:key => :value}.must_be_present
See also ActiveSupport::TestCase#assert_present
:method: must_be_present :args: message = nil
71 |
# File 'lib/minitest/rails/expectations.rb', line 71 infect_an_assertion :assert_present, :must_be_present, :unary |
#assert_recognizes ⇒ Object
Expects that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
Pass a hash in the second argument (path) to specify the request method. This is useful for routes requiring a specific HTTP method. The hash should contain a :path with the incoming request path and a :method containing the required HTTP verb.
# assert that POSTing to /items will call the create action on ItemsController
assert_recognizes({controller: 'items', action: 'create'}, {path: 'items', method: :post})
You can also pass in extras with a hash containing URL parameters that would normally be in the query string. This can be used to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the extras argument, appending the query string on the path directly will not work. For example:
# Expect that a path of '/items/list/1?view=print' returns the correct options
'items/list/1'.must_route_from({controller: 'items', action: 'list', id: '1', view: 'print'}, { view: "print" })
The message parameter allows you to pass in an error message that is displayed upon failure.
# Check the default route (i.e., the index action)
'items'.must_route_from({controller: 'items', action: 'index'})
# Test a specific action
'items/list'.must_route_from({controller: 'items', action: 'list'})
# Test an action with a parameter
'items/destroy/1'.must_route_from({controller: 'items', action: 'destroy', id: '1'})
# Test a custom route
'view/item1'.must_route_from({controller: 'items', action: 'show', id: '1'})
See also ActionController::TestCase#assert_recognizes See also ActionView::TestCase#assert_recognizes See also ActionDispatch::IntegrationTest#assert_recognizes
:method: must_route_from :call-seq: path.must_route_from(expected_options, extras={}, msg=nil)
276 |
# File 'lib/minitest/rails/expectations.rb', line 276 infect_an_assertion :assert_recognizes, :must_route_from |
#assert_routing ⇒ Object
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
:method: must_route_for :call-seq: options.must_route_for(path, defaults={}, extras={}, message=nil)
307 |
# File 'lib/minitest/rails/expectations.rb', line 307 infect_an_assertion :assert_routing, :must_route_for |
#refute_blank ⇒ Object
Checks if an expression is not blank. Passes if actual.blank? is false.
This expectation is deprecated.
Use the following to check for blank? instead:
actual.wont_be :blank?
The deprecated expectation can be called like this:
"here".wont_be_blank
Object.new.wont_be_blank
[1,2,3].wont_be_blank
{:key => :value}.wont_be_blank
See also ActiveSupport::TestCase#refute_blank
:method: wont_be_blank :args: message = nil
93 |
# File 'lib/minitest/rails/expectations.rb', line 93 infect_an_assertion :refute_blank, :wont_be_blank, :unary |
#refute_difference ⇒ Object
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
:method: wont_change :args: expression, message = nil
118 |
# File 'lib/minitest/rails/expectations.rb', line 118 infect_an_assertion :refute_difference, :wont_change |
#refute_dom_equal ⇒ Object
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
:method: wont_dom_equal :args: expected, message = nil
497 |
# File 'lib/minitest/rails/expectations.rb', line 497 infect_an_assertion :refute_dom_equal, :wont_dom_equal |
#refute_present ⇒ Object
Checks if an expression is not present. Passes if actual.present? is false.
This expectation is deprecated.
Use the following to check for present? instead:
actual.wont_be :present?
The deprecated expectation can be called like this:
"".wont_be_present
nil.wont_be_present
[].wont_be_present
{}.wont_be_present
See also ActiveSupport::TestCase#refute_present
:method: wont_be_present :args: message = nil
49 |
# File 'lib/minitest/rails/expectations.rb', line 49 infect_an_assertion :refute_present, :wont_be_present, :unary |