Module: CustomAssertions
- Included in:
- CustomAssertionsTest, TableHelperTest
- Defined in:
- lib/generators/dry_crud/templates/test/support/custom_assertions.rb
Overview
A handful of convenient assertions. The aim of custom assertions is to provide more specific error messages and to perform complex checks.
Ideally, include this module into your test_helper.rb file:
# at the beginning of the file:
require 'support/custom_assertions'
# inside the class definition:
include CustomAssertions
Instance Method Summary collapse
-
#assert_count(expected, regexp, string, msg = '') ⇒ Object
Asserts that regexp occurs exactly expected times in string.
-
#assert_not_valid(record, *invalid_attrs) ⇒ Object
Asserts that the given active model record is not valid.
-
#assert_valid(record, msg = '') ⇒ Object
Asserts that the given active model record is valid.
-
#mu_pp(obj) ⇒ Object
The method used to by Test::Unit to format arguments.
Instance Method Details
#assert_count(expected, regexp, string, msg = '') ⇒ Object
Asserts that regexp occurs exactly expected times in string.
13 14 15 16 17 18 19 20 |
# File 'lib/generators/dry_crud/templates/test/support/custom_assertions.rb', line 13 def assert_count(expected, regexp, string, msg = '') actual = string.scan(regexp).size msg = (msg) do "Expected #{mu_pp(regexp)} to occur #{expected} time(s), " \ "but occured #{actual} time(s) in \n#{mu_pp(string)}" end assert expected == actual, msg end |
#assert_not_valid(record, *invalid_attrs) ⇒ Object
Asserts that the given active model record is not valid. If you provide a set of invalid attribute symbols, all of and only these attributes are expected to have errors. If no invalid attributes are specified, only the invalidity of the record is asserted.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/generators/dry_crud/templates/test/support/custom_assertions.rb', line 38 def assert_not_valid(record, *invalid_attrs) msg = do "Expected #{mu_pp(record)} to be invalid, but is valid." end assert_not record.valid?, msg if invalid_attrs.present? assert_invalid_attrs_have_errors(record, *invalid_attrs) assert_other_attrs_have_no_errors(record, *invalid_attrs) end end |
#assert_valid(record, msg = '') ⇒ Object
Asserts that the given active model record is valid. This method used to be part of Rails but was deprecated, no idea why.
24 25 26 27 28 29 30 31 32 |
# File 'lib/generators/dry_crud/templates/test/support/custom_assertions.rb', line 24 def assert_valid(record, msg = '') record.valid? msg = (msg) do "Expected #{mu_pp(record)} to be valid, " \ "but has the following errors:\n" + mu_pp(record.errors..join("\n")) end assert record.valid?, msg end |
#mu_pp(obj) ⇒ Object
The method used to by Test::Unit to format arguments. Prints ActiveRecord objects in a simpler format.
52 53 54 55 56 57 58 |
# File 'lib/generators/dry_crud/templates/test/support/custom_assertions.rb', line 52 def mu_pp(obj) if obj.is_a?(ActiveRecord::Base) # :nodoc: obj.to_s else super end end |