servus 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +6 -0
- data/CHANGELOG.md +8 -1
- data/IDEAS.md +5 -0
- data/READme.md +147 -42
- data/Rakefile +33 -0
- data/builds/servus-0.1.2.gem +0 -0
- data/builds/servus-0.1.3.gem +0 -0
- data/builds/servus-0.1.4.gem +0 -0
- data/docs/core/1_overview.md +77 -0
- data/docs/core/2_architecture.md +92 -0
- data/docs/core/3_service_objects.md +121 -0
- data/docs/features/1_schema_validation.md +119 -0
- data/docs/features/2_error_handling.md +121 -0
- data/docs/features/3_async_execution.md +81 -0
- data/docs/features/4_logging.md +64 -0
- data/docs/guides/1_common_patterns.md +90 -0
- data/docs/guides/2_migration_guide.md +175 -0
- data/docs/integration/1_configuration.md +51 -0
- data/docs/integration/2_testing.md +164 -0
- data/docs/integration/3_rails_integration.md +99 -0
- data/docs/yard/Servus/Base.html +1645 -0
- data/docs/yard/Servus/Config.html +582 -0
- data/docs/yard/Servus/Extensions/Async/Call.html +400 -0
- data/docs/yard/Servus/Extensions/Async/Errors/AsyncError.html +140 -0
- data/docs/yard/Servus/Extensions/Async/Errors/JobEnqueueError.html +154 -0
- data/docs/yard/Servus/Extensions/Async/Errors/ServiceNotFoundError.html +154 -0
- data/docs/yard/Servus/Extensions/Async/Errors.html +128 -0
- data/docs/yard/Servus/Extensions/Async/Ext.html +119 -0
- data/docs/yard/Servus/Extensions/Async/Job.html +310 -0
- data/docs/yard/Servus/Extensions/Async.html +141 -0
- data/docs/yard/Servus/Extensions.html +117 -0
- data/docs/yard/Servus/Generators/ServiceGenerator.html +261 -0
- data/docs/yard/Servus/Generators.html +115 -0
- data/docs/yard/Servus/Helpers/ControllerHelpers.html +457 -0
- data/docs/yard/Servus/Helpers.html +115 -0
- data/docs/yard/Servus/Railtie.html +134 -0
- data/docs/yard/Servus/Support/Errors/AuthenticationError.html +287 -0
- data/docs/yard/Servus/Support/Errors/BadRequestError.html +283 -0
- data/docs/yard/Servus/Support/Errors/ForbiddenError.html +284 -0
- data/docs/yard/Servus/Support/Errors/InternalServerError.html +283 -0
- data/docs/yard/Servus/Support/Errors/NotFoundError.html +284 -0
- data/docs/yard/Servus/Support/Errors/ServiceError.html +489 -0
- data/docs/yard/Servus/Support/Errors/ServiceUnavailableError.html +290 -0
- data/docs/yard/Servus/Support/Errors/UnauthorizedError.html +200 -0
- data/docs/yard/Servus/Support/Errors/UnprocessableEntityError.html +288 -0
- data/docs/yard/Servus/Support/Errors/ValidationError.html +200 -0
- data/docs/yard/Servus/Support/Errors.html +140 -0
- data/docs/yard/Servus/Support/Logger.html +856 -0
- data/docs/yard/Servus/Support/Rescuer/BlockContext.html +585 -0
- data/docs/yard/Servus/Support/Rescuer/CallOverride.html +257 -0
- data/docs/yard/Servus/Support/Rescuer/ClassMethods.html +343 -0
- data/docs/yard/Servus/Support/Rescuer.html +267 -0
- data/docs/yard/Servus/Support/Response.html +574 -0
- data/docs/yard/Servus/Support/Validator.html +1150 -0
- data/docs/yard/Servus/Support.html +119 -0
- data/docs/yard/Servus/Testing/ExampleBuilders.html +523 -0
- data/docs/yard/Servus/Testing/ExampleExtractor.html +578 -0
- data/docs/yard/Servus/Testing.html +142 -0
- data/docs/yard/Servus.html +343 -0
- data/docs/yard/_index.html +535 -0
- data/docs/yard/class_list.html +54 -0
- data/docs/yard/css/common.css +1 -0
- data/docs/yard/css/full_list.css +58 -0
- data/docs/yard/css/style.css +503 -0
- data/docs/yard/file.1_common_patterns.html +154 -0
- data/docs/yard/file.1_configuration.html +115 -0
- data/docs/yard/file.1_overview.html +142 -0
- data/docs/yard/file.1_schema_validation.html +188 -0
- data/docs/yard/file.2_architecture.html +157 -0
- data/docs/yard/file.2_error_handling.html +190 -0
- data/docs/yard/file.2_migration_guide.html +242 -0
- data/docs/yard/file.2_testing.html +227 -0
- data/docs/yard/file.3_async_execution.html +145 -0
- data/docs/yard/file.3_rails_integration.html +160 -0
- data/docs/yard/file.3_service_objects.html +191 -0
- data/docs/yard/file.4_logging.html +135 -0
- data/docs/yard/file.ErrorHandling.html +190 -0
- data/docs/yard/file.READme.html +674 -0
- data/docs/yard/file.architecture.html +157 -0
- data/docs/yard/file.async_execution.html +145 -0
- data/docs/yard/file.common_patterns.html +154 -0
- data/docs/yard/file.configuration.html +115 -0
- data/docs/yard/file.error_handling.html +190 -0
- data/docs/yard/file.logging.html +135 -0
- data/docs/yard/file.migration_guide.html +242 -0
- data/docs/yard/file.overview.html +142 -0
- data/docs/yard/file.rails_integration.html +160 -0
- data/docs/yard/file.schema_validation.html +188 -0
- data/docs/yard/file.service_objects.html +191 -0
- data/docs/yard/file.testing.html +227 -0
- data/docs/yard/file_list.html +119 -0
- data/docs/yard/frames.html +22 -0
- data/docs/yard/index.html +674 -0
- data/docs/yard/js/app.js +344 -0
- data/docs/yard/js/full_list.js +242 -0
- data/docs/yard/js/jquery.js +4 -0
- data/docs/yard/method_list.html +542 -0
- data/docs/yard/top-level-namespace.html +110 -0
- data/lib/generators/servus/service/service_generator.rb +64 -1
- data/lib/generators/servus/service/templates/service.rb.erb +1 -1
- data/lib/servus/base.rb +258 -57
- data/lib/servus/config.rb +58 -12
- data/lib/servus/extensions/async/call.rb +50 -18
- data/lib/servus/extensions/async/errors.rb +23 -3
- data/lib/servus/extensions/async/ext.rb +10 -2
- data/lib/servus/extensions/async/job.rb +32 -11
- data/lib/servus/helpers/controller_helpers.rb +73 -37
- data/lib/servus/support/errors.rb +135 -45
- data/lib/servus/support/rescuer.rb +189 -36
- data/lib/servus/support/response.rb +49 -7
- data/lib/servus/support/validator.rb +120 -19
- data/lib/servus/testing/example_builders.rb +133 -0
- data/lib/servus/testing/example_extractor.rb +309 -0
- data/lib/servus/testing.rb +17 -0
- data/lib/servus/version.rb +1 -1
- metadata +118 -19
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>
|
|
7
|
+
File: Guides / 2. Migration Guide
|
|
8
|
+
|
|
9
|
+
— Servus | Service Object Framework
|
|
10
|
+
|
|
11
|
+
</title>
|
|
12
|
+
|
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
|
14
|
+
|
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
|
16
|
+
|
|
17
|
+
<script type="text/javascript">
|
|
18
|
+
pathId = "2_migration_guide";
|
|
19
|
+
relpath = '';
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
24
|
+
|
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<div class="nav_wrap">
|
|
31
|
+
<iframe id="nav" src="file_list.html?1"></iframe>
|
|
32
|
+
<div id="resizer"></div>
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
<div id="main" tabindex="-1">
|
|
36
|
+
<div id="header">
|
|
37
|
+
<div id="menu">
|
|
38
|
+
|
|
39
|
+
<a href="_index.html">Index</a> »
|
|
40
|
+
<span class="title">File: Guides / 2. Migration Guide</span>
|
|
41
|
+
|
|
42
|
+
</div>
|
|
43
|
+
|
|
44
|
+
<div id="search">
|
|
45
|
+
|
|
46
|
+
<a class="full_list_link" id="class_list_link"
|
|
47
|
+
href="class_list.html">
|
|
48
|
+
|
|
49
|
+
<svg width="24" height="24">
|
|
50
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
|
51
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
|
52
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
|
53
|
+
</svg>
|
|
54
|
+
</a>
|
|
55
|
+
|
|
56
|
+
</div>
|
|
57
|
+
<div class="clear"></div>
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<div id="content"><div id='filecontents'><h1 id="migration-guide">Migration Guide</h1>
|
|
61
|
+
|
|
62
|
+
<p>Strategies for adopting Servus in existing Rails applications.</p>
|
|
63
|
+
|
|
64
|
+
<h2 id="incremental-adoption">Incremental Adoption</h2>
|
|
65
|
+
|
|
66
|
+
<p>Servus coexists with existing code - no need to rewrite your entire application. Start with one complex use case, validate the pattern works for your team, then expand gradually.</p>
|
|
67
|
+
|
|
68
|
+
<h2 id="extracting-from-fat-controllers">Extracting from Fat Controllers</h2>
|
|
69
|
+
|
|
70
|
+
<p>Identify controller actions with complex business logic and extract to services:</p>
|
|
71
|
+
|
|
72
|
+
<p><strong>Before</strong>:</p>
|
|
73
|
+
|
|
74
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>OrdersController</span> <span class='op'><</span> <span class='const'>ApplicationController</span>
|
|
75
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_create'>create</span>
|
|
76
|
+
<span class='comment'># 50 lines of business logic
|
|
77
|
+
</span> <span class='comment'># Multiple model operations
|
|
78
|
+
</span> <span class='comment'># External API calls
|
|
79
|
+
</span> <span class='comment'># Email sending
|
|
80
|
+
</span> <span class='kw'>end</span>
|
|
81
|
+
<span class='kw'>end</span>
|
|
82
|
+
</code></pre>
|
|
83
|
+
|
|
84
|
+
<p><strong>After</strong>:</p>
|
|
85
|
+
|
|
86
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>OrdersController</span> <span class='op'><</span> <span class='const'>ApplicationController</span>
|
|
87
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_create'>create</span>
|
|
88
|
+
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='const'>Orders</span><span class='op'>::</span><span class='const'>Create</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_order_params'>order_params</span><span class='rparen'>)</span>
|
|
89
|
+
<span class='kw'>if</span> <span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_success?'>success?</span>
|
|
90
|
+
<span class='id identifier rubyid_render'>render</span> <span class='label'>json:</span> <span class='lbrace'>{</span> <span class='label'>order:</span> <span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='symbol'>:order</span><span class='rbracket'>]</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>status:</span> <span class='symbol'>:created</span>
|
|
91
|
+
<span class='kw'>else</span>
|
|
92
|
+
<span class='id identifier rubyid_render'>render</span> <span class='label'>json:</span> <span class='lbrace'>{</span> <span class='label'>error:</span> <span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_error'>error</span><span class='period'>.</span><span class='id identifier rubyid_api_error'>api_error</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>status:</span> <span class='symbol'>:unprocessable_entity</span>
|
|
93
|
+
<span class='kw'>end</span>
|
|
94
|
+
<span class='kw'>end</span>
|
|
95
|
+
<span class='kw'>end</span>
|
|
96
|
+
|
|
97
|
+
<span class='comment'># Or use the helper
|
|
98
|
+
</span><span class='kw'>class</span> <span class='const'>OrdersController</span> <span class='op'><</span> <span class='const'>ApplicationController</span>
|
|
99
|
+
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Helpers.html" title="Servus::Helpers (module)">Helpers</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Helpers/ControllerHelpers.html" title="Servus::Helpers::ControllerHelpers (module)">ControllerHelpers</a></span></span>
|
|
100
|
+
|
|
101
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_create'>create</span>
|
|
102
|
+
<span class='id identifier rubyid_run_service'>run_service</span><span class='lparen'>(</span><span class='const'>Orders</span><span class='op'>::</span><span class='const'>Create</span><span class='op'>::</span><span class='const'>Service</span><span class='comma'>,</span> <span class='id identifier rubyid_order_params'>order_params</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_result'>result</span><span class='op'>|</span>
|
|
103
|
+
<span class='id identifier rubyid_render'>render</span> <span class='label'>json:</span> <span class='lbrace'>{</span> <span class='label'>order:</span> <span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='symbol'>:order</span><span class='rbracket'>]</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>status:</span> <span class='symbol'>:created</span>
|
|
104
|
+
<span class='kw'>end</span>
|
|
105
|
+
<span class='kw'>end</span>
|
|
106
|
+
<span class='kw'>end</span>
|
|
107
|
+
</code></pre>
|
|
108
|
+
|
|
109
|
+
<h2 id="extracting-from-fat-models">Extracting from Fat Models</h2>
|
|
110
|
+
|
|
111
|
+
<p>Move orchestration logic from models to services. Keep data-related methods in models:</p>
|
|
112
|
+
|
|
113
|
+
<p><strong>Before</strong>:</p>
|
|
114
|
+
|
|
115
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Order</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
116
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_complete_purchase'>complete_purchase</span>
|
|
117
|
+
<span class='id identifier rubyid_charge_payment'>charge_payment</span>
|
|
118
|
+
<span class='id identifier rubyid_update_inventory'>update_inventory</span>
|
|
119
|
+
<span class='id identifier rubyid_send_confirmation_email'>send_confirmation_email</span>
|
|
120
|
+
<span class='id identifier rubyid_create_invoice'>create_invoice</span>
|
|
121
|
+
<span class='kw'>end</span>
|
|
122
|
+
<span class='kw'>end</span>
|
|
123
|
+
</code></pre>
|
|
124
|
+
|
|
125
|
+
<p><strong>After</strong>:</p>
|
|
126
|
+
|
|
127
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Order</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
128
|
+
<span class='comment'># Model focuses on data
|
|
129
|
+
</span> <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:total</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
|
|
130
|
+
<span class='id identifier rubyid_belongs_to'>belongs_to</span> <span class='symbol'>:user</span>
|
|
131
|
+
<span class='kw'>end</span>
|
|
132
|
+
|
|
133
|
+
<span class='kw'>class</span> <span class='const'>Orders</span><span class='op'>::</span><span class='const'>CompletePurchase</span><span class='op'>::</span><span class='const'>Service</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Base.html" title="Servus::Base (class)">Base</a></span></span>
|
|
134
|
+
<span class='comment'># Service handles orchestration
|
|
135
|
+
</span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>order_id:</span><span class='rparen'>)</span>
|
|
136
|
+
<span class='ivar'>@order_id</span> <span class='op'>=</span> <span class='id identifier rubyid_order_id'>order_id</span>
|
|
137
|
+
<span class='kw'>end</span>
|
|
138
|
+
|
|
139
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
|
140
|
+
<span class='id identifier rubyid_order'>order</span> <span class='op'>=</span> <span class='const'>Order</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='ivar'>@order_id</span><span class='rparen'>)</span>
|
|
141
|
+
<span class='const'>Payments</span><span class='op'>::</span><span class='const'>Charge</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='label'>order_id:</span> <span class='id identifier rubyid_order'>order</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='rparen'>)</span>
|
|
142
|
+
<span class='const'>Inventory</span><span class='op'>::</span><span class='const'>Update</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='label'>order_id:</span> <span class='id identifier rubyid_order'>order</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='rparen'>)</span>
|
|
143
|
+
<span class='const'>Mailers</span><span class='op'>::</span><span class='const'>SendConfirmation</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='label'>order_id:</span> <span class='id identifier rubyid_order'>order</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='rparen'>)</span>
|
|
144
|
+
<span class='id identifier rubyid_success'>success</span><span class='lparen'>(</span><span class='label'>order:</span> <span class='id identifier rubyid_order'>order</span><span class='rparen'>)</span>
|
|
145
|
+
<span class='kw'>end</span>
|
|
146
|
+
<span class='kw'>end</span>
|
|
147
|
+
</code></pre>
|
|
148
|
+
|
|
149
|
+
<h2 id="replacing-callbacks">Replacing Callbacks</h2>
|
|
150
|
+
|
|
151
|
+
<p>Extract callback logic to explicit service calls:</p>
|
|
152
|
+
|
|
153
|
+
<p><strong>Before</strong>:</p>
|
|
154
|
+
|
|
155
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
156
|
+
<span class='id identifier rubyid_after_create'>after_create</span> <span class='symbol'>:send_welcome_email</span>
|
|
157
|
+
<span class='id identifier rubyid_after_create'>after_create</span> <span class='symbol'>:create_default_account</span>
|
|
158
|
+
<span class='id identifier rubyid_after_update'>after_update</span> <span class='symbol'>:notify_changes</span><span class='comma'>,</span> <span class='label'>if:</span> <span class='symbol'>:email_changed?</span>
|
|
159
|
+
<span class='kw'>end</span>
|
|
160
|
+
</code></pre>
|
|
161
|
+
|
|
162
|
+
<p><strong>After</strong>:</p>
|
|
163
|
+
|
|
164
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
165
|
+
<span class='comment'># Minimal or no callbacks
|
|
166
|
+
</span><span class='kw'>end</span>
|
|
167
|
+
|
|
168
|
+
<span class='kw'>class</span> <span class='const'>Users</span><span class='op'>::</span><span class='const'>Create</span><span class='op'>::</span><span class='const'>Service</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Base.html" title="Servus::Base (class)">Base</a></span></span>
|
|
169
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
|
170
|
+
<span class='id identifier rubyid_user'>user</span> <span class='op'>=</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_create!'>create!</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span><span class='rparen'>)</span>
|
|
171
|
+
<span class='id identifier rubyid_send_welcome_email'>send_welcome_email</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
|
|
172
|
+
<span class='id identifier rubyid_create_default_account'>create_default_account</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
|
|
173
|
+
<span class='id identifier rubyid_success'>success</span><span class='lparen'>(</span><span class='label'>user:</span> <span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
|
|
174
|
+
<span class='kw'>end</span>
|
|
175
|
+
<span class='kw'>end</span>
|
|
176
|
+
</code></pre>
|
|
177
|
+
|
|
178
|
+
<h2 id="migrating-background-jobs">Migrating Background Jobs</h2>
|
|
179
|
+
|
|
180
|
+
<p>Extract job logic to services, call via <code>.call_async</code>:</p>
|
|
181
|
+
|
|
182
|
+
<p><strong>Before</strong>:</p>
|
|
183
|
+
|
|
184
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProcessOrderJob</span> <span class='op'><</span> <span class='const'>ApplicationJob</span>
|
|
185
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_perform'>perform</span><span class='lparen'>(</span><span class='id identifier rubyid_order_id'>order_id</span><span class='rparen'>)</span>
|
|
186
|
+
<span class='comment'># 50 lines of business logic
|
|
187
|
+
</span> <span class='kw'>end</span>
|
|
188
|
+
<span class='kw'>end</span>
|
|
189
|
+
|
|
190
|
+
<span class='const'>ProcessOrderJob</span><span class='period'>.</span><span class='id identifier rubyid_perform_later'>perform_later</span><span class='lparen'>(</span><span class='id identifier rubyid_order'>order</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='rparen'>)</span>
|
|
191
|
+
</code></pre>
|
|
192
|
+
|
|
193
|
+
<p><strong>After</strong>:</p>
|
|
194
|
+
|
|
195
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Orders</span><span class='op'>::</span><span class='const'>Process</span><span class='op'>::</span><span class='const'>Service</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Base.html" title="Servus::Base (class)">Base</a></span></span>
|
|
196
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>order_id:</span><span class='rparen'>)</span>
|
|
197
|
+
<span class='ivar'>@order_id</span> <span class='op'>=</span> <span class='id identifier rubyid_order_id'>order_id</span>
|
|
198
|
+
<span class='kw'>end</span>
|
|
199
|
+
|
|
200
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span>
|
|
201
|
+
<span class='comment'># Business logic
|
|
202
|
+
</span> <span class='kw'>end</span>
|
|
203
|
+
<span class='kw'>end</span>
|
|
204
|
+
|
|
205
|
+
<span class='const'>Orders</span><span class='op'>::</span><span class='const'>Process</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call_async'>call_async</span><span class='lparen'>(</span><span class='label'>order_id:</span> <span class='id identifier rubyid_order'>order</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='rparen'>)</span>
|
|
206
|
+
</code></pre>
|
|
207
|
+
|
|
208
|
+
<p>Now the service can be called synchronously (from console, tests) or asynchronously (from controllers, jobs).</p>
|
|
209
|
+
|
|
210
|
+
<h2 id="testing-during-migration">Testing During Migration</h2>
|
|
211
|
+
|
|
212
|
+
<p>Keep existing tests working while adding service tests:</p>
|
|
213
|
+
|
|
214
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># Keep existing controller test
|
|
215
|
+
</span><span class='id identifier rubyid_describe'>describe</span> <span class='const'>OrdersController</span> <span class='kw'>do</span>
|
|
216
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>creates order</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
217
|
+
<span class='id identifier rubyid_post'>post</span> <span class='symbol'>:create</span><span class='comma'>,</span> <span class='label'>params:</span> <span class='id identifier rubyid_params'>params</span>
|
|
218
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_response'>response</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be_successful'>be_successful</span>
|
|
219
|
+
<span class='kw'>end</span>
|
|
220
|
+
<span class='kw'>end</span>
|
|
221
|
+
|
|
222
|
+
<span class='comment'># Add service test
|
|
223
|
+
</span><span class='id identifier rubyid_describe'>describe</span> <span class='const'>Orders</span><span class='op'>::</span><span class='const'>Create</span><span class='op'>::</span><span class='const'>Service</span> <span class='kw'>do</span>
|
|
224
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>creates order</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
225
|
+
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='id identifier rubyid_described_class'>described_class</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span><span class='rparen'>)</span>
|
|
226
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_success?'>success?</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be'>be</span> <span class='kw'>true</span>
|
|
227
|
+
<span class='kw'>end</span>
|
|
228
|
+
<span class='kw'>end</span>
|
|
229
|
+
</code></pre>
|
|
230
|
+
|
|
231
|
+
<p>Remove legacy tests after service tests prove comprehensive.</p>
|
|
232
|
+
</div></div>
|
|
233
|
+
|
|
234
|
+
<div id="footer">
|
|
235
|
+
Generated on Fri Nov 21 00:33:23 2025 by
|
|
236
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
237
|
+
0.9.37 (ruby-3.4.4).
|
|
238
|
+
</div>
|
|
239
|
+
|
|
240
|
+
</div>
|
|
241
|
+
</body>
|
|
242
|
+
</html>
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>
|
|
7
|
+
File: Integration / 2. Testing
|
|
8
|
+
|
|
9
|
+
— Servus | Service Object Framework
|
|
10
|
+
|
|
11
|
+
</title>
|
|
12
|
+
|
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
|
14
|
+
|
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
|
16
|
+
|
|
17
|
+
<script type="text/javascript">
|
|
18
|
+
pathId = "2_testing";
|
|
19
|
+
relpath = '';
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
24
|
+
|
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<div class="nav_wrap">
|
|
31
|
+
<iframe id="nav" src="file_list.html?1"></iframe>
|
|
32
|
+
<div id="resizer"></div>
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
<div id="main" tabindex="-1">
|
|
36
|
+
<div id="header">
|
|
37
|
+
<div id="menu">
|
|
38
|
+
|
|
39
|
+
<a href="_index.html">Index</a> »
|
|
40
|
+
<span class="title">File: Integration / 2. Testing</span>
|
|
41
|
+
|
|
42
|
+
</div>
|
|
43
|
+
|
|
44
|
+
<div id="search">
|
|
45
|
+
|
|
46
|
+
<a class="full_list_link" id="class_list_link"
|
|
47
|
+
href="class_list.html">
|
|
48
|
+
|
|
49
|
+
<svg width="24" height="24">
|
|
50
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
|
51
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
|
52
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
|
53
|
+
</svg>
|
|
54
|
+
</a>
|
|
55
|
+
|
|
56
|
+
</div>
|
|
57
|
+
<div class="clear"></div>
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<div id="content"><div id='filecontents'><h1 id="testing">Testing</h1>
|
|
61
|
+
|
|
62
|
+
<p>Services are designed for easy testing with explicit inputs (arguments) and outputs (Response objects). No special test infrastructure needed.</p>
|
|
63
|
+
|
|
64
|
+
<h2 id="schema-example-helpers">Schema Example Helpers</h2>
|
|
65
|
+
|
|
66
|
+
<p>Servus provides test helpers that extract <code>example</code> values from your JSON schemas, making it easy to generate test fixtures without maintaining separate factories.</p>
|
|
67
|
+
|
|
68
|
+
<h3 id="setup">Setup</h3>
|
|
69
|
+
|
|
70
|
+
<p>Include the helpers in your test suite:</p>
|
|
71
|
+
|
|
72
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># spec/spec_helper.rb
|
|
73
|
+
</span><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>servus/testing</span><span class='tstring_end'>'</span></span>
|
|
74
|
+
|
|
75
|
+
<span class='const'>RSpec</span><span class='period'>.</span><span class='id identifier rubyid_configure'>configure</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_config'>config</span><span class='op'>|</span>
|
|
76
|
+
<span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Testing.html" title="Servus::Testing (module)">Testing</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Testing/ExampleBuilders.html" title="Servus::Testing::ExampleBuilders (module)">ExampleBuilders</a></span></span>
|
|
77
|
+
<span class='kw'>end</span>
|
|
78
|
+
</code></pre>
|
|
79
|
+
|
|
80
|
+
<h3 id="using-schema-examples">Using Schema Examples</h3>
|
|
81
|
+
|
|
82
|
+
<p>Add <code>example</code> or <code>examples</code> keywords to your schemas:</p>
|
|
83
|
+
|
|
84
|
+
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Base.html" title="Servus::Base (class)">Base</a></span></span>
|
|
85
|
+
<span class='id identifier rubyid_schema'>schema</span><span class='lparen'>(</span>
|
|
86
|
+
<span class='label'>arguments:</span> <span class='lbrace'>{</span>
|
|
87
|
+
<span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>object</span><span class='tstring_end'>"</span></span><span class='comma'>,</span>
|
|
88
|
+
<span class='label'>properties:</span> <span class='lbrace'>{</span>
|
|
89
|
+
<span class='label'>user_id:</span> <span class='lbrace'>{</span> <span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>integer</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>example:</span> <span class='int'>123</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
|
90
|
+
<span class='label'>amount:</span> <span class='lbrace'>{</span> <span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>number</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>example:</span> <span class='float'>100.0</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
|
91
|
+
<span class='label'>currency:</span> <span class='lbrace'>{</span> <span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>string</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>examples:</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>USD</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>EUR</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>GBP</span><span class='tstring_end'>"</span></span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
|
|
92
|
+
<span class='rbrace'>}</span>
|
|
93
|
+
<span class='rbrace'>}</span><span class='comma'>,</span>
|
|
94
|
+
<span class='label'>result:</span> <span class='lbrace'>{</span>
|
|
95
|
+
<span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>object</span><span class='tstring_end'>"</span></span><span class='comma'>,</span>
|
|
96
|
+
<span class='label'>properties:</span> <span class='lbrace'>{</span>
|
|
97
|
+
<span class='label'>transaction_id:</span> <span class='lbrace'>{</span> <span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>string</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>example:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>txn_abc123</span><span class='tstring_end'>"</span></span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
|
98
|
+
<span class='label'>status:</span> <span class='lbrace'>{</span> <span class='label'>type:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>string</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='label'>example:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>approved</span><span class='tstring_end'>"</span></span> <span class='rbrace'>}</span>
|
|
99
|
+
<span class='rbrace'>}</span>
|
|
100
|
+
<span class='rbrace'>}</span>
|
|
101
|
+
<span class='rparen'>)</span>
|
|
102
|
+
<span class='kw'>end</span>
|
|
103
|
+
</code></pre>
|
|
104
|
+
|
|
105
|
+
<p>Then use the helpers in your tests:</p>
|
|
106
|
+
|
|
107
|
+
<pre class="code ruby"><code class="ruby"><span class='const'>RSpec</span><span class='period'>.</span><span class='id identifier rubyid_describe'>describe</span> <span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span> <span class='kw'>do</span>
|
|
108
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>processes payment successfully</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
109
|
+
<span class='comment'># Extract examples from schema and override specific values
|
|
110
|
+
</span> <span class='id identifier rubyid_args'>args</span> <span class='op'>=</span> <span class='id identifier rubyid_servus_arguments_example'>servus_arguments_example</span><span class='lparen'>(</span><span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span><span class='comma'>,</span> <span class='label'>amount:</span> <span class='float'>50.0</span><span class='rparen'>)</span>
|
|
111
|
+
<span class='comment'># => { user_id: 123, amount: 50.0, currency: "USD" }
|
|
112
|
+
</span>
|
|
113
|
+
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='op'>**</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
|
|
114
|
+
|
|
115
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be_success'>be_success</span>
|
|
116
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_match_array'>match_array</span><span class='lparen'>(</span>
|
|
117
|
+
<span class='id identifier rubyid_servus_result_example'>servus_result_example</span><span class='lparen'>(</span><span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
|
|
118
|
+
<span class='rparen'>)</span>
|
|
119
|
+
<span class='kw'>end</span>
|
|
120
|
+
|
|
121
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>handles different currencies</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
122
|
+
<span class='qwords_beg'>%w[</span><span class='tstring_content'>USD</span><span class='words_sep'> </span><span class='tstring_content'>EUR</span><span class='words_sep'> </span><span class='tstring_content'>GBP</span><span class='tstring_end'>]</span></span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_currency'>currency</span><span class='op'>|</span>
|
|
123
|
+
<span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> <span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span>
|
|
124
|
+
<span class='op'>**</span><span class='id identifier rubyid_servus_arguments_example'>servus_arguments_example</span><span class='lparen'>(</span><span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span><span class='comma'>,</span> <span class='label'>currency:</span> <span class='id identifier rubyid_currency'>currency</span><span class='rparen'>)</span>
|
|
125
|
+
<span class='rparen'>)</span>
|
|
126
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be_success'>be_success</span>
|
|
127
|
+
<span class='kw'>end</span>
|
|
128
|
+
<span class='kw'>end</span>
|
|
129
|
+
<span class='kw'>end</span>
|
|
130
|
+
</code></pre>
|
|
131
|
+
|
|
132
|
+
<h3 id="deep-merging">Deep Merging</h3>
|
|
133
|
+
|
|
134
|
+
<p>Overrides are deep-merged with schema examples, allowing you to override nested values:</p>
|
|
135
|
+
|
|
136
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># Schema has nested structure
|
|
137
|
+
</span><span class='id identifier rubyid_args'>args</span> <span class='op'>=</span> <span class='id identifier rubyid_servus_arguments_example'>servus_arguments_example</span><span class='lparen'>(</span>
|
|
138
|
+
<span class='const'>CreateUser</span><span class='op'>::</span><span class='const'>Service</span><span class='comma'>,</span>
|
|
139
|
+
<span class='label'>user:</span> <span class='lbrace'>{</span> <span class='label'>profile:</span> <span class='lbrace'>{</span> <span class='label'>age:</span> <span class='int'>35</span> <span class='rbrace'>}</span> <span class='rbrace'>}</span>
|
|
140
|
+
<span class='rparen'>)</span>
|
|
141
|
+
<span class='comment'># => { user: { id: 1, profile: { name: 'Alice', age: 35 } } }
|
|
142
|
+
</span></code></pre>
|
|
143
|
+
|
|
144
|
+
<h3 id="available-helpers">Available Helpers</h3>
|
|
145
|
+
|
|
146
|
+
<ul>
|
|
147
|
+
<li><code>servus_arguments_example(ServiceClass, **overrides)</code> - Returns hash of argument examples</li>
|
|
148
|
+
<li><code>servus_result_example(ServiceClass, **overrides)</code> - Returns Response object with result examples</li>
|
|
149
|
+
</ul>
|
|
150
|
+
|
|
151
|
+
<h2 id="basic-testing-pattern">Basic Testing Pattern</h2>
|
|
152
|
+
|
|
153
|
+
<pre class="code ruby"><code class="ruby"><span class='const'>RSpec</span><span class='period'>.</span><span class='id identifier rubyid_describe'>describe</span> <span class='const'>ProcessPayment</span><span class='op'>::</span><span class='const'>Service</span> <span class='kw'>do</span>
|
|
154
|
+
<span class='id identifier rubyid_describe'>describe</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>.call</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
155
|
+
<span class='id identifier rubyid_let'>let</span><span class='lparen'>(</span><span class='symbol'>:user</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_create'>create</span><span class='lparen'>(</span><span class='symbol'>:user</span><span class='comma'>,</span> <span class='label'>balance:</span> <span class='int'>1000</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
|
|
156
|
+
|
|
157
|
+
<span class='id identifier rubyid_subject'>subject</span><span class='lparen'>(</span><span class='symbol'>:result</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_described_class'>described_class</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='label'>user_id:</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span><span class='comma'>,</span> <span class='label'>amount:</span> <span class='id identifier rubyid_amount'>amount</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
|
|
158
|
+
|
|
159
|
+
<span class='id identifier rubyid_context'>context</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>with sufficient balance</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
160
|
+
<span class='id identifier rubyid_let'>let</span><span class='lparen'>(</span><span class='symbol'>:amount</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='int'>50</span> <span class='rbrace'>}</span>
|
|
161
|
+
|
|
162
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>processes payment</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
163
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_success?'>success?</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be'>be</span> <span class='kw'>true</span>
|
|
164
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='symbol'>:new_balance</span><span class='rbracket'>]</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_eq'>eq</span><span class='lparen'>(</span><span class='int'>950</span><span class='rparen'>)</span>
|
|
165
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_reload'>reload</span><span class='period'>.</span><span class='id identifier rubyid_balance'>balance</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_eq'>eq</span><span class='lparen'>(</span><span class='int'>950</span><span class='rparen'>)</span>
|
|
166
|
+
<span class='kw'>end</span>
|
|
167
|
+
<span class='kw'>end</span>
|
|
168
|
+
|
|
169
|
+
<span class='id identifier rubyid_context'>context</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>with insufficient balance</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
170
|
+
<span class='id identifier rubyid_let'>let</span><span class='lparen'>(</span><span class='symbol'>:amount</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='int'>2000</span> <span class='rbrace'>}</span>
|
|
171
|
+
|
|
172
|
+
<span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>returns failure</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
173
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_success?'>success?</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be'>be</span> <span class='kw'>false</span>
|
|
174
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_error'>error</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_eq'>eq</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Insufficient funds</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
|
175
|
+
<span class='id identifier rubyid_expect'>expect</span><span class='lparen'>(</span><span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_error'>error</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_be_a'>be_a</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support.html" title="Servus::Support (module)">Support</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support/Errors.html" title="Servus::Support::Errors (module)">Errors</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support/Errors/ServiceError.html" title="Servus::Support::Errors::ServiceError (class)">ServiceError</a></span></span><span class='rparen'>)</span>
|
|
176
|
+
<span class='kw'>end</span>
|
|
177
|
+
<span class='kw'>end</span>
|
|
178
|
+
<span class='kw'>end</span>
|
|
179
|
+
<span class='kw'>end</span>
|
|
180
|
+
</code></pre>
|
|
181
|
+
|
|
182
|
+
<h2 id="testing-service-composition">Testing Service Composition</h2>
|
|
183
|
+
|
|
184
|
+
<p>When testing services that call other services, mock the child services:</p>
|
|
185
|
+
|
|
186
|
+
<pre class="code ruby"><code class="ruby">describe Users::CreateWithAccount::Service do
|
|
187
|
+
# Make local or global helpers to clean up tests
|
|
188
|
+
def servus_success_result(data)
|
|
189
|
+
Servus::Support::Response.new(success: true, data: data, error: nil)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it "calls both create services" do
|
|
193
|
+
# Mock child services
|
|
194
|
+
allow(Users::Create::Service).to receive(:call).and_return(servus_success_result{ user: user })
|
|
195
|
+
allow(Accounts::Create::Service).to receive(:call).and_return(servus_success_result{ account: account })
|
|
196
|
+
|
|
197
|
+
result = described_class.call(email: "test@example.com", plan: "premium")
|
|
198
|
+
|
|
199
|
+
expect(Users::Create::Service).to have_received(:call)
|
|
200
|
+
expect(Accounts::Create::Service).to have_received(:call)
|
|
201
|
+
|
|
202
|
+
expect(result.success?).to be true
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
</code></pre>
|
|
206
|
+
|
|
207
|
+
<h2 id="testing-schema-validation">Testing Schema Validation</h2>
|
|
208
|
+
|
|
209
|
+
<p>Don't test that valid arguments pass validation - that's testing the framework. Do test that your schema catches invalid inputs:</p>
|
|
210
|
+
|
|
211
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_it'>it</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>validates required fields</span><span class='tstring_end'>"</span></span> <span class='kw'>do</span>
|
|
212
|
+
<span class='id identifier rubyid_expect'>expect</span> <span class='lbrace'>{</span>
|
|
213
|
+
<span class='const'>Service</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='label'>invalid:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>params</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
|
214
|
+
<span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_to'>to</span> <span class='id identifier rubyid_raise_error'>raise_error</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="Servus.html" title="Servus (module)">Servus</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support.html" title="Servus::Support (module)">Support</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support/Errors.html" title="Servus::Support::Errors (module)">Errors</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Servus/Support/Errors/ValidationError.html" title="Servus::Support::Errors::ValidationError (class)">ValidationError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>required</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
|
|
215
|
+
<span class='kw'>end</span>
|
|
216
|
+
</code></pre>
|
|
217
|
+
</div></div>
|
|
218
|
+
|
|
219
|
+
<div id="footer">
|
|
220
|
+
Generated on Fri Nov 21 00:33:23 2025 by
|
|
221
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
222
|
+
0.9.37 (ruby-3.4.4).
|
|
223
|
+
</div>
|
|
224
|
+
|
|
225
|
+
</div>
|
|
226
|
+
</body>
|
|
227
|
+
</html>
|