Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Created October 11, 2013 03:09
Show Gist options
  • Save JonRowe/6929003 to your computer and use it in GitHub Desktop.
Save JonRowe/6929003 to your computer and use it in GitHub Desktop.
Whilst betterspecs.org has good ideas, it's examples are flawed.

Use contexts

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment