vim-rails icon indicating copy to clipboard operation
vim-rails copied to clipboard

Let :A edit requests spec

Open sebastianhoitz opened this issue 10 years ago • 16 comments
trafficstars

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.

sebastianhoitz avatar Nov 25 '14 19:11 sebastianhoitz

Do request tests map cleanly to controllers?

tpope avatar Nov 25 '14 19:11 tpope

Yes.

app/controllers/v1/users_controller.rb => spec/requests/v1/users_controller.rb

sebastianhoitz avatar Nov 26 '14 14:11 sebastianhoitz

Uh are you sure you didn't screw that up?

tpope avatar Nov 26 '14 19:11 tpope

Sorry, I meant spec/requests/v1/users_controller.rb :coffee:

sebastianhoitz avatar Nov 27 '14 10:11 sebastianhoitz

So the name of the spec doesn't have _spec in it?

tpope avatar Nov 27 '14 19:11 tpope

Brining this issue back to life.

They usually map app/controllers/api/v1/users_controller.rb => spec/requests/api/v1/users_spec.rb

teoljungberg avatar Mar 17 '15 08:03 teoljungberg

@teoljungberg can you cite an OSS project or 2 that does this?

tpope avatar May 07 '15 18:05 tpope

@tpope not from the top of my head. I've seen it in a few client projects though

teoljungberg avatar May 08 '15 07:05 teoljungberg

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",
      \   },
      \ }

teoljungberg avatar Dec 06 '16 08:12 teoljungberg

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

jasonkarns avatar Mar 28 '18 17:03 jasonkarns

Seems irrelevant to a filename conversation but ok.

tpope avatar Mar 29 '18 03:03 tpope

It seems that there is no alternate defined on the request spec file so going from spec to controller does not work.

eloyesp avatar Jul 10 '19 17:07 eloyesp

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

AaronRustad avatar Apr 12 '20 15:04 AaronRustad

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 avatar May 20 '20 17:05 eloyesp

@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

heraldofsolace avatar Jun 14 '20 09:06 heraldofsolace

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'
      \   }
      \}

benoittgt avatar Oct 28 '20 08:10 benoittgt