RSpec: describe/context/it
This course covers the structure of RSpec spec files, using the specs of one Rails app. It shows how RSpec.describe names the class under test, giving examples described_class and an implicit subject, and how an added string narrows a file to one slice of behavior. It covers context as an alias of describe for shared premises, two styles of it examples, and lazy let versus eager let!. It ends with the spec/ naming convention and the app's rails_helper.rb settings.
What you'll learn
- ✓Naming the class under test with RSpec.describe and described_class
- ✓Grouping examples under a shared premise with context
- ✓Writing it examples in block style and one-line matcher style
- ✓The difference between lazy let and eager let!
- ✓How spec/ file paths mirror app/ file paths
- ✓The rails_helper.rb settings for FactoryBot, transactions and shoulda-matchers
This app's payoff: RSpec, the framework it chose
That's terminal output from this app's spec suite, unedited. Every indented line traces back to a describe, context or it in one file, spec/services/identity/account_update_service_spec.rb. This course's job is to show how that file produces that output.
Read the full lesson free →Curriculum
7 lessonsThis app's payoff: RSpec, the framework it chose
Shows documentation-format output from one of this app's spec files and introduces describe, context, it and let.
RSpec.describe: naming what's under test, two forms
Covers RSpec.describe with a class or a class plus a string, described_class and the implicit subject.
context: grouping related examples under a shared premise
Explains context as an alias of describe that groups examples under a named premise and can override subject.
it: the example itself, in two styles this app uses
Compares the descriptive block style of it with the one-line is_expected style built on shoulda-matchers.
let vs let!: lazy vs eager, and the few lines that show the difference
Contrasts lazy let with eager let!, using a query spec and the rspec-core source that defines let!.
spec/ mirrors app/: the naming convention, and this app's RSpec config
Maps spec files to app files and explains the rails_helper.rb settings and the documentation formatter.
Putting it together: the framework, from the inside out
Reviews the describe, context, it and let model and introduces a pure Ruby challenge called format_examples.