Concept Flow - RSpec expectations and matchers
Start Test
Set expectation
Run code under test
Compare actual vs expected
Continue
End Test
The test sets an expectation, runs code, compares actual and expected results, then passes or fails.
expect(5 + 3).to eq(8) expect('ruby').to include('rub') expect([1,2,3]).not_to be_empty
| Step | Expectation | Actual Value | Matcher | Result | Pass/Fail |
|---|---|---|---|---|---|
| 1 | expect(5 + 3).to eq(8) | 8 | eq(8) | 8 == 8 | Pass |
| 2 | expect('ruby').to include('rub') | 'ruby' | include('rub') | 'ruby' includes 'rub' | Pass |
| 3 | expect([1,2,3]).not_to be_empty | [1,2,3] | not_to be_empty | Array is not empty | Pass |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| actual_value | nil | 8 | 'ruby' | [1,2,3] | [1,2,3] |
| expectation_result | nil | Pass | Pass | Pass | Pass |
RSpec expectations check if code results match what we want. Use expect(actual).to matcher(expected) for positive checks. Use expect(actual).not_to matcher for negative checks. Common matchers: eq, include, be_empty. Tests pass if actual matches expected, else fail.