Pedro Adame Vergara
Pedro Adame Vergara

Reputation: 592

Rails testing, have a setup only for some tests

We are writing tests to a Rails application and we need to test a controller where we pass a header for almost every request, but not all.

You pass a header setting @request.headers['my-header'] = 'my-header-content', but we only want to do this in some tests, not all, so having this line in setup is not an option, but we consider incorrect to have it repeated 20 or 30 times for every request made.

How could we achieve that?

Upvotes: 0

Views: 304

Answers (1)

lcguida
lcguida

Reputation: 3847

If you're using rspec, you can separate them into two different context and then do a before for that context:

describe MyController do 
  context 'with specific header' do 
    before :all do 
      @request.headers['my-header'] = 'my-header-content'
    end
  end

  context 'other context' do
     #Tests without context
  end
end

If you're using minitest you can always separate your tests into classes. Then make one class to the specific header and another one to the other tests.

Upvotes: 1

Related Questions