Search code examples
ruby-on-railstddrspec-rails

Conditional filters for contexts in rspec?


Is there a way to set conditional filters for contexts in rspec?

I have a context with variables that are only valid if a particular variable != 1, so I wanted to use that as the filter for that context's test. I can't have the filter on the test itself because I'm using said variables to set the context for that test (see below for ideal state):

context 'if interval is not 1 every non interval day', :if => Reminders::INTERVAL != 1  do
        num = rand(0..100)
        non_limbo = num + num/(Reminders::INTERVAL - 1) + 1

        let!(:evaluation) { create(:evaluation, created_at: non_limbo.days.ago) }

        it 'doesn't send an email' do
          expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(0)
        end
      end

Solution

  • You can surround the whole context in a condition:

    if Reminders::INTERVAL != 1
      context 'if interval...' do
        # ...
      end
    end