pezra / rspec-mode

An RSpec minor mode for Emacs
258 stars 112 forks source link

Support for request specs in rspec-toggle-spec-and-target? #202

Open powersurge360 opened 2 years ago

powersurge360 commented 2 years ago

Hello! I searched the source code and checked the documentation but I didn't see very much about request specs. Is there anyway to set rspec-toggle-spec-and-target to match something in spec/requests/{model-name}_spec.rb instead of spec/controllers/{model-name}_controller_spec.rb? Controller specs seem to be soft-deprecated and rspec stubs request specs instead of controller specs nowadays.

dgutov commented 2 years ago

Hi!

Historically, I think, rspec specs never used a "fancy" naming scheme for directories. Unlike test-unit which is used with Rails by default.

And IIUC this didn't change. Looking at https://github.com/rspec/rspec-rails, it suggests specifying the spec type using the :type metadata. The recently merged new snippet desct allows you to have that value auto-filled.

powersurge360 commented 2 years ago

In the generators for controllers, it will generate a requests/{model-name}_spec.rb if the :request_specs option is set to true. There's a similar check for controllers but only :request_specs defaults to true.

On the official rspec blog back during the release of rspec 3.5, they mentioned that controller tests were soft deprecated in rails 5.

A relevant quote from that article:

For new Rails apps: we don't recommend adding the rails-controller-testing gem to your application. The official recommendation of the Rails team and the RSpec core team is to write request specs instead.

With these changes going back to rails 5 released back in 2016, I think it's pretty compelling to add at least an option to toggle to requests/ instead of controllers/ for this function. Folks working on rails apps after that time period are not likely to even have controller specs with the above deprecations and recommendations and project defaults.

dgutov commented 2 years ago

All right, then.

I suppose this will require some extra logic in rspec-spec-file-for and rspec-target-file-for.

Perhaps you'd like to try implementing that?

powersurge360 commented 2 years ago

Yea I can take a crack at it sometime! May take me a bit and it will be literally the first elisp I've written but I'll check it out sometime soon. I guess what I'd need to do is create a sort of special case for request specs because they, unlike most of the other conventions, don't map 1:1 with the file name and require a little massaging, right? Is there anything I ought to know or that you'd like to see to accept a PR?

dgutov commented 2 years ago

Things to know:

C-M-x re-evaluates a defun in the current session. So you can try out new changes right away. C-u C-M-x instruments a defun for debugging.

And the functions that I mentioned in the previous message are most likely the only ones that need changing.

There are no "unusual" mappings supported by rspec-mode so far, so there is no existing setting or a list that can be added to.