Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Is there a correct folder/file structure for request specs on rspec?

$
0
0

spec/requests is the default. From the docs...

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.

There is no clear guidance beyond that. But organizing them by route seems to make sense.

Say I have a test that creates a resource A (post /resource_A), then creates a resource B (post resource_B) and then checks that on GET /resource_A/previously_created_a_id I get the created resource B as an associated resource, and all this endpoints go to different controllers, on what file should I put this test?

This is a test that /resource_A/:id displays its associated resource. Creating the associated resource_B is test setup and, unless there's a good reason, should not happen via a route. That would be tested as part of resource_B.

Here I'm assuming you're using FactoryBot. If you're not, you still wouldn't make test resources by calling a route, but instead by using the model or controller.

# spec/requests/resource_a_spec.rbRspec.describe 'ResourceA management' do  let(:resource_A) { create(:resource_a) }  describe '/resource_A/:id' do    context 'with a b' do      let(:resource_B) { create(:resource_b) }      before {        resource_A.update!(b: resource_b)      }      it 'sees its associated resource' do        get "/resource_A/#{resource_A.id}"        ...      end    end  endend

As your tests for a route grow larger, you might want to split them up into separate files. Depends on what you're testing. For example, you could do it by action.

spec/  requests/    resource_a/      show_spec.rb    # GET /resource_A/:id      index_spec.rb   # GET /resource_A/      create_spec.rb  # GET /resource_A/new and POST /resource_A      edit_spec.rb    # GET /resource_A/:id/edit and PATCH /resource_A/:id      destroy_spec.rb # DELETE /resource_A/:id

Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>