简体   繁体   中英

RSPEC - comment out controller action, test still passing? Ruby on Rails

I am adding tests to an already existing application. Here is my controller:

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]

   def index
     if params[:filter]
       @jobs = Job.order(applied_on: :asc)
     else
       @jobs = Job.all
     end
   end
end

Here is my spec file:

require 'rails_helper'

RSpec.describe JobsController, type: :controller do
  describe "#index" do 

    it "should render index template" do 
       get(:index)
       expect(response).to render_template(:index)
    end

  end
end

If I comment out the entire index action, and run the test it still passes? Why is this?

Other scenerios I tried:

Commenting out the routes resources :jobs makes the test fail

How can i fix this issue? It is clear that the test is passing regardless of it testing and that the proper thing is not being tested here.

This is part of the Rails 'magic' of convention over configuration.

If you have a route that points to a controller action, Rails will first try to locate the action on the controller. In your case, it will look for an index action.

If it doesn't find it, all is not lost. Rails assumes you left it out on purpose because it was blank and that there is no particular logic needed to render the corresponding view. Going off this default assumption, it will look for a matching template in the controller's view directory ( index.html.erb , for example) and if it finds one with a name matching the controller action it will render it.

This is a sensible default. Sometimes controller actions are just empty methods because they don't assign any instance variables to the view. In these cases, Rails makes the controller method (action) totally optional which means there is less overhead to wiring a new view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM