Role models are important.
|
Tip
|
You can find a beautiful version of this guide with much improved navigation at https://rspec.rubystyle.guide. |
This RSpec style guide outlines the recommended best practices for real-world programmers to write code that can be maintained by other real-world programmers.
RuboCop, a static code analyzer (linter) and formatter, has a rubocop-rspec extension, provides a way to enforce the rules outlined in this guide.
|
Note
|
This guide assumes you are using RSpec 3 or later. |
You can generate a PDF copy of this guide using AsciiDoctor PDF, and an HTML copy with AsciiDoctor using the following commands:
# Generates README.pdf
asciidoctor-pdf -a allow-uri-read README.adoc
# Generates README.html
asciidoctor README.adoc|
Tip
|
Install the gem install rouge |
The guide is separated into sections based on the different pieces of an entire spec file. There was an attempt to omit all obvious information, if anything is unclear, feel free to open an issue asking for further clarity.
Per the comment above, this guide is a work in progress - some rules are simply lacking thorough examples, but some things in the RSpec world change week by week or month by month. With that said, as the standard changes this guide is meant to be able to change with it.
Do not leave empty lines after feature, context or describe descriptions.
It doesn’t make the code more readable and lowers the value of logical chunks.
# bad
describe Article do
describe '#summary' do
context 'when there is a summary' do
it 'returns the summary' do
# ...
end
end
end
end
# good
describe Article do
describe '#summary' do
context 'when there is a summary' do
it 'returns the summary' do
# ...
end
end
end
endLeave one empty line between feature, context or describe blocks.
Do not leave empty line after the last such block in a group.
# bad
describe Article do
describe '#summary' do
context 'when there is a summary' do
# ...
end
context 'when there is no summary' do
# ...
end
end
describe '#comments' do
# ...
end
end
# good
describe Article do
describe '#summary' do
context 'when there is a summary' do
# ...
end
context 'when there is no summary' do
# ...
end
end
describe '#comments' do
# ...
end
endLeave one empty line after let, subject, and before/after blocks.
# bad
describe Article do
subject { FactoryBot.create(:some_article) }
describe '#summary' do
# ...
end
end
# good
describe Article do
subject { FactoryBot.create(:some_article) }
describe '#summary' do
# ...
end
endOnly group let, subject blocks and separate them from before/after blocks.
It makes the code much more readable.
# bad
describe Article do
subject { FactoryBot.create(:some_article) }
let(:user) { FactoryBot.create(:user) }
before do
# ...
end
after do
# ...
end
describe '#summary' do
# ...
end
end
# good
describe Article do
subject { FactoryBot.create(:some_article) }
let(:user) { FactoryBot.create(:user) }
before do
# ...
end
after do
# ...
end
describe '#summary' do
# ...
end
endLeave one empty line around it/specify blocks. This helps to separate the expectations from their conditional logic (contexts for instance).
# bad
describe '#summary' do
let(:item) { double('something') }
it 'returns the summary' do
# ...
end
it 'does something else' do
# ...
end
it 'does another thing' do
# ...
end
end
# good
describe '#summary' do
let(:item) { double('something') }
it 'returns the summary' do
# ...
end
it 'does something else' do
# ...
end
it 'does another thing' do
# ...
end
endWhen subject is used, it should be the first declaration in the example group.
# bad
describe Article do
before do
# ...
end
let(:user) { FactoryBot.create(:user) }
subject { FactoryBot.create(:some_article) }
describe '#summary' do
# ...
end
end
# good
describe Article do
subject { FactoryBot.create(:some_article) }
let(:user) { FactoryBot.create(:user) }
before do
# ...
end
describe '#summary' do
# ...
end
endWhen declaring subject, let!/let and before/after hooks they should be in the following order:
-
subject -
let!/let -
before/after
# bad
describe Article do
before do
# ...
end
after do
# ...
end
let(:user) { FactoryBot.create(:user) }
subject { FactoryBot.create(:some_article) }
describe '#summary' do
# ...
end
end
# good
describe Article do
subject { FactoryBot.create(:some_article) }
let(:user) { FactoryBot.create(:user) }
before do
# ...
end
after do
# ...
end
describe '#summary' do
# ...
end
endUse contexts to make the tests clear, well organized, and easy to read.
# bad
it 'has 200 status code if logged in' do
expect(response).to respond_with 200
end
it 'has 401 status code if not logged in' do
expect(response).to respond_with 401
end
# good
context 'when logged in' do
it { is_expected.to respond_with 200 }
end
context 'when logged out' do
it { is_expected.to respond_with 401 }
endcontext blocks should pretty much always have an opposite negative case.
It is a code smell if there is a single context (without a matching negative case), and this code needs refactoring, or may have no purpose.
# bad - needs refactoring
describe '#attributes' do
context 'the returned hash' do
it 'includes the display name' do
# ...
end
it 'includes the creation time' do
# ...
end
end
end
# bad - the negative case needs to be tested, but isn't
describe '#attributes' do
context 'when display name is present' do
before do
article.display_name = 'something'
end
it 'includes the display name' do
# ...
end
end
end
# good
describe '#attributes' do
subject(:attributes) { article.attributes }
let(:article) { FactoryBot.create(:article) }
context 'when display name is present' do
before do
article.display_name = 'something'
end
it { is_expected.to include(display_name: article.display_name) }
end
context 'when display name is not present' do
before do
article.display_name = nil
end
it { is_expected.not_to include(:display_name) }
end
endUse let and let! for data that is used across several examples in an example group.
Use let! to define variables even if they are not referenced in some of the examples, e.g. when testing balancing negative cases.
Do not overuse lets for primitive data, find the balance between frequency of use and complexity of the definition.
# bad
it 'finds shortest path' do
tree = Tree.new(1 => 2, 2 => 3, 2 => 6, 3 => 4, 4 => 5, 5 => 6)
expect(dijkstra.shortest_path(tree, from: 1, to: 6)).to eq([1, 2, 6])
end
it 'finds longest path' do
tree = Tree.new(1 => 2, 2 => 3, 2 => 6, 3 => 4, 4 => 5, 5 => 6)
expect(dijkstra.longest_path(tree, from: 1, to: 6)).to eq([1, 2, 3, 4, 5, 6])
end
# good
let(:tree) { Tree.new(1 => 2, 2 => 3, 2 => 6, 3 => 4, 4 => 5, 5 => 6) }
it 'finds shortest path' do
expect(dijkstra.shortest_path(tree, from: 1, to: 6)).to eq([1, 2, 6])
end
it 'finds longest path' do
expect(dijkstra.longest_path(tree, from: 1, to: 6)).to eq([1, 2, 3, 4, 5, 6])
endUse let definitions instead of instance variables.
# bad
before { @name = 'John Wayne' }
it 'reverses a name' do
expect(reverser.reverse(@name)).to eq('enyaW nhoJ')
end
# good
let(:name) { 'John Wayne' }
it 'reverses a name' do
expect(reverser.reverse(name)).to eq('enyaW nhoJ')
endUse shared examples to reduce code duplication.
# bad
describe 'GET /articles' do
let(:article) { FactoryBot.create(:article, owner: owner) }
before { page.driver.get '/articles' }
context 'when user is the owner' do
let(:user) { owner }
it 'shows all owned articles' do
expect(page.status_code).to be(200)
contains_resource resource
end
end
context 'when user is an admin' do
let(:user) { FactoryBot.create(:user, :admin) }
it 'shows all resources' do
expect(page.status_code).to be(200)
contains_resource resource
end
end
end
# good
describe 'GET /articles' do
let(:article) { FactoryBot.create(:article, owner: owner) }
before { page.driver.get '/articles'
评论