sus 0.34.0 → 0.35.0
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
- checksums.yaml.gz.sig +0 -0
- data/context/getting-started.md +352 -0
- data/context/index.yaml +9 -0
- data/context/mocking.md +100 -30
- data/context/{shared.md → shared-contexts.md} +29 -2
- data/lib/sus/assertions.rb +91 -18
- data/lib/sus/base.rb +13 -1
- data/lib/sus/be.rb +84 -0
- data/lib/sus/be_truthy.rb +16 -0
- data/lib/sus/be_within.rb +25 -0
- data/lib/sus/clock.rb +21 -0
- data/lib/sus/config.rb +58 -1
- data/lib/sus/context.rb +28 -5
- data/lib/sus/describe.rb +14 -0
- data/lib/sus/expect.rb +23 -0
- data/lib/sus/file.rb +38 -0
- data/lib/sus/filter.rb +21 -0
- data/lib/sus/fixtures/temporary_directory_context.rb +27 -0
- data/lib/sus/fixtures.rb +1 -0
- data/lib/sus/have/all.rb +8 -0
- data/lib/sus/have/any.rb +8 -0
- data/lib/sus/have.rb +42 -0
- data/lib/sus/have_duration.rb +16 -0
- data/lib/sus/identity.rb +44 -1
- data/lib/sus/integrations.rb +1 -0
- data/lib/sus/it.rb +33 -0
- data/lib/sus/it_behaves_like.rb +16 -0
- data/lib/sus/let.rb +3 -0
- data/lib/sus/mock.rb +39 -1
- data/lib/sus/output/backtrace.rb +31 -1
- data/lib/sus/output/bar.rb +17 -0
- data/lib/sus/output/buffered.rb +32 -1
- data/lib/sus/output/lines.rb +10 -0
- data/lib/sus/output/messages.rb +26 -3
- data/lib/sus/output/null.rb +16 -2
- data/lib/sus/output/progress.rb +29 -1
- data/lib/sus/output/status.rb +13 -0
- data/lib/sus/output/structured.rb +14 -1
- data/lib/sus/output/text.rb +33 -1
- data/lib/sus/output/xterm.rb +11 -1
- data/lib/sus/output.rb +9 -0
- data/lib/sus/raise_exception.rb +16 -0
- data/lib/sus/receive.rb +82 -0
- data/lib/sus/registry.rb +20 -1
- data/lib/sus/respond_to.rb +29 -2
- data/lib/sus/shared.rb +16 -0
- data/lib/sus/tree.rb +10 -0
- data/lib/sus/version.rb +2 -1
- data/lib/sus/with.rb +18 -0
- data/readme.md +8 -0
- data/releases.md +4 -0
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +0 -0
- data/context/usage.md +0 -380
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e68f73a268f03439168f0ce3daec5a802174d6ada57de7e03d8362d2102eb3b2
|
|
4
|
+
data.tar.gz: 273c4098c1a760fa81525302d38f985f2f07ad630c2ddced74db4c1fd67ce030
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c7f993c9daea1b833f4f34ec5e5c0cee6898fda975c0ea0850f907c45f325533b26ba6ddcda394125f33e03ea601cd4c7db8e42a09a38de3fee4ff4e7ba19b89
|
|
7
|
+
data.tar.gz: 7e8eb36690ba2e85686efa0b955755a5b587619803bb8f953b8b2c2ba11576733044d7b5255047d2ac3734ef9a5e84b86adf45c10a758bb2d22baf30661e3fa3
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/context/getting-started.md
CHANGED
|
@@ -47,3 +47,355 @@ Check out all the repositories in this organisation, including these notable exa
|
|
|
47
47
|
|
|
48
48
|
- [sus/test](https://github.com/socketry/sus/tree/main/test/sus)
|
|
49
49
|
- [async/test](https://github.com/socketry/async/tree/main/test)
|
|
50
|
+
|
|
51
|
+
## Project Structure
|
|
52
|
+
|
|
53
|
+
Here is an example structure for testing with Sus - the actual structure may vary based on your gem's organization, but aside from the `lib/` directory, sus expects the following structure:
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
my-gem/
|
|
57
|
+
├── config/
|
|
58
|
+
│ └── sus.rb # Sus configuration file
|
|
59
|
+
├── lib/
|
|
60
|
+
│ ├── my_gem.rb
|
|
61
|
+
│ └── my_gem/
|
|
62
|
+
│ └── my_thing.rb
|
|
63
|
+
├── fixtures/
|
|
64
|
+
│ └── my_gem/
|
|
65
|
+
│ └── a_thing.rb # Provides MyGem::AThing shared context
|
|
66
|
+
└── test/
|
|
67
|
+
├── my_gem.rb # Tests MyGem
|
|
68
|
+
└── my_gem/
|
|
69
|
+
└── my_thing.rb # Tests MyGem::MyThing
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Configuration File
|
|
73
|
+
|
|
74
|
+
Create `config/sus.rb`:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
# frozen_string_literal: true
|
|
78
|
+
|
|
79
|
+
# Use the covered gem for test coverage reporting:
|
|
80
|
+
require "covered/sus"
|
|
81
|
+
include Covered::Sus
|
|
82
|
+
|
|
83
|
+
def before_tests(assertions, output: self.output)
|
|
84
|
+
# Starts the clock and sets up the test environment:
|
|
85
|
+
super
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def after_tests(assertions, output: self.output)
|
|
89
|
+
# Stops the clock and prints the test results:
|
|
90
|
+
super
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Fixtures Files
|
|
95
|
+
|
|
96
|
+
`fixtures/` gets added to the `$LOAD_PATH` automatically, so you can require files from there without needing to specify the full path.
|
|
97
|
+
|
|
98
|
+
### Test Files
|
|
99
|
+
|
|
100
|
+
Sus runs all Ruby files in the `test/` directory by default. But you can also create tests in any file, and run them with the `sus my_tests.rb` command.
|
|
101
|
+
|
|
102
|
+
## Test Syntax
|
|
103
|
+
|
|
104
|
+
### `describe` - Test Groups
|
|
105
|
+
|
|
106
|
+
Use `describe` to group related tests:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
describe MyThing do
|
|
110
|
+
# The subject will be whatever is described:
|
|
111
|
+
let(:my_thing) {subject.new}
|
|
112
|
+
end
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### `it` - Individual Tests
|
|
116
|
+
|
|
117
|
+
Use `it` to define individual test cases:
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
it "returns the expected value" do
|
|
121
|
+
expect(result).to be == "expected"
|
|
122
|
+
end
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
You can use `it` blocks at the top level or within `describe` or `with` blocks.
|
|
126
|
+
|
|
127
|
+
### `with` - Context Blocks
|
|
128
|
+
|
|
129
|
+
Use `with` to create context-specific test groups:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
with "valid input" do
|
|
133
|
+
let(:input) {"valid input"}
|
|
134
|
+
it "succeeds" do
|
|
135
|
+
expect{my_thing.process(input)}.not.to raise_exception
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Non-lazy state can be provided as keyword arguments:
|
|
140
|
+
with "invalid input", input: nil do
|
|
141
|
+
it "raises an error" do
|
|
142
|
+
expect{my_thing.process(input)}.to raise_exception(ArgumentError)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
When testing methods, use `with` to specify the method being tested:
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
with "#my_method" do
|
|
151
|
+
it "returns a value" do
|
|
152
|
+
expect(my_thing.my_method).to be == 42
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
with ".my_class_method" do
|
|
157
|
+
it "returns a value" do
|
|
158
|
+
expect(MyThing.class_method).to be == "class value"
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### `let` - Lazy Variables
|
|
164
|
+
|
|
165
|
+
Use `let` to define variables that are evaluated when first accessed:
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
let(:helper) {subject.new}
|
|
169
|
+
let(:test_data) {"test value"}
|
|
170
|
+
|
|
171
|
+
it "uses the helper" do
|
|
172
|
+
expect(helper.process(test_data)).to be_truthy
|
|
173
|
+
end
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### `before` and `after` - Setup/Teardown
|
|
177
|
+
|
|
178
|
+
Use `before` and `after` for setup and teardown logic:
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
before do
|
|
182
|
+
# Setup logic.
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
after do
|
|
186
|
+
# Cleanup logic.
|
|
187
|
+
end
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Error handling in `after` allows you to perform cleanup even if the test fails with an exception (not a test failure).
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
after do |error = nil|
|
|
194
|
+
if error
|
|
195
|
+
# The state of the test is unknown, so you may want to forcefully kill processes or clean up resources.
|
|
196
|
+
Process.kill(:KILL, @child_pid)
|
|
197
|
+
else
|
|
198
|
+
# Normal cleanup logic.
|
|
199
|
+
Process.kill(:TERM, @child_pid)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
Process.waitpid(@child_pid)
|
|
203
|
+
end
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### `around` - Setup/Teardown
|
|
207
|
+
|
|
208
|
+
Use `around` for setup and teardown logic:
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
around do |&block|
|
|
212
|
+
# Setup logic.
|
|
213
|
+
super() do
|
|
214
|
+
# Run the test.
|
|
215
|
+
block.call
|
|
216
|
+
end
|
|
217
|
+
ensure
|
|
218
|
+
# Cleanup logic.
|
|
219
|
+
end
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Invoking `super()` calls any parent `around` block, allowing you to chain setup and teardown logic.
|
|
223
|
+
|
|
224
|
+
## Assertions
|
|
225
|
+
|
|
226
|
+
### Basic Assertions
|
|
227
|
+
|
|
228
|
+
```ruby
|
|
229
|
+
expect(value).to be == expected
|
|
230
|
+
expect(value).to be >= 10
|
|
231
|
+
expect(value).to be <= 100
|
|
232
|
+
expect(value).to be > 0
|
|
233
|
+
expect(value).to be < 1000
|
|
234
|
+
expect(value).to be_truthy
|
|
235
|
+
expect(value).to be_falsey
|
|
236
|
+
expect(value).to be_nil
|
|
237
|
+
expect(value).to be_equal(another_value)
|
|
238
|
+
expect(value).to be_a(Class)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Strings
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
expect(string).to be(:start_with?, "prefix")
|
|
245
|
+
expect(string).to be(:end_with?, "suffix")
|
|
246
|
+
expect(string).to be(:match?, /pattern/)
|
|
247
|
+
expect(string).to be(:include?, "substring")
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Ranges and Tolerance
|
|
251
|
+
|
|
252
|
+
```ruby
|
|
253
|
+
expect(value).to be_within(0.1).of(5.0)
|
|
254
|
+
expect(value).to be_within(5).percent_of(100)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Method Calls
|
|
258
|
+
|
|
259
|
+
To call methods on the expected object:
|
|
260
|
+
|
|
261
|
+
```ruby
|
|
262
|
+
expect(array).to be(:include?, "value")
|
|
263
|
+
expect(string).to be(:start_with?, "prefix")
|
|
264
|
+
expect(object).to be(:respond_to?, :method_name)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Collection Assertions
|
|
268
|
+
|
|
269
|
+
```ruby
|
|
270
|
+
expect(array).to have_attributes(length: be == 1)
|
|
271
|
+
expect(array).to have_value(be > 1)
|
|
272
|
+
|
|
273
|
+
expect(hash).to have_keys(:key1, "key2")
|
|
274
|
+
expect(hash).to have_keys(key1: be == 1, "key2" => be == 2)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Attribute Testing
|
|
278
|
+
|
|
279
|
+
```ruby
|
|
280
|
+
expect(user).to have_attributes(
|
|
281
|
+
name: be == "John",
|
|
282
|
+
age: be >= 18,
|
|
283
|
+
email: be(:include?, "@")
|
|
284
|
+
)
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Exception Assertions
|
|
288
|
+
|
|
289
|
+
```ruby
|
|
290
|
+
expect do
|
|
291
|
+
risky_operation
|
|
292
|
+
end.to raise_exception(RuntimeError, message: be =~ /expected error message/)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Combining Predicates
|
|
296
|
+
|
|
297
|
+
Predicates can be nested.
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
300
|
+
expect(user).to have_attributes(
|
|
301
|
+
name: have_attributes(
|
|
302
|
+
first: be == "John",
|
|
303
|
+
last: be == "Doe"
|
|
304
|
+
),
|
|
305
|
+
comments: have_value(be =~ /test comment/),
|
|
306
|
+
created_at: be_within(1.minute).of(Time.now)
|
|
307
|
+
)
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Logical Combinations
|
|
311
|
+
|
|
312
|
+
```ruby
|
|
313
|
+
expect(value).to (be > 10).and(be < 20)
|
|
314
|
+
expect(value).to be_a(String).or(be_a(Symbol), be_a(Integer))
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Custom Predicates
|
|
318
|
+
|
|
319
|
+
You can create custom predicates for more complex assertions:
|
|
320
|
+
|
|
321
|
+
```ruby
|
|
322
|
+
def be_small_prime
|
|
323
|
+
(be == 2).or(be == 3, be == 5, be == 7)
|
|
324
|
+
end
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Block Expectations
|
|
328
|
+
|
|
329
|
+
### Testing Blocks
|
|
330
|
+
|
|
331
|
+
```ruby
|
|
332
|
+
expect{operation}.to raise_exception(Error)
|
|
333
|
+
expect{operation}.to have_duration(be < 1.0)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Performance Testing
|
|
337
|
+
|
|
338
|
+
You should generally avoid testing performance in unit tests, as it will be highly unstable and dependent on the environment. However, if you need to test performance, you can use:
|
|
339
|
+
|
|
340
|
+
```ruby
|
|
341
|
+
expect{slow_operation}.to have_duration(be < 2.0)
|
|
342
|
+
expect{fast_operation}.to have_duration(be < 0.1)
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
- For less unstable performance tests, you can use the `sus-fixtures-time` gem which tries to compensate for the environment by measuring execution time.
|
|
346
|
+
|
|
347
|
+
- For benchmarking, you can use the `sus-fixtures-benchmark` gem which measures a block of code multiple times and reports the execution time.
|
|
348
|
+
|
|
349
|
+
## File Operations
|
|
350
|
+
|
|
351
|
+
### Temporary Directories
|
|
352
|
+
|
|
353
|
+
Use `Dir.mktmpdir` for isolated test environments:
|
|
354
|
+
|
|
355
|
+
```ruby
|
|
356
|
+
around do |block|
|
|
357
|
+
Dir.mktmpdir do |root|
|
|
358
|
+
@root = root
|
|
359
|
+
block.call
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
let(:test_path) {File.join(@root, "test.txt")}
|
|
364
|
+
|
|
365
|
+
it "can create a file" do
|
|
366
|
+
File.write(test_path, "content")
|
|
367
|
+
expect(File).to be(:exist?, test_path)
|
|
368
|
+
end
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## Test Output
|
|
372
|
+
|
|
373
|
+
In general, tests should not produce output unless there is an error or failure.
|
|
374
|
+
|
|
375
|
+
### Informational Output
|
|
376
|
+
|
|
377
|
+
You can use `inform` to print informational messages during tests:
|
|
378
|
+
|
|
379
|
+
```ruby
|
|
380
|
+
it "logs an informational message" do
|
|
381
|
+
rate = copy_data(source, destination)
|
|
382
|
+
inform "Copied data at #{rate}MB/s"
|
|
383
|
+
expect(rate).to be > 0
|
|
384
|
+
end
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
This can be useful for debugging or providing context during test runs.
|
|
388
|
+
|
|
389
|
+
### Console Output
|
|
390
|
+
|
|
391
|
+
The `sus-fixtures-console` gem provides a way to suppress and capture console output during tests. If you are using code which generates console output, you can use this gem to capture it and assert on it.
|
|
392
|
+
|
|
393
|
+
## Running Tests
|
|
394
|
+
|
|
395
|
+
```bash
|
|
396
|
+
# Run all tests
|
|
397
|
+
bundle exec sus
|
|
398
|
+
|
|
399
|
+
# Run specific test file
|
|
400
|
+
bundle exec sus test/specific_test.rb
|
|
401
|
+
```
|
data/context/index.yaml
CHANGED
|
@@ -11,3 +11,12 @@ files:
|
|
|
11
11
|
title: Getting Started
|
|
12
12
|
description: This guide explains how to use the `sus` gem to write tests for your
|
|
13
13
|
Ruby projects.
|
|
14
|
+
- path: mocking.md
|
|
15
|
+
title: Mocking
|
|
16
|
+
description: This guide explains how to use mocking in sus to isolate dependencies
|
|
17
|
+
and verify interactions in your tests.
|
|
18
|
+
- path: shared-contexts.md
|
|
19
|
+
title: Shared Test Behaviors and Fixtures
|
|
20
|
+
description: This guide explains how to use shared test contexts and fixtures in
|
|
21
|
+
sus to reduce duplication and ensure consistent test behavior across your test
|
|
22
|
+
suite.
|
data/context/mocking.md
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
# Mocking
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This guide explains how to use mocking in sus to isolate dependencies and verify interactions in your tests.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
When testing code that depends on external services, slow operations, or complex objects, you need a way to control those dependencies without actually invoking them. Mocking allows you to replace method implementations or set expectations on method calls, making your tests faster, more reliable, and easier to maintain.
|
|
8
|
+
|
|
9
|
+
Use mocking when you need:
|
|
10
|
+
- **Isolation**: Test your code without depending on external services (databases, APIs, file systems)
|
|
11
|
+
- **Performance**: Avoid slow operations during testing
|
|
12
|
+
- **Control**: Simulate error conditions or edge cases that are hard to reproduce
|
|
13
|
+
- **Verification**: Ensure your code calls methods with the correct arguments
|
|
14
|
+
|
|
15
|
+
Sus provides two types of mocking: `receive` for method call expectations and `mock` for replacing method implementations. The `receive` matcher is a subset of full mocking and is used to set expectations on method calls, while `mock` can be used to replace method implementations or set up more complex behavior.
|
|
16
|
+
|
|
17
|
+
**Important**: Mocking non-local objects permanently changes the object's ancestors, so it should be used with care. For local objects, you can use `let` to define the object and then mock it.
|
|
6
18
|
|
|
7
19
|
Sus does not support the concept of test doubles, but you can use `receive` and `mock` to achieve similar functionality.
|
|
8
20
|
|
|
@@ -10,86 +22,144 @@ Sus does not support the concept of test doubles, but you can use `receive` and
|
|
|
10
22
|
|
|
11
23
|
The `receive(:method)` expectation is used to set up an expectation that a method will be called on an object. You can also specify arguments and return values. However, `receive` is not sequenced, meaning it does not enforce the order of method calls. If you need to enforce the order, use `mock` instead.
|
|
12
24
|
|
|
25
|
+
### Basic Usage
|
|
26
|
+
|
|
27
|
+
Verify that a method is called:
|
|
28
|
+
|
|
13
29
|
```ruby
|
|
14
|
-
describe
|
|
15
|
-
let(:
|
|
30
|
+
describe PaymentProcessor do
|
|
31
|
+
let(:payment_processor) {subject.new}
|
|
32
|
+
let(:logger) {Object.new}
|
|
16
33
|
|
|
17
|
-
it "
|
|
18
|
-
expect(
|
|
34
|
+
it "logs payment attempts" do
|
|
35
|
+
expect(logger).to receive(:info)
|
|
19
36
|
|
|
20
|
-
|
|
37
|
+
payment_processor.process_payment(amount: 100, logger: logger)
|
|
21
38
|
end
|
|
22
39
|
end
|
|
23
40
|
```
|
|
24
41
|
|
|
25
42
|
### With Arguments
|
|
26
43
|
|
|
44
|
+
Verify method calls with specific arguments:
|
|
45
|
+
|
|
27
46
|
```ruby
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
# or .with_options(be == {option1: value1, option2: value2})
|
|
32
|
-
# or .with_block
|
|
47
|
+
describe EmailService do
|
|
48
|
+
let(:email_service) {subject.new}
|
|
49
|
+
let(:smtp_client) {Object.new}
|
|
33
50
|
|
|
34
|
-
|
|
51
|
+
it "sends emails with correct recipient and subject" do
|
|
52
|
+
expect(smtp_client).to receive(:send).with("user@example.com", "Welcome!")
|
|
53
|
+
|
|
54
|
+
email_service.send_welcome_email("user@example.com", smtp_client)
|
|
55
|
+
end
|
|
35
56
|
end
|
|
36
57
|
```
|
|
37
58
|
|
|
59
|
+
You can also use more flexible argument matching:
|
|
60
|
+
- `.with_arguments(be == [arg1, arg2])` for positional arguments
|
|
61
|
+
- `.with_options(be == {option1: value1})` for keyword arguments
|
|
62
|
+
- `.with_block` to verify a block is passed
|
|
63
|
+
|
|
38
64
|
### Returning Values
|
|
39
65
|
|
|
66
|
+
Set up return values for mocked methods:
|
|
67
|
+
|
|
40
68
|
```ruby
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
69
|
+
describe UserRepository do
|
|
70
|
+
let(:repository) {subject.new}
|
|
71
|
+
let(:database) {Object.new}
|
|
72
|
+
|
|
73
|
+
it "retrieves user by ID" do
|
|
74
|
+
expected_user = {id: 1, name: "Alice"}
|
|
75
|
+
expect(database).to receive(:find_user).with(1).and_return(expected_user)
|
|
76
|
+
|
|
77
|
+
user = repository.find(1, database)
|
|
78
|
+
expect(user).to be == expected_user
|
|
79
|
+
end
|
|
45
80
|
end
|
|
46
81
|
```
|
|
47
82
|
|
|
48
83
|
### Raising Exceptions
|
|
49
84
|
|
|
85
|
+
Simulate error conditions:
|
|
86
|
+
|
|
50
87
|
```ruby
|
|
51
|
-
|
|
52
|
-
|
|
88
|
+
describe FileUploader do
|
|
89
|
+
let(:uploader) {subject.new}
|
|
90
|
+
let(:storage_service) {Object.new}
|
|
53
91
|
|
|
54
|
-
|
|
92
|
+
it "handles storage failures gracefully" do
|
|
93
|
+
expect(storage_service).to receive(:upload).and_raise(StandardError, "Storage unavailable")
|
|
94
|
+
|
|
95
|
+
expect{uploader.upload_file("data.txt", storage_service)}.to raise_exception(StandardError, message: "Storage unavailable")
|
|
96
|
+
end
|
|
55
97
|
end
|
|
56
98
|
```
|
|
57
99
|
|
|
58
100
|
### Multiple Calls
|
|
59
101
|
|
|
102
|
+
Verify methods are called multiple times:
|
|
103
|
+
|
|
60
104
|
```ruby
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
105
|
+
describe CacheWarmer do
|
|
106
|
+
let(:warmer) {subject.new}
|
|
107
|
+
let(:cache) {Object.new}
|
|
108
|
+
|
|
109
|
+
it "warms multiple cache entries" do
|
|
110
|
+
expect(cache).to receive(:set).twice.and_return(true)
|
|
111
|
+
|
|
112
|
+
warmer.warm(["key1", "key2"], cache)
|
|
113
|
+
end
|
|
66
114
|
end
|
|
67
115
|
```
|
|
68
116
|
|
|
117
|
+
You can also use `.with_call_count(be == 2)` for more flexible call count expectations.
|
|
118
|
+
|
|
69
119
|
## Mock Objects
|
|
70
120
|
|
|
71
121
|
Mock objects are used to replace method implementations or set up complex behavior. They can be used to intercept method calls, modify arguments, and control the flow of execution. They are thread-local, meaning they only affect the current thread, therefore are not suitable for use in tests that have multiple threads.
|
|
72
122
|
|
|
123
|
+
### Replacing Method Implementations
|
|
124
|
+
|
|
125
|
+
Replace methods to return controlled values:
|
|
126
|
+
|
|
73
127
|
```ruby
|
|
74
128
|
describe ApiClient do
|
|
75
129
|
let(:http_client) {Object.new}
|
|
76
130
|
let(:client) {ApiClient.new(http_client)}
|
|
77
131
|
let(:users) {["Alice", "Bob"]}
|
|
78
132
|
|
|
79
|
-
it "
|
|
133
|
+
it "fetches users from API" do
|
|
80
134
|
mock(http_client) do |mock|
|
|
81
135
|
mock.replace(:get) do |url, headers: {}|
|
|
82
136
|
expect(url).to be == "/api/users"
|
|
83
137
|
expect(headers).to be == {"accept" => "application/json"}
|
|
84
138
|
users.to_json
|
|
85
139
|
end
|
|
86
|
-
|
|
87
|
-
# or mock.before {|...| ...}
|
|
88
|
-
# or mock.after {|...| ...}
|
|
89
|
-
# or mock.wrap(:new) {|original, ...| original.call(...)}
|
|
90
140
|
end
|
|
91
141
|
|
|
92
142
|
expect(client.fetch_users).to be == users
|
|
93
143
|
end
|
|
94
144
|
end
|
|
95
145
|
```
|
|
146
|
+
|
|
147
|
+
### Advanced Mocking Patterns
|
|
148
|
+
|
|
149
|
+
You can also use:
|
|
150
|
+
- `mock.before {|...| ...}` to execute code before the original method
|
|
151
|
+
- `mock.after {|...| ...}` to execute code after the original method
|
|
152
|
+
- `mock.wrap(:method) {|original, ...| original.call(...)}` to wrap the original method
|
|
153
|
+
|
|
154
|
+
## Best Practices
|
|
155
|
+
|
|
156
|
+
1. **Prefer real objects**: Use mocks only when necessary (external services, slow operations, error conditions)
|
|
157
|
+
2. **Use dependency injection**: Make dependencies explicit so they can be easily mocked
|
|
158
|
+
3. **Mock at boundaries**: Mock external services, not internal implementation details
|
|
159
|
+
4. **Keep mocks simple**: Complex mock setups indicate the code might need refactoring
|
|
160
|
+
|
|
161
|
+
## Common Pitfalls
|
|
162
|
+
|
|
163
|
+
1. **Over-mocking**: Mocking too much makes tests brittle and less valuable
|
|
164
|
+
2. **Thread safety**: Mock objects are thread-local, don't use them in multi-threaded tests
|
|
165
|
+
3. **Permanent changes**: Mocking non-local objects permanently changes their ancestors - use `let` for local objects instead
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
# Shared Test Behaviors and Fixtures
|
|
2
2
|
|
|
3
|
+
This guide explains how to use shared test contexts and fixtures in sus to reduce duplication and ensure consistent test behavior across your test suite.
|
|
4
|
+
|
|
3
5
|
## Overview
|
|
4
6
|
|
|
7
|
+
When you have common test behaviors that need to be applied to multiple test files or multiple implementations of the same interface, shared contexts allow you to define those behaviors once and reuse them. This reduces duplication, ensures consistency, and makes it easier to maintain your tests.
|
|
8
|
+
|
|
9
|
+
Use shared contexts when you need:
|
|
10
|
+
- **Code reuse**: Apply the same test behavior to multiple classes or modules
|
|
11
|
+
- **Consistency**: Ensure all implementations of an interface are tested the same way
|
|
12
|
+
- **Maintainability**: Update test behavior in one place rather than many
|
|
13
|
+
- **Parameterization**: Run the same tests with different inputs or configurations
|
|
14
|
+
|
|
5
15
|
Sus provides shared test contexts which can be used to define common behaviours or tests that can be reused across one or more test files.
|
|
6
16
|
|
|
7
17
|
When you have common test behaviors that you want to apply to multiple test files, add them to the `fixtures/` directory. When you have common test behaviors that you want to apply to multiple implementations of the same interface, within a single test file, you can define them as shared contexts within that file.
|
|
@@ -10,6 +20,8 @@ When you have common test behaviors that you want to apply to multiple test file
|
|
|
10
20
|
|
|
11
21
|
### Directory Structure
|
|
12
22
|
|
|
23
|
+
Shared fixtures are stored in the `fixtures/` directory, which mirrors your project structure:
|
|
24
|
+
|
|
13
25
|
```
|
|
14
26
|
my-gem/
|
|
15
27
|
├── lib/
|
|
@@ -25,6 +37,8 @@ my-gem/
|
|
|
25
37
|
└── my_thing.rb
|
|
26
38
|
```
|
|
27
39
|
|
|
40
|
+
The `fixtures/` directory is automatically added to the `$LOAD_PATH`, so you can require files from there without needing to specify the full path.
|
|
41
|
+
|
|
28
42
|
### Creating Shared Fixtures
|
|
29
43
|
|
|
30
44
|
Create shared behaviors in the `fixtures/` directory using `Sus::Shared`:
|
|
@@ -65,7 +79,7 @@ Require and use shared fixtures in your test files:
|
|
|
65
79
|
|
|
66
80
|
```ruby
|
|
67
81
|
# test/my_gem/user_manager.rb
|
|
68
|
-
require
|
|
82
|
+
require "my_gem/a_user"
|
|
69
83
|
|
|
70
84
|
describe MyGem::UserManager do
|
|
71
85
|
it_behaves_like MyGem::AUser, "manager"
|
|
@@ -108,7 +122,7 @@ Use specific shared fixtures:
|
|
|
108
122
|
|
|
109
123
|
```ruby
|
|
110
124
|
# test/my_gem/authorization.rb
|
|
111
|
-
require
|
|
125
|
+
require "my_gem/users"
|
|
112
126
|
|
|
113
127
|
describe MyGem::Authorization do
|
|
114
128
|
with "standard user" do
|
|
@@ -183,3 +197,16 @@ end
|
|
|
183
197
|
```
|
|
184
198
|
|
|
185
199
|
Note the use of `unique: adapter.name` to ensure each test is uniquely identified, which is useful for reporting and debugging - otherwise the same test line number would be used for all iterations, which can make it hard to identify which specific test failed.
|
|
200
|
+
|
|
201
|
+
## Best Practices
|
|
202
|
+
|
|
203
|
+
1. **Organize by domain**: Group related shared contexts together in modules
|
|
204
|
+
2. **Keep contexts focused**: Each shared context should test one cohesive behavior
|
|
205
|
+
3. **Use parameters**: Make shared contexts flexible by accepting parameters
|
|
206
|
+
4. **Document intent**: Use clear names that explain what behavior is being tested
|
|
207
|
+
|
|
208
|
+
## Common Pitfalls
|
|
209
|
+
|
|
210
|
+
1. **Over-sharing**: Don't create shared contexts for behaviors that are only used once
|
|
211
|
+
2. **Tight coupling**: Avoid shared contexts that depend on too many specific implementation details
|
|
212
|
+
3. **Unclear names**: Use descriptive names that make it obvious what behavior is being tested
|