sus 0.33.1 → 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/{usage.md → getting-started.md} +58 -37
- data/context/index.yaml +22 -0
- data/context/mocking.md +100 -30
- data/context/{shared.md → shared-contexts.md} +29 -2
- data/lib/sus/assertions.rb +93 -20
- 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 +59 -2
- 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 +53 -15
- 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 +28 -5
- data/lib/sus/output/null.rb +18 -4
- 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 +85 -3
- data/lib/sus/registry.rb +20 -1
- data/lib/sus/respond_to.rb +30 -3
- 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 +12 -0
- data/releases.md +8 -0
- data.tar.gz.sig +0 -0
- metadata +6 -4
- metadata.gz.sig +0 -0
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
|
|
@@ -1,10 +1,54 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Getting Started
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This guide explains how to use the `sus` gem to write tests for your Ruby projects.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Add the gem to your project:
|
|
8
|
+
|
|
9
|
+
~~~ bash
|
|
10
|
+
$ bundle add sus
|
|
11
|
+
~~~
|
|
12
|
+
|
|
13
|
+
## Write Some Tests
|
|
14
|
+
|
|
15
|
+
Create a test file in your project `test/my_project/my_class.rb`:
|
|
16
|
+
|
|
17
|
+
~~~ ruby
|
|
18
|
+
describe MyProject::MyClass do
|
|
19
|
+
let(:instance) {subject.new}
|
|
20
|
+
|
|
21
|
+
it "instantiates an object" do
|
|
22
|
+
expect(instance).to be_a(Object)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
~~~
|
|
26
|
+
|
|
27
|
+
## Run Your Tests
|
|
28
|
+
|
|
29
|
+
Run your tests with the `sus` command:
|
|
30
|
+
|
|
31
|
+
~~~ bash
|
|
32
|
+
$ sus
|
|
33
|
+
1 passed out of 1 total (1 assertions)
|
|
34
|
+
🏁 Finished in 47.0µs; 21272.535 assertions per second.
|
|
35
|
+
🐇 No slow tests found! Well done!
|
|
36
|
+
~~~
|
|
37
|
+
|
|
38
|
+
You can also run your tests in parallel:
|
|
39
|
+
|
|
40
|
+
~~~ bash
|
|
41
|
+
$ sus-parallel
|
|
42
|
+
~~~
|
|
43
|
+
|
|
44
|
+
## More Examples
|
|
45
|
+
|
|
46
|
+
Check out all the repositories in this organisation, including these notable examples:
|
|
47
|
+
|
|
48
|
+
- [sus/test](https://github.com/socketry/sus/tree/main/test/sus)
|
|
49
|
+
- [async/test](https://github.com/socketry/async/tree/main/test)
|
|
50
|
+
|
|
51
|
+
## Project Structure
|
|
8
52
|
|
|
9
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:
|
|
10
54
|
|
|
@@ -33,7 +77,7 @@ Create `config/sus.rb`:
|
|
|
33
77
|
# frozen_string_literal: true
|
|
34
78
|
|
|
35
79
|
# Use the covered gem for test coverage reporting:
|
|
36
|
-
require
|
|
80
|
+
require "covered/sus"
|
|
37
81
|
include Covered::Sus
|
|
38
82
|
|
|
39
83
|
def before_tests(assertions, output: self.output)
|
|
@@ -55,21 +99,7 @@ end
|
|
|
55
99
|
|
|
56
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.
|
|
57
101
|
|
|
58
|
-
##
|
|
59
|
-
|
|
60
|
-
```ruby
|
|
61
|
-
# frozen_string_literal: true
|
|
62
|
-
|
|
63
|
-
describe MyThing do
|
|
64
|
-
let(:my_thing) {subject.new}
|
|
65
|
-
|
|
66
|
-
with "#my_method" do
|
|
67
|
-
it "does something" do
|
|
68
|
-
expect(my_thing.my_method).to be == 42
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
```
|
|
102
|
+
## Test Syntax
|
|
73
103
|
|
|
74
104
|
### `describe` - Test Groups
|
|
75
105
|
|
|
@@ -118,8 +148,8 @@ When testing methods, use `with` to specify the method being tested:
|
|
|
118
148
|
|
|
119
149
|
```ruby
|
|
120
150
|
with "#my_method" do
|
|
121
|
-
it "
|
|
122
|
-
expect(my_thing.
|
|
151
|
+
it "returns a value" do
|
|
152
|
+
expect(my_thing.my_method).to be == 42
|
|
123
153
|
end
|
|
124
154
|
end
|
|
125
155
|
|
|
@@ -197,7 +227,7 @@ Invoking `super()` calls any parent `around` block, allowing you to chain setup
|
|
|
197
227
|
|
|
198
228
|
```ruby
|
|
199
229
|
expect(value).to be == expected
|
|
200
|
-
|
|
230
|
+
expect(value).to be >= 10
|
|
201
231
|
expect(value).to be <= 100
|
|
202
232
|
expect(value).to be > 0
|
|
203
233
|
expect(value).to be < 1000
|
|
@@ -248,9 +278,9 @@ expect(hash).to have_keys(key1: be == 1, "key2" => be == 2)
|
|
|
248
278
|
|
|
249
279
|
```ruby
|
|
250
280
|
expect(user).to have_attributes(
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
281
|
+
name: be == "John",
|
|
282
|
+
age: be >= 18,
|
|
283
|
+
email: be(:include?, "@")
|
|
254
284
|
)
|
|
255
285
|
```
|
|
256
286
|
|
|
@@ -312,7 +342,7 @@ expect{slow_operation}.to have_duration(be < 2.0)
|
|
|
312
342
|
expect{fast_operation}.to have_duration(be < 0.1)
|
|
313
343
|
```
|
|
314
344
|
|
|
315
|
-
- For less
|
|
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.
|
|
316
346
|
|
|
317
347
|
- For benchmarking, you can use the `sus-fixtures-benchmark` gem which measures a block of code multiple times and reports the execution time.
|
|
318
348
|
|
|
@@ -358,7 +388,7 @@ This can be useful for debugging or providing context during test runs.
|
|
|
358
388
|
|
|
359
389
|
### Console Output
|
|
360
390
|
|
|
361
|
-
The `sus-fixtures-console` gem provides a way to
|
|
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.
|
|
362
392
|
|
|
363
393
|
## Running Tests
|
|
364
394
|
|
|
@@ -369,12 +399,3 @@ bundle exec sus
|
|
|
369
399
|
# Run specific test file
|
|
370
400
|
bundle exec sus test/specific_test.rb
|
|
371
401
|
```
|
|
372
|
-
|
|
373
|
-
## Best Practices
|
|
374
|
-
|
|
375
|
-
1. **Use real objects** instead of mocks when possible.
|
|
376
|
-
2. **Dependency injection** for testability.
|
|
377
|
-
3. **Isolatae mutable state** using temporary directories.
|
|
378
|
-
4. **Clear test descriptions** that explain the behavior.
|
|
379
|
-
5. **Group tests** with `describe` (classes) and `with` for better organization.
|
|
380
|
-
6. **Keep tests simple** and focused on one behavior.
|
data/context/index.yaml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Automatically generated context index for Utopia::Project guides.
|
|
2
|
+
# Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
|
|
3
|
+
---
|
|
4
|
+
description: A fast and scalable test runner.
|
|
5
|
+
metadata:
|
|
6
|
+
documentation_uri: https://socketry.github.io/sus/
|
|
7
|
+
funding_uri: https://github.com/sponsors/ioquatix/
|
|
8
|
+
source_code_uri: https://github.com/socketry/sus.git
|
|
9
|
+
files:
|
|
10
|
+
- path: getting-started.md
|
|
11
|
+
title: Getting Started
|
|
12
|
+
description: This guide explains how to use the `sus` gem to write tests for your
|
|
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
|