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

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kinetic_cafe/error/minitest.rb', line 7

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."

  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.



29
30
31
32
33
34
35
36
37
# File 'lib/kinetic_cafe/error/minitest.rb', line 29

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)
  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.



56
57
58
59
60
61
62
63
64
65
# File 'lib/kinetic_cafe/error/minitest.rb', line 56

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.



41
42
43
44
45
46
47
48
49
# File 'lib/kinetic_cafe/error/minitest.rb', line 41

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