-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Consider making example group mixins more explicit #662
Comments
👍 Or maybe the ability to write a top level yaml file that defines what our directory structure looks like? I'm returning to rspec after a year and I'm having trouble figuring out exactly what methods are available where. |
I don't think this makes anything more transparent in terms of seeing what modules are mixed in or what modules are available but I'm fan of not enforcing a directory layout on people. |
Obviously I am in favour of this :) I've always found the magic directories confusing, even before I started doing it my own way. |
👉 (mixed on this one) I personally, just my 2¢, really like the auto-inferring based on the file path. This very much falls in line with the Rails convention over configuration. Rails has specific locations for code, it just makes sense that the tests mirror that (to a degree). It also makes sense that the related type of tests auto-includes the relevant setup (yes I realize that MiniTest and Rails force class inheritance and don't mix stuff in magically). IMHO, I think With that said, there are times I do want to customize it. Having looked at the code, I know how to do this (see setup file). Though, duplicating those lines for different file paths is not a fun approach. It would be great to see:
❤️ |
|
What about Maintain the existing magic whilst making overriding/reconfiguring easy. |
I can go either way on this one, but I think providing this as a configuration option instead of the default might be a better path to go down. Just my $0.02. Either way, I think it's worth implementing in some form. |
I'll take a stab at adding the [current version of the] docs I proposed tomorrow ❤️ ❤️ |
possibly interesting, but how about infering the mixin from the class of the top level describe subject. So if it is a kind_of? ActiveRecord::Base, it gets the models, if a Controller it gets them. thats ok I suppose for Model and Controller mixings, but when you get to Request... hmmm, then fall back to explicitly mentioning again, and allow explicit for when you have a bare Object subclass as a model... |
Great idea. I don't see this as being the standard use case for most Rails developers, so I agree that it should remain the way it is now with a configuration option to disable the auto-mixins. However, adding it as a configuration option in the generated |
rspec-rails uses public APIs from rspec-core to do this auto-inclusion. There is one additional helper in that file, which is there to support windows and *nix paths. We could probably clarify that helper a bit and expose it as an API, but I don't think we need to expose any new API on |
I personally never had a need to change the inferred defaults and think that making it explicit would be a step back. Overridible defaults > configuration. |
👍 it will make it easier to avoid getting the Active Record stuff mixed into specs of non-AR models. |
I think being explicit when it comes to test environment loading is a fantastic idea. My vote would be to turn the consider ├── lib
├── mailers
├── models
├── observers
├── presenters
├── requests
├── routing
├── scripts
├── services
└── spec_helper.rb What I would like to see is a vanilla RSpec environment for all specs referencing I like this approach for a couple reasons. It localizes configuration for spec types (change little instead of a lot) And it also is very clear what the spec_helper.rb file in the sub-directory is intended for. |
As someone who teaches beginning developers to test Rails apps using RSpec this feature often causes a lot of confusion. Mostly because the Really though, I'd like to auto generate the folder structure and require explicit mixin inclusion (if that's the only magic done at the moment). |
Just out of curiosity, how many different types of directories are supported? models, unit, features, routing, requests, views, controllers, acceptance, integration, helpers, support, api? Seems we support multiple conventions, which breaks the convention argument. Let's pick one. For instance, from http://rubydoc.info/gems/rspec-rails/file/README.md, "Request specs live in spec/requests, spec/api and spec/integration" ... "Note that Capybara's DSL as shown is, by default, only available in specs in the spec/features" This type of caveating shouldn't be necessary. I think the support for multiple conventions is what makes it difficult to use. My vote is to reduce the directory support and as @ndelage said, generate the directories when the generator is run. |
They should be generated when the specific generator for a type is invoked. It's not a necessity for any of them to be used so creating a tonne of empty directories will just confuse people. The read me could do with updating, request specs live in request specs. The other two are for integration tests, they just happen to use the same functionality. |
I don't think we should move on this right now. With #969 merged, we enable people who care about this to try a number of different ideas. I'd like to do that to see if anything ends up being naturally popular. If nothing takes hold we can circle back and lead the charge. |
#969 seems like a pretty hidden feature -- you have to know about I'd still like to see the directory-based inference changed from opt-out to opt-in as it's not a way I would recommend using RSpec, and it seems odd for the defaults of a new major version not to align with the lead maintainer's recommendation. That said, I don't use rails and the fact that no one has stepped forward to implement this suggests it's not super important to those who use rspec-rails. Also, I don't consider this a release blocker. |
My point being that the few people who really care about this issue now have non-hack way to experiment with different ideas and hopefully propose something that works in the real world. An example of this happening (perhaps not how we wanted!): with #969 I personally don't feel any need to change anything else, since I can just disable the entire feature I dislike ;) |
I've been mulling this over some more. I'm still definitely in the "this is not a release blocker" camp but I think I'd like to see #970 resurrected and merged in. In #970, @xaviershay had said:
IMO, explicit configuration of directories would be a new feature and could easily go in any future 3.x release, but changing from opt-out to opt-in is a "now or never" (or at least not until 4.0) thing -- and given how close #970 is to changing this to opt-in, I think we should take it across the finish line. Having the line in the generated Besides, I think an explicit I may even work on resurrecting #970 myself. |
I resurrected #970 |
Do we have anything left to complete for this one? |
Not as far as I know. |
…rectories. Added metadata to all tests to let rspec-rails know the type of each test suite (controller, model, feature etc). Older rspec-rails versions demanded a particular directory structure for the specs, so that e.g. controllers tests (which had to be in /specs/controllers) had a module mixed in during testing, to enable helper methods necessary for each type of test (e.g. accessing response headers directly during controller tests). This is being deprecated and it will become opt-in in rspec-rails 3. A particular directory structure will no longer be enforced, each test suite will have to specify the type of test by using metadata (a :type named argument to the "describe" method at the top of each file). I've gone ahead and, in preparation for rspec-rails 3, I've added metadata to all tests which make use of rspec-rails helper methods. Now that a particular directory structure is no longer enforced, I've reorganized tests in a "unit_tests" and "acceptance_tests" structure which is more confortable for me. For details about the old directory structure and the new metadata-based spect type specification, see: https://relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure For interesting discussions about this topic see: rspec/rspec-rails#662 https://github.com/rspec/rspec-rails/pull/970/files rspec/rspec-rails#1002
There's a lot of implicit file-location-based configuration done by rspec-rails--e.g. spec files in "spec/{models/controllers/requests/mailers/etc}" get a special mixin just by being in that directory. This fits well with rails' "convention over configuration" philosophy, but often creates confusion, as it's not obvious from looking at the code where particular helper methods are coming from. In addition, this setup can actively get in your way if you decide that you want to use a different directory structure. For example, @xaviershay blogged about the way he tests rails apps, and he uses a top level unit/integration/acceptance directory structure. I haven't worked on a rails app in a while, but I've been using kind of top-level structure in my ruby applications as well, and I'd probably use it the next time I build a rails app.
For rspec-rails, I was thinking we could make things a bit more explicit:
describe User, :model do...
ordescribe UsersController, :controller do...
). This is the same mechanism that shared contexts can use, and in fact, the existing mixins could easily be rewritten as shared contexts (which would decouple them fromActiveSupport::Concern
).RSpec.configuration.infer_spec_type_from_file_location = true
.spec_helper.rb
file could include this config line by default, with a comment explaining it--that would keep the current behavior the same for new apps.The text was updated successfully, but these errors were encountered: