1

I am curious if there is a way to skip a test in RSpec in a before block? I am thinking something like this:

RSpec.configure do |config|
  config.before(:each) do |spec|
    if some_condition?
      spec.skip
    end
  end
end

The above call to skip does not work. This is for an idea I am working on, so it's important that it works this way and not have to modify the test with metadata for instance. I imagine it's possible using RSpec internals, for instance tagging the status, but I wanted to check first to see if there is any API for this.

n_x_l
  • 1,552
  • 3
  • 17
  • 34
  • You could [`break`](https://stackoverflow.com/questions/1402757/how-to-break-out-from-a-ruby-block) out of the block – builder-7000 Sep 05 '19 at 01:38
  • That does not work, just tried. By that I mean that it does exit the before block but the example still runs. – n_x_l Sep 05 '19 at 01:43
  • I see, then you could use [`pending()`](https://stackoverflow.com/questions/27288775/how-to-ignore-or-skip-a-test-method-using-rspec) to suspend the test. – builder-7000 Sep 05 '19 at 01:53
  • That doesn't do it either, I already went through the options listed online. – n_x_l Sep 05 '19 at 02:29

2 Answers2

2

Looks like you have to explicitly call the skip inside the block:

around(:each) do |example|
  if some_condition?
    example.run
  else
    example.skip
  end
end

Documentation for around hooks

Turns the example into a "pending" (skips it, prints output differently, etc)

Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24
  • Does not work, as expected since this will surely run the example regardless. – n_x_l Sep 05 '19 at 13:40
  • I updated my comment with a working example that I tested. Call `example.skip` to skip a spec, `example.run` to run it – Jay Dorsey Sep 05 '19 at 15:32
0

I asked in the rspec mailing list, and it turns out skip 'reason' does the trick. The example is marked as pending, which is not ideal, but that answers the question.

n_x_l
  • 1,552
  • 3
  • 17
  • 34