Berna Castro
Berna Castro

Reputation: 35

RSpec: Multiple asserts for a controller spec

Let me explain the issue...

I was testing a Rails controller with RSpec and I have one scenario with about 8 asserts..

I don't like to have an "it" with that amount of asserts so what I did was this:

RSpec.shared_examples 'successful payment' do |term_type, years|
  let(:surgeon_package) { assigns(:surgeon).reload.last_surgeon_package }
  let(:payment)         { surgeon_package.payments.first }

  before do
    put(:update_account, params)
  end

  it 'package term must be yearly' do
    expect(surgeon_package.payment_term.term).to eq(1)
    expect(surgeon_package.payment_term.term_type).to eq(term_type)
  end

  it 'package payments must be one' do
    expect(surgeon_package.payments.count).to eq(1)
  end

  it '...'
end

But as you can see I request the action :update_account on each it.

What do you think is the best way to solve this issue? since I don't want to request /update_account every time.

Global variables? $cache? get the code back to one "it" ? thoughts?

Thanks community

Upvotes: 2

Views: 191

Answers (1)

zetetic
zetetic

Reputation: 47588

Your test looks fine. Unless there are severe performance issues I would leave it just as it is.

It may be tempting to contort yourself into knots trying to have your cake and eat it too, but I have always found it best to resist the temptation and keep the test code simple and readable (or "expressive").

If tests are just too slow, consider collapsing multiple examples into one and making multiple expectations. This hurts the test report in favor of increasing test speed. There is always a trade-off.

Upvotes: 1

Related Questions