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_false(value, 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
- #assert_true(value, message = nil) ⇒ 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
227 228 229 |
# File 'lib/tina4/testing.rb', line 227 def assert(condition, = "Assertion failed") raise TestFailure, unless condition end |
#assert_equal(expected, actual, message = nil) ⇒ Object
231 232 233 234 |
# File 'lib/tina4/testing.rb', line 231 def assert_equal(expected, actual, = nil) msg = || "Expected #{expected.inspect}, got #{actual.inspect}" raise TestFailure, msg unless expected == actual end |
#assert_false(value, message = nil) ⇒ Object
268 269 270 271 |
# File 'lib/tina4/testing.rb', line 268 def assert_false(value, = nil) msg = || "Expected falsy, got #{value.inspect}" raise TestFailure, msg if value end |
#assert_includes(collection, item, message = nil) ⇒ Object
251 252 253 254 |
# File 'lib/tina4/testing.rb', line 251 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
278 279 280 281 282 |
# File 'lib/tina4/testing.rb', line 278 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
273 274 275 276 |
# File 'lib/tina4/testing.rb', line 273 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
241 242 243 244 |
# File 'lib/tina4/testing.rb', line 241 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
236 237 238 239 |
# File 'lib/tina4/testing.rb', line 236 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
246 247 248 249 |
# File 'lib/tina4/testing.rb', line 246 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
256 257 258 259 260 261 |
# File 'lib/tina4/testing.rb', line 256 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
284 285 286 287 |
# File 'lib/tina4/testing.rb', line 284 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 |
#assert_true(value, message = nil) ⇒ Object
263 264 265 266 |
# File 'lib/tina4/testing.rb', line 263 def assert_true(value, = nil) msg = || "Expected truthy, got #{value.inspect}" raise TestFailure, msg unless value end |
#delete(path, headers: {}) ⇒ Object
308 309 310 |
# File 'lib/tina4/testing.rb', line 308 def delete(path, headers: {}) simulate_request("DELETE", path, headers: headers) end |
#get(path, headers: {}, params: {}) ⇒ Object
296 297 298 |
# File 'lib/tina4/testing.rb', line 296 def get(path, headers: {}, params: {}) simulate_request("GET", path, headers: headers, params: params) end |
#post(path, body: nil, headers: {}) ⇒ Object
300 301 302 |
# File 'lib/tina4/testing.rb', line 300 def post(path, body: nil, headers: {}) simulate_request("POST", path, body: body, headers: headers) end |
#put(path, body: nil, headers: {}) ⇒ Object
304 305 306 |
# File 'lib/tina4/testing.rb', line 304 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
290 291 292 293 294 |
# File 'lib/tina4/testing.rb', line 290 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 |