Module: Minitest::KineticCafeErrorAssertions
- Defined in:
- lib/kinetic_cafe/error/minitest.rb
Overview
Add assertions to Minitest for testing KineticCafe::Error objects.
Instance Method Summary collapse
-
#assert_kc_error(expected, actual, params = {}, msg = nil) ⇒ Object
Assert that the
actualexception received is theexpecteddescendant of KineticCafe::Error and that it has been constructed with the sameparamsprovided. -
#assert_kc_error_json(expected, actual, params = {}, msg = nil) ⇒ Object
Assert that the
actualstring received is theexpecteddescendant of KineticCafe::Error and that it has been constructed with the sameparamsprovided. -
#assert_response_kc_error(expected, params = {}, msg = nil) ⇒ Object
Assert that a reponse body (
@response.body, useful from ActionController::TestCase) is JSON for the expected error. -
#assert_response_kc_error_html(expected, msg = nil) ⇒ Object
Assert that a reponse body (
@response.body, useful from ActionController::TestCase) is HTML for the expected error.
Instance Method Details
#assert_kc_error(expected, actual, params = {}, msg = nil) ⇒ Object
Assert that the actual exception received is the expected descendant of KineticCafe::Error and that it has been constructed with the same params provided.
If a cause is not provided, any cause on the received error will be ignored.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kinetic_cafe/error/minitest.rb', line 10 def assert_kc_error expected, actual, params = {}, msg = nil msg, params = params, {} if msg.nil? && params.kind_of?(String) assert_kind_of KineticCafe::ErrorModule, actual, msg || "Expected #{actual} to be #{expected}, but it was not." assert_kind_of expected, actual, msg || "Expected #{actual} to be #{expected}, but it was not." unless params.key?(:cause) actual = actual.dup actual.instance_variable_set(:@cause, nil) actual.instance_variable_set(:@initialized_cause, true) actual.instance_variable_get(:@i18n_params).tap do |params| params.delete(:cause) end end expected = expected.new(params) assert_equal expected, actual, msg || "Expected #{actual} to be #{expected}, but it was not." end |
#assert_kc_error_json(expected, actual, params = {}, msg = nil) ⇒ Object
Assert that the actual string received is the expected descendant of KineticCafe::Error and that it has been constructed with the same params provided.
This differs from assert_kc_error in that comparison of the parsed JSON output is compared, not KineticCafe::Error objects. The JSON for the provided KineticCafe::Error object is generated through KineticCafe::Error#error_json.
If a cause is not provided, any cause on the received error will be ignored.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kinetic_cafe/error/minitest.rb', line 44 def assert_kc_error_json expected, actual, params = {}, msg = nil msg, params = params, {} if msg.nil? && params.kind_of?(String) msg ||= "Expected #{actual} to be JSON for #{expected}, but it was not." actual = JSON.parse(actual) unless params.key?(:cause) actual['error'].tap do |error| error.delete('cause') if error['i18n_params'] error['i18n_params'].delete('cause') error.delete('i18n_params') if error['i18n_params'].empty? end end actual end expected = JSON.parse(expected.new(params).error_result.to_json) assert_equal expected, actual, msg end |
#assert_response_kc_error(expected, params = {}, msg = nil) ⇒ Object
Assert that a reponse body (@response.body, useful from ActionController::TestCase) is JSON for the expected error. This is a convenience wrapper around #assert_kc_error_json or #assert_kc_error_html, depending on whether or not the response is HTML or not.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kinetic_cafe/error/minitest.rb', line 83 def assert_response_kc_error expected, params = {}, msg = nil msg, params = params, {} if msg.nil? && params.kind_of?(String) msg ||= "Expected response to be #{expected}, but was not." if @request.format.html? assert_response_kc_error_html expected, msg else assert_kc_error_json expected, @response.body, params, msg end end |
#assert_response_kc_error_html(expected, msg = nil) ⇒ Object
Assert that a reponse body (@response.body, useful from ActionController::TestCase) is HTML for the expected error.
68 69 70 71 72 73 74 75 76 |
# File 'lib/kinetic_cafe/error/minitest.rb', line 68 def assert_response_kc_error_html expected, msg = nil msg ||= "Expected #{actual} to be HTML for #{expected}, but it was not." assert_template 'kinetic_cafe_error/page', msg assert_template 'kinetic_cafe_error/_table', msg assert_match(/#{expected.i18n_key}/, @response.body, msg) assert_response expected.new.status, msg end |