Contexts are a powerful method to make your tests clear and well organized. In the long term this practice will keep tests easy to read.
I agree with the premise, and I agree that this:
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
Is bad, and needs some work. However I feel their GOOD example:
GOOD
context 'when logged in' do
it { should respond_with 200 }
end
context 'when logged out' do
it { should respond_with 401 }
end
doesn't actually describe the behaviour of the system under test, it's too focused on implementation details I prefer
BETTER
context 'when logged in' do
it 'allows access to the resource' do
expect(response).to respond_with 200
end
end
context 'when logged out' do
it 'prevents access to the resource' do
expect(response).to respond_with 401
end
end