Module: Servus::Testing::ExampleBuilders
- Defined in:
- lib/servus/testing/example_builders.rb
Overview
Provides helper methods for extracting example values from service schemas.
This module is designed to be included in test files (RSpec, Minitest, etc.) to provide convenient access to schema example values. It’s particularly useful for generating test fixtures without manually maintaining separate factory files.
The ‘servus_` prefix on method names prevents naming collisions with other testing libraries and makes it clear these are Servus-specific helpers.
Instance Method Summary collapse
-
#servus_arguments_example(service_class, overrides = {}) ⇒ Hash<Symbol, Object>
Extracts example argument values from a service’s schema.
-
#servus_failure_example(service_class, overrides = {}) ⇒ Servus::Support::Response
Extracts example failure data values from a service’s schema.
-
#servus_failure_response(message = nil, data: nil, type: Servus::Support::Errors::ServiceError) ⇒ Servus::Support::Response
Builds a failure Response object with the given message and error type.
-
#servus_result_example(service_class, overrides = {}) ⇒ Servus::Support::Response
Extracts example result values from a service’s schema.
-
#servus_success_response(data = {}) ⇒ Servus::Support::Response
Builds a successful Response object with the given data.
Instance Method Details
#servus_arguments_example(service_class, overrides = {}) ⇒ Hash<Symbol, Object>
Override keys can be strings or symbols; they’ll be converted to symbols
Returns empty hash if service has no arguments schema defined
Extracts example argument values from a service’s schema.
Looks for ‘example` or `examples` keywords in the service’s arguments schema and returns them as a hash ready to be passed to the service’s ‘.call` method.
75 76 77 |
# File 'lib/servus/testing/example_builders.rb', line 75 def servus_arguments_example(service_class, overrides = {}) extract_example_from(service_class, :arguments, overrides) end |
#servus_failure_example(service_class, overrides = {}) ⇒ Servus::Support::Response
Override keys can be strings or symbols; they’ll be converted to symbols
Returns empty hash if service has no failure schema defined
Extracts example failure data values from a service’s schema.
Looks for ‘example` or `examples` keywords in the service’s failure schema and returns them wrapped in a failure Response. Useful for validating failure response structure in tests.
136 137 138 139 |
# File 'lib/servus/testing/example_builders.rb', line 136 def servus_failure_example(service_class, overrides = {}) example = extract_example_from(service_class, :failure, overrides) Servus::Support::Response.new(false, example, Servus::Support::Errors::ServiceError.new) end |
#servus_failure_response(message = nil, data: nil, type: Servus::Support::Errors::ServiceError) ⇒ Servus::Support::Response
Builds a failure Response object with the given message and error type.
Convenience method for creating failure responses in tests. Mirrors the signature of Base#failure.
188 189 190 191 |
# File 'lib/servus/testing/example_builders.rb', line 188 def servus_failure_response( = nil, data: nil, type: Servus::Support::Errors::ServiceError) error = type.new() Servus::Support::Response.new(false, data, error) end |
#servus_result_example(service_class, overrides = {}) ⇒ Servus::Support::Response
Override keys can be strings or symbols; they’ll be converted to symbols
Returns empty hash if service has no result schema defined
Extracts example result values from a service’s schema.
Looks for ‘example` or `examples` keywords in the service’s result schema and returns them as a hash. Useful for validating service response structure and expected data shapes in tests.
113 114 115 116 117 |
# File 'lib/servus/testing/example_builders.rb', line 113 def servus_result_example(service_class, overrides = {}) example = extract_example_from(service_class, :result, overrides) # Wrap in a successful Response object Servus::Support::Response.new(true, example, nil) end |
#servus_success_response(data = {}) ⇒ Servus::Support::Response
Builds a successful Response object with the given data.
Convenience method for creating successful responses in tests without calling Servus::Support::Response.new directly.
160 161 162 |
# File 'lib/servus/testing/example_builders.rb', line 160 def servus_success_response(data = {}) Servus::Support::Response.new(true, data, nil) end |