Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require all spec/support files by default #92

Open
andyw8 opened this issue Apr 28, 2019 · 5 comments
Open

Don't require all spec/support files by default #92

andyw8 opened this issue Apr 28, 2019 · 5 comments

Comments

@andyw8
Copy link
Contributor

andyw8 commented Apr 28, 2019

This is specific to rspec-rails:

The generator originally created a helper which required everything in support/**/*.rb. This was changed in rspec/rspec-rails#1137 but I still find many projects which require all the files, either because it's an old project, or because the advice was ignored.

@pirj
Copy link
Member

pirj commented Apr 29, 2019

Is this specific to rspec-rails, or does bare RSpec require support files? I see this in RSpec's own test suite:

require 'rspec/support/spec'

@andyw8
Copy link
Contributor Author

andyw8 commented Apr 30, 2019

Oh, interesting.

It seems RSpec own suite started using that in rspec/rspec-core@82bf93e (March 2010), which was far before rspec/rspec-rails#1137.

I suspect many most non-Rails projects don't do this, but I think it would be fine to have this advice apply for everywhere.

@dgollahon
Copy link

I think this advice really only makes sense if you have a large number of support files. The trouble with selectively requireing any files in ruby is that if multiple files need the dependency, there's no way to enforce that they all individually require it as long as the first one that happens to be loaded requires it.

Therefore, you may not notice that you've missed the appropriate require in some files. This means that if you load only that one spec (that is supposed to be able to run fast now), it won't work at all. I think the maintenance of taking a selective require approach in ruby should only be undertaken if your test suite is painfully slow. Maybe it makes sense for most rails apps--I'm not sure. I always require everything before running my tests to avoid this problem but all the projects I work on are small-to-medium size. I think this recommendation should be project-size dependent or at least list this maintenance caveat.

@andyw8
Copy link
Contributor Author

andyw8 commented Apr 30, 2019

Fair point. I'll think about how to phrase it.

@pirj
Copy link
Member

pirj commented Apr 30, 2019

There's also when_first_matching_example_defined:

RSpec.configure do |config|
  config.when_first_matching_example_defined(:db) do
    require "support/db"
  end
end

Together with define_derived_metadata:

RSpec.configure do |config|
  # Tag all groups and examples in the spec/model directory with :db => true
  config.define_derived_metadata(:file_path => %r{/spec/model/}) do |metadata|
    metadata[:db] = true
  end
end 

it provides a quite elegant (though magical) way to lazy-load support dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants