Rspec-core: `when_first_matching_example_defined` runs when all matching examples are filtered out

Created on 13 Feb 2020  路  6Comments  路  Source: rspec/rspec-core

Your environment

  • Ruby version: 2.5.7
  • rspec-core version: 3.8.2

Steps to reproduce

1.

config.when_first_matching_example_defined(type: :feature) do
  require 'support/db'
end
  1. Have an example with type: :feature
  2. bundle exec rspec -t ~type:feature

Expected behavior

require 'support/db' is not run

Actual behavior

require 'support/db' is run

Most helpful comment

I knew had a clue that answer would be that, thanks anyway :)

All 6 comments

:wave: It's when the example is defined, not when its run. You've loaded the example and filtered it out, which is defined, but skipped. This is thus working as designed and documented.

Without knowing the contents of your support/db I can't help with much advice, but you can then add something else to be run when the first example runs to setup the db etc.

Hey Slava 馃憢

I can suggest using a regular before hook with some trigger-once check:

before(:example, type: :feature) do
  only_once { require 'support/db' }
end

Fun fact, require 'support/db' will only run once provided its the same path.

Agree, it's a bad example. A slightly better one:

before(:example, type: :feature) do
  only_once { expensive_setup }
end

I knew had a clue that answer would be that, thanks anyway :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

take picture take  路  5Comments

benmmurphy picture benmmurphy  路  6Comments

pirj picture pirj  路  5Comments

andyl picture andyl  路  6Comments

ankit8898 picture ankit8898  路  3Comments