Module: Minitest::Rails::Expectations

Included in:
Object
Defined in:
lib/minitest/rails/expectations.rb

Defined Under Namespace

Modules: ActiveJob

Instance Method Summary collapse

Instance Method Details

#assert_differenceObject

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


21
# File 'lib/minitest/rails/expectations.rb', line 21

infect_an_assertion :assert_difference, :must_change

#assert_dom_equalObject

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


394
# File 'lib/minitest/rails/expectations.rb', line 394

infect_an_assertion :assert_dom_equal, :must_dom_equal

#assert_generatesObject

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)


150
# File 'lib/minitest/rails/expectations.rb', line 150

infect_an_assertion :assert_generates, :must_route_to

#assert_recognizesObject

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)


190
# File 'lib/minitest/rails/expectations.rb', line 190

infect_an_assertion :assert_recognizes, :must_route_from

#assert_routingObject

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)


221
# File 'lib/minitest/rails/expectations.rb', line 221

infect_an_assertion :assert_routing, :must_route_for

#refute_differenceObject

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


32
# File 'lib/minitest/rails/expectations.rb', line 32

infect_an_assertion :refute_difference, :wont_change

#refute_dom_equalObject

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


411
# File 'lib/minitest/rails/expectations.rb', line 411

infect_an_assertion :refute_dom_equal, :wont_dom_equal