Programmatically skip a spec in a before block

17 views
Skip to first unread message

Wael

unread,
Sep 5, 2019, 3:05:37 AM9/5/19
to rspec
Hi, 

I am working on an idea and I was curious if there is a way I can skip a spec 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

Ideally, I want to skip the spec and mark it as so visually, perhaps using something other like "*", but for now, I just want to have it working. The above actually does not achieve it, the tests still run. 

Any ideas?

Benoit Tigeot

unread,
Sep 5, 2019, 3:35:16 AM9/5/19
to rs...@googlegroups.com, Wael
Hello

Your a probably looking for exclusion filters:

See https://relishapp.com/rspec/rspec-core/v/2-0/docs/filtering/exclusion-filters

Regards
Benoit
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/5fc0d890-7649-4fb6-87a4-213259f53670%40googlegroups.com.

Jon Rowe

unread,
Sep 5, 2019, 3:46:43 AM9/5/19
to rs...@googlegroups.com
Hi

You can skip specs using metadata, which might be appropriate if you can access `some_condition?` outside of your hook.

e.g.

RSpec.describe “Something”, skip: some_condition? do 
  it “will not run” do
    fail
  end
end

Or you can use the skip method.

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

The method on the passed in example you are attempting to use, is actually just a reader for the metadata value, and doesn’t skip.

See:

Cheers
Jon Rowe
---------------------------

Mohamed Wael Khobalatte

unread,
Sep 5, 2019, 7:44:26 AM9/5/19
to rs...@googlegroups.com
John, that's exactly what I am looking for, the `skip "Reason"` part, thank you. Just one more question, is there a way I can have it marked in the output as something other than pending? Perhaps something like "Skipped"? The "Reason" is shown, but sort of buried below all the pending statements. 

--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/NDAgImEWdrE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/dejalu-217-84541c10-9259-45e6-bc21-140b6642fe57%40jonrowe.co.uk.


--
Wael Khobalatte

Jon Rowe

unread,
Sep 5, 2019, 9:06:38 AM9/5/19
to rs...@googlegroups.com
I’m afraid not, skipped examples are what originally were pending examples. An example without an implementation is a skipped example. If you use the RSpec 3 pending API then that example is run and expected to fail.

Jon Rowe
---------------------------

On 5 September 2019 at 12:43, Mohamed Wael Khobalatte wrote:
John, that's exactly what I am looking for, the `skip "Reason"` part, thank you. Just one more question, is there a way I can have it marked in the output as something other than pending? Perhaps something like "Skipped"? The "Reason" is shown, but sort of buried below all the pending statements. 


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages