Class: Syntropy::Test
- Inherits:
-
Minitest::Test
- Object
- Minitest::Test
- Syntropy::Test
- Defined in:
- lib/syntropy/test.rb
Overview
Test provides a class for testing a Syntropy app, based on Minitest.
Constant Summary collapse
Class Attribute Summary collapse
-
.env ⇒ Object
Gets/sets app environment for tests.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#machine ⇒ Object
readonly
Returns the value of attribute machine.
Class Method Summary collapse
Instance Method Summary collapse
-
#env ⇒ Hash
Returns the test environment.
-
#get(path, **headers) ⇒ Syntropy::Request
Makes an HTTP GET request to the test app.
-
#http_request(headers, body = nil) ⇒ Syntropy::Request
Makes an HTTP request to the test app.
-
#load_module(ref, raise_on_missing: true) ⇒ any
Loads and returns a module with the given reference.
-
#patch(path, content_type, body, **headers) ⇒ Syntropy::Request
Makes an HTTP PATCH request to the test app.
-
#post(path, content_type, body, **headers) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app.
-
#post_form(path, data) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app with a “application/x-www-form-urlencoded” content type.
-
#post_json(path, data) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app with a “application/json” content type.
-
#setup ⇒ void
Sets up a test instance.
-
#teardown ⇒ void
Cleans up a test instance.
Class Attribute Details
.env ⇒ Object
Gets/sets app environment for tests
24 25 26 |
# File 'lib/syntropy/test.rb', line 24 def env @env end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
27 28 29 |
# File 'lib/syntropy/test.rb', line 27 def app @app end |
#machine ⇒ Object (readonly)
Returns the value of attribute machine.
27 28 29 |
# File 'lib/syntropy/test.rb', line 27 def machine @machine end |
Class Method Details
.global_env ⇒ Object
19 20 21 |
# File 'lib/syntropy/test.rb', line 19 def global_env @@global_env end |
.global_env=(env) ⇒ Object
15 16 17 |
# File 'lib/syntropy/test.rb', line 15 def global_env=(env) @@global_env = env end |
Instance Method Details
#env ⇒ Hash
Returns the test environment.
32 33 34 35 |
# File 'lib/syntropy/test.rb', line 32 def env klass = self.class klass.env || klass.global_env end |
#get(path, **headers) ⇒ Syntropy::Request
Makes an HTTP GET request to the test app.
59 60 61 62 63 64 65 66 |
# File 'lib/syntropy/test.rb', line 59 def get(path, **headers) http_request( headers.merge( ':method' => 'GET', ':path' => path ) ) end |
#http_request(headers, body = nil) ⇒ Syntropy::Request
Makes an HTTP request to the test app.
50 51 52 |
# File 'lib/syntropy/test.rb', line 50 def http_request(headers, body = nil) @test_harness.request(headers, body) end |
#load_module(ref, raise_on_missing: true) ⇒ any
Loads and returns a module with the given reference.
41 42 43 |
# File 'lib/syntropy/test.rb', line 41 def load_module(ref, raise_on_missing: true) app.module_loader.load(ref, raise_on_missing:) end |
#patch(path, content_type, body, **headers) ⇒ Syntropy::Request
Makes an HTTP PATCH request to the test app.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/syntropy/test.rb', line 117 def patch(path, content_type, body, **headers) headers = headers.merge('content-type' => content_type) if content_type http_request( headers.merge( { ':method' => 'PATCH', ':path' => path } ), body ) end |
#post(path, content_type, body, **headers) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/syntropy/test.rb', line 75 def post(path, content_type, body, **headers) headers = headers.merge('content-type' => content_type) if content_type http_request( headers.merge( { ':method' => 'POST', ':path' => path } ), body ) end |
#post_form(path, data) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app with a “application/x-www-form-urlencoded” content type. The given data is converted to URL Encoded form format and sent as the request body.
106 107 108 |
# File 'lib/syntropy/test.rb', line 106 def post_form(path, data, **) post(path, 'application/x-www-form-urlencoded', URI.encode_www_form(data), **) end |
#post_json(path, data) ⇒ Syntropy::Request
Makes an HTTP POST request to the test app with a “application/json” content type. The given object is converted to JSON and sent as the request body.
95 96 97 |
# File 'lib/syntropy/test.rb', line 95 def post_json(path, data, **) post(path, 'application/json', JSON.dump(data), **) end |
#setup ⇒ void
This method returns an undefined value.
Sets up a test instance.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/syntropy/test.rb', line 133 def setup env = self.env raise 'Environment not set' if !env Syntropy.load_config(env) @machine = UM.new @app = Syntropy::App.new( **env.merge( machine: @machine, test_mode: true ) ) @test_harness = Syntropy::TestHarness.new(@app) @db = load_module('/_lib/storage', raise_on_missing: false) @db&.migrate! if @db.respond_to?(:migrate!) end |
#teardown ⇒ void
This method returns an undefined value.
Cleans up a test instance.
155 156 157 158 159 |
# File 'lib/syntropy/test.rb', line 155 def teardown @machine = nil @app = nil @test_harness = nil end |