Module: Whoosh::Test
- Includes:
- Rack::Test::Methods
- Defined in:
- lib/whoosh/test.rb
Instance Method Summary collapse
-
#app ⇒ Object
Override in your test class to return the Rack app.
- #assert_json(expected_hash) ⇒ Object
- #assert_json_includes(key) ⇒ Object
- #assert_json_path(path, expected_value) ⇒ Object
-
#assert_response(expected_status) ⇒ Object
— Response assertions —.
- #get_with_auth(path, key:, header: "HTTP_X_API_KEY") ⇒ Object
- #patch_json(path, body = {}, headers: {}) ⇒ Object
-
#post_json(path, body = {}, headers: {}) ⇒ Object
— Request helpers —.
- #post_with_auth(path, body = {}, key:, header: "HTTP_X_API_KEY") ⇒ Object
- #put_json(path, body = {}, headers: {}) ⇒ Object
- #response_json ⇒ Object
Instance Method Details
#app ⇒ Object
Override in your test class to return the Rack app
12 13 14 |
# File 'lib/whoosh/test.rb', line 12 def app raise NotImplementedError, "Define #app in your test class to return a Rack app" end |
#assert_json(expected_hash) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/whoosh/test.rb', line 47 def assert_json(expected_hash) body = JSON.parse(last_response.body) expected_hash.each do |key, value| expect(body[key.to_s]).to eq(value) end end |
#assert_json_includes(key) ⇒ Object
61 62 63 64 |
# File 'lib/whoosh/test.rb', line 61 def assert_json_includes(key) body = JSON.parse(last_response.body) expect(body).to have_key(key.to_s) end |
#assert_json_path(path, expected_value) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/whoosh/test.rb', line 54 def assert_json_path(path, expected_value) body = JSON.parse(last_response.body) keys = path.to_s.split(".") value = keys.reduce(body) { |h, k| h.is_a?(Hash) ? h[k] : nil } expect(value).to eq(expected_value) end |
#assert_response(expected_status) ⇒ Object
— Response assertions —
43 44 45 |
# File 'lib/whoosh/test.rb', line 43 def assert_response(expected_status) expect(last_response.status).to eq(expected_status) end |
#get_with_auth(path, key:, header: "HTTP_X_API_KEY") ⇒ Object
33 34 35 |
# File 'lib/whoosh/test.rb', line 33 def get_with_auth(path, key:, header: "HTTP_X_API_KEY") get path, {}, { header => key } end |
#patch_json(path, body = {}, headers: {}) ⇒ Object
28 29 30 31 |
# File 'lib/whoosh/test.rb', line 28 def patch_json(path, body = {}, headers: {}) merged_headers = { "CONTENT_TYPE" => "application/json" }.merge(headers) patch path, JSON.generate(body), merged_headers end |
#post_json(path, body = {}, headers: {}) ⇒ Object
— Request helpers —
18 19 20 21 |
# File 'lib/whoosh/test.rb', line 18 def post_json(path, body = {}, headers: {}) merged_headers = { "CONTENT_TYPE" => "application/json" }.merge(headers) post path, JSON.generate(body), merged_headers end |
#post_with_auth(path, body = {}, key:, header: "HTTP_X_API_KEY") ⇒ Object
37 38 39 |
# File 'lib/whoosh/test.rb', line 37 def post_with_auth(path, body = {}, key:, header: "HTTP_X_API_KEY") post path, JSON.generate(body), { "CONTENT_TYPE" => "application/json", header => key } end |
#put_json(path, body = {}, headers: {}) ⇒ Object
23 24 25 26 |
# File 'lib/whoosh/test.rb', line 23 def put_json(path, body = {}, headers: {}) merged_headers = { "CONTENT_TYPE" => "application/json" }.merge(headers) put path, JSON.generate(body), merged_headers end |
#response_json ⇒ Object
66 67 68 |
# File 'lib/whoosh/test.rb', line 66 def response_json JSON.parse(last_response.body) end |