Class: Tina4::Testing::TestContext
- Inherits:
-
Object
- Object
- Tina4::Testing::TestContext
- Defined in:
- lib/tina4/testing.rb
Instance Method Summary collapse
- #assert(condition, message = "Assertion failed") ⇒ Object
- #assert_equal(expected, actual, message = nil) ⇒ Object
- #assert_includes(collection, item, message = nil) ⇒ Object
- #assert_json(response_body) ⇒ Object
- #assert_match(pattern, string, message = nil) ⇒ Object
- #assert_nil(value, message = nil) ⇒ Object
- #assert_not_equal(expected, actual, message = nil) ⇒ Object
- #assert_not_nil(value, message = nil) ⇒ Object
- #assert_raises(exception_class, message = nil) ⇒ Object
- #assert_status(response, expected_status) ⇒ Object
- #delete(path, headers: {}) ⇒ Object
- #get(path, headers: {}, params: {}) ⇒ Object
- #post(path, body: nil, headers: {}) ⇒ Object
- #put(path, body: nil, headers: {}) ⇒ Object
-
#simulate_request(method, path, body: nil, headers: {}, params: {}) ⇒ Object
HTTP test helpers.
Instance Method Details
#assert(condition, message = "Assertion failed") ⇒ Object
110 111 112 |
# File 'lib/tina4/testing.rb', line 110 def assert(condition, = "Assertion failed") raise TestFailure, unless condition end |
#assert_equal(expected, actual, message = nil) ⇒ Object
114 115 116 117 |
# File 'lib/tina4/testing.rb', line 114 def assert_equal(expected, actual, = nil) msg = || "Expected #{expected.inspect}, got #{actual.inspect}" raise TestFailure, msg unless expected == actual end |
#assert_includes(collection, item, message = nil) ⇒ Object
134 135 136 137 |
# File 'lib/tina4/testing.rb', line 134 def assert_includes(collection, item, = nil) msg = || "Expected #{collection.inspect} to include #{item.inspect}" raise TestFailure, msg unless collection.include?(item) end |
#assert_json(response_body) ⇒ Object
151 152 153 154 155 |
# File 'lib/tina4/testing.rb', line 151 def assert_json(response_body) JSON.parse(response_body) rescue JSON::ParserError => e raise TestFailure, "Invalid JSON: #{e.}" end |
#assert_match(pattern, string, message = nil) ⇒ Object
146 147 148 149 |
# File 'lib/tina4/testing.rb', line 146 def assert_match(pattern, string, = nil) msg = || "Expected #{string.inspect} to match #{pattern.inspect}" raise TestFailure, msg unless pattern.match?(string) end |
#assert_nil(value, message = nil) ⇒ Object
124 125 126 127 |
# File 'lib/tina4/testing.rb', line 124 def assert_nil(value, = nil) msg = || "Expected nil, got #{value.inspect}" raise TestFailure, msg unless value.nil? end |
#assert_not_equal(expected, actual, message = nil) ⇒ Object
119 120 121 122 |
# File 'lib/tina4/testing.rb', line 119 def assert_not_equal(expected, actual, = nil) msg = || "Expected #{actual.inspect} to not equal #{expected.inspect}" raise TestFailure, msg if expected == actual end |
#assert_not_nil(value, message = nil) ⇒ Object
129 130 131 132 |
# File 'lib/tina4/testing.rb', line 129 def assert_not_nil(value, = nil) msg = || "Expected non-nil value" raise TestFailure, msg if value.nil? end |
#assert_raises(exception_class, message = nil) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/tina4/testing.rb', line 139 def assert_raises(exception_class, = nil) yield raise TestFailure, || "Expected #{exception_class} to be raised" rescue exception_class true end |
#assert_status(response, expected_status) ⇒ Object
157 158 159 160 |
# File 'lib/tina4/testing.rb', line 157 def assert_status(response, expected_status) actual = response.is_a?(Array) ? response[0] : response.status assert_equal(expected_status, actual, "Expected status #{expected_status}, got #{actual}") end |
#delete(path, headers: {}) ⇒ Object
181 182 183 |
# File 'lib/tina4/testing.rb', line 181 def delete(path, headers: {}) simulate_request("DELETE", path, headers: headers) end |
#get(path, headers: {}, params: {}) ⇒ Object
169 170 171 |
# File 'lib/tina4/testing.rb', line 169 def get(path, headers: {}, params: {}) simulate_request("GET", path, headers: headers, params: params) end |
#post(path, body: nil, headers: {}) ⇒ Object
173 174 175 |
# File 'lib/tina4/testing.rb', line 173 def post(path, body: nil, headers: {}) simulate_request("POST", path, body: body, headers: headers) end |
#put(path, body: nil, headers: {}) ⇒ Object
177 178 179 |
# File 'lib/tina4/testing.rb', line 177 def put(path, body: nil, headers: {}) simulate_request("PUT", path, body: body, headers: headers) end |
#simulate_request(method, path, body: nil, headers: {}, params: {}) ⇒ Object
HTTP test helpers
163 164 165 166 167 |
# File 'lib/tina4/testing.rb', line 163 def simulate_request(method, path, body: nil, headers: {}, params: {}) env = build_test_env(method, path, body: body, headers: headers, params: params) app = Tina4::RackApp.new app.call(env) end |