Skip to content

Consider making example group mixins more explicit #662

@myronmarston

Description

@myronmarston

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:

  • By default, don't mixin the various example group modules based on file location.
  • Use simple, explicit metadata tagging to mixin example groups instead (e.g. describe User, :model do... or describe 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 from ActiveSupport::Concern).
  • Provide a config option so that users who want to use the file-location mechanism still can, e.g. RSpec.configuration.infer_spec_type_from_file_location = true.
  • The generated 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.
  • If we move in this direction, we'll probably want to provide the config option in the last 2.x release, and maybe have it generate a warning if it's not set (so that users can use that release to help get their apps ready to upgrade, then upgrade to rspec-rails 3 and have it work).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions