Skip to content

Commit d1258fb

Browse files
100% documentation coverage.
1 parent cb0d873 commit d1258fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2302
-445
lines changed

context/getting-started.md

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,355 @@ Check out all the repositories in this organisation, including these notable exa
4747

4848
- [sus/test](https://github.com/socketry/sus/tree/main/test/sus)
4949
- [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+
```

context/index.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ files:
1111
title: Getting Started
1212
description: This guide explains how to use the `sus` gem to write tests for your
1313
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.

0 commit comments

Comments
 (0)