vim-rails
vim-rails copied to clipboard
Let :A edit requests spec
I have a controller that does not have controller unit tests but request integration tests.
When I am inside the controller I would like to be able to press :A to open the request test file. But instead it tries to open the controller unit test.
Do request tests map cleanly to controllers?
Yes.
app/controllers/v1/users_controller.rb => spec/requests/v1/users_controller.rb
Uh are you sure you didn't screw that up?
Sorry, I meant spec/requests/v1/users_controller.rb :coffee:
So the name of the spec doesn't have _spec in it?
Brining this issue back to life.
They usually map app/controllers/api/v1/users_controller.rb => spec/requests/api/v1/users_spec.rb
@teoljungberg can you cite an OSS project or 2 that does this?
@tpope not from the top of my head. I've seen it in a few client projects though
Here's a projection I use to resolve this issue (for those interested).
let g:rails_projections = {
\ "app/controllers/*_controller.rb": {
\ "test": [
\ "spec/requests/{}_spec.rb",
\ "spec/controllers/{}_controller_spec.rb",
\ "test/controllers/{}_controller_test.rb"
\ ],
\ "alternate": [
\ "spec/requests/{}_spec.rb",
\ "spec/controllers/{}_controller_spec.rb",
\ "test/controllers/{}_controller_test.rb"
\ ],
\ },
\ "spec/requests/*_spec.rb": {
\ "command": "request",
\ "alternate": "app/controllers/{}_controller.rb",
\ "template": "require 'rails_helper'\n\n" .
\ "RSpec.describe '{}' do\nend",
\ },
\ }
FWIW, rspec-rails follows this convention.
Request specs are marked by :type => :request or if you have set config.infer_spec_type_from_file_location! by placing them in spec/requests.
https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec
Seems irrelevant to a filename conversation but ok.
It seems that there is no alternate defined on the request spec file so going from spec to controller does not work.
When using @teoljungberg rails projections placed in config/projections.json, I'm presented with the following error opening vim. I assume this has to do with non-standard(?)/unexpected JSON format and perhaps pre-processing of the file? Would anybody be able to provide pointers as to where I should be looking to ensure that the file is correctly used? I assume there must be something wrong with my environment.
Error detected while processing function rails#buffer_setup[14]..<SNR>47_BufProjectionCommands[14]..<SNR>47_app_commands[2]..<SNR>47_app_projections[37]..rails#json_parse:
line 3:
E474: Invalid argument
Press ENTER or type command to continue
Error detected while processing function rails#buffer_setup[14]..<SNR>47_BufProjectionCommands:
line 14:
E170: Missing :endfor
It seems that requests specs are generated by default now by rspec-rails and the convention seems to be:
app/controllers/users_controller.rb => spec/requests/users_spec.rb
app/controllers/admin/users_controller.rb => spec/requests/admin/users_spec.rb
As far as I understand, it seems that requests specs should be first in the alternate list.
@eloyesp That seems like what is created when you generate a scaffold. When you generate a controller, it seems to be using a different format
app/controllers/some_controller.rb => spec/requests/some_request_spec.rb
The same can be done for rake tasks with RSpec.
let g:rails_projections = {
\ 'lib/tasks/*.rake': {
\ 'alternate': 'spec/lib/tasks/{}_spec.rb'
\ },
\ 'spec/lib/tasks/*_spec.rb': {
\ 'alternate': 'lib/tasks/{}.rake'
\ }
\}