Comments (6)
@pirj @JonRowe thanks for the comments.
I have carefully read them. I have also read more about rspec in docs, in the help command output, and in source code files. Now I see the ground you stand on and to be honest I agree with your points. I agree that it is not the rspec responsibility to do a sophisticated loading of ruby files.
I will open a PR soon with a small README.md changes about how the shared contexts/examples may be loaded to be used in test cases.
from rspec-core.
👋 Ruby itself has no auto requiring etc of any files by default, it places lib
onto the load path and by convention gems start from lib/<their-name>.rb
and require
,autoload
or load
etc from there. Bundler requires gems by default (unless you tell it not to) so a lot of things appear to be inferred but they are actually explicitly loaded, RSpec follows this convention. Some frameworks (such as Rails, autoload files with more magic) but its not been something we do.
The pattern you mention is actually generated as part of a rspec-rails
install but not the plain spec/spec_helper.rb
, if you wanted to improve the generated spec helper to mention this pattern commented out as an example...
from rspec-core.
@JonRowe thanks for the quick reply.
It is clear that Ruby does not load every file in project directories by itself. This responsibility was taken by zeitwerk
gem in the recent rails
versions for example.
My concern about file loading comes from this sentence: "I believe that every core feature should work out of the box". For example one of the rails
core features - MVC, so we have models, controllers, view directories. Imagine that files from controllers and views would be autoloaded, but files from models dir would have to be loaded manually with require
. Looks strange at least. I have had similar confusion with shared contexts. Maybe my labeling of "shared contexts/examples" as the core feature is wrong.
There is a convention for rspec - load files with _spec.rb suffix. But "shared logic" is not a "spec" so it has to be loaded another way. I get it. However at the same time, I would love to have it explicitly mentioned in the documentation (so I will see that I can't fully rely on rspec autoload), not to mention to have it preloaded automatically without order conflicts (which may be resolved with already mentioned zeitwerk
I think).
I'd love to update the documentation at least, if my reasons above make sense to you.
from rspec-core.
convention for rspec - load files with _spec.rb suffix
-
We don’t load all, and don’t want to. You may have e.g. a custom rubocop cop in your code, and loading Rails-related specs/shared examples is wasteful.
-
There is no single one-fits-all directory structure for shared examples. If ‘support’ was in the load_path, it would be an easier decision. Some prefer to put shared examples/contexts closer to their specs, and not to ‘shared’, and load them with ‘require_relative’.
-
Auto-loading won’t work. We could do that. But I have no certainty we want to. Also, edge cases like ‘[“left”, “right”].each do |dir| RSpec.shared_context dir do’ - what file name you’ll put it to to auto-load?
A pr showing an example of how shared examples CAN be loaded is probably fine, but I would be opposed if it promotes the “one true way” of doing things.
In any case, I would rely on @JonRowe opinion here.
from rspec-core.
I won't argue that shared examples should work out of the box but they do work out of the box, you can (and people do) use them in spec files themselves (they're actually scoped by example group) and you can put them in your spec_helper, in fact thats whats you're doing by requiring extra files you're breaking up the spec helper.
I agree it would be odd if a framework with an autoloading behaviour only loaded certain parts of it self and you had to autoload the rest, but thats not how I see RSpec here, the pattern for loading specs has a default spec/**/*_spec.rb
(which is also a configurable option) which is what we use to find and load specs, you can also override this by passing explicit specs, so we don't actually autoload anything, we load a specific set of files and then follow Ruby semantics. You'll note you have to require spec_helper.rb too, its just normally in a .rspec or other option.
In theory theres nothing stopping you configuring adding and zeitwerk to load your support folder but thats not a responsibility RSpec wishes to take on.
from rspec-core.
@pirj @JonRowe please, check this out
#3051
from rspec-core.
Related Issues (20)
- Request for an _appropriate_ way to tell if all of the examples in a given ExampleGroup are being executed
- Raise errors if `it` executed in a `before` block HOT 1
- Introduce stub_env helper HOT 1
- Nested `skip` behaves weird, is this intended? HOT 1
- Calling `before` or `after` in an invalid context that will never run should raise an error HOT 3
- RSpec skipping at_exit hooks on force quit HOT 1
- No way to filter tags with and logic HOT 3
- The SPEC environment variable does not support using bracketed example IDs HOT 9
- Guard against name collisions with internals HOT 2
- add :context scope to let method HOT 2
- Add `--only-pending` CLI option which filters to only the pending examples HOT 3
- Requiring rspec before IRB console causes warning HOT 1
- doc_string which is the first argument of example, must be string object HOT 3
- Unclear working of shared_examples inside a context in a shared_context HOT 4
- Define weak/default memoized helpers with `let?` method. HOT 6
- Numbered list of failed specs HOT 2
- Docs are offline HOT 1
- config.include works unpredictably when supplying more than one metadata to filter HOT 4
- Adding ProfileFormatter report to output stream? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rspec-core.