Testing
Basic Service Testing
Testing a Simple Service
# app/services/greet_service.rb
class GreetService < ApplicationService
arg :name, type: String
step :greet
output :message
private
def greet
self.message = "Hello, #{name}!"
end
end# spec/services/greet_service_spec.rb
RSpec.describe GreetService do
describe ".run" do
it "returns a greeting message" do
service = described_class.run(name: "John")
expect(service).to be_successful
expect(service.message).to eq("Hello, John!")
end
end
endTesting Success and Failure
Testing with Context
Testing Child Services in Context
Testing Conditional Steps
Testing Early Exit with stop!
Testing Configuration Overrides
Testing run! vs run
Testing Warnings
Mocking External Services
Testing Argument Validation
Shared Examples for CRUD Services
Test Helpers
Custom RSpec Matchers
Setup
DSL Definition Matchers
define_argument
define_argumentdefine_output
define_outputdefine_step and define_steps
define_step and define_stepsError and Warning Matchers
have_error_on and have_errors_on
have_error_on and have_errors_onhave_warning_on and have_warnings_on
have_warning_on and have_warnings_onExecution Matchers
execute_step and skip_step
execute_step and skip_stepexecute_steps and execute_steps_in_order
execute_steps and execute_steps_in_orderCallback Matchers
trigger_callback
trigger_callbackWhat's Next?
Last updated