Emil
Emil

Reputation: 1260

Rspec Best Practices : Why should "it" expect only one thing?

I read on this slide about RSpec best practices ( http://blog.bandzarewicz.com/slides/krug-the-perfect-rspec/#19 ) and many other places , that it is best practice to have only one expectation with one "it" . For example :

describe UsersController, '#create' do
  # setup spec...
  it 'creates a new user' do
    should assign_to(:user).with(user)
    should set_the_flash
    should respond_with(:redirect)
    should redirect_to(admin_user_path(user))
  end
end

vs.

describe UsersController, '#create' do
  # setup spec...
  it { should assign_to(:user).with(user) }
  it { should set_the_flash }
  it { should respond_with(:redirect) }
  it { should redirect_to(admin_user_path(user)) }
end

Why is it best practise to have only one expectation with one "it" ?

Upvotes: 3

Views: 556

Answers (1)

denis.peplin
denis.peplin

Reputation: 9851

Because this approach is better for documentation. Try rspec --format documentation. And another reason, with one should per it, you can always see which test is failing.

Upvotes: 3

Related Questions