简体   繁体   English

RSPEC-注释掉控制器动作,测试是否仍通过? Ruby on Rails

[英]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 注释掉路由resources :jobs使测试失败

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. 这是Rails约定优于配置的“魔术”的一部分。

If you have a route that points to a controller action, Rails will first try to locate the action on the controller. 如果您有指向控制器动作的路由,Rails将首先尝试在控制器上找到该动作。 In your case, it will look for an index action. 在您的情况下,它将寻找index动作。

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. Rails假设您故意将其遗漏,因为它是空白的,并且不需要特殊的逻辑即可呈现相应的视图。 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. 按照此默认假设,它将在控制器的视图目录(例如index.html.erb )中查找匹配的模板,如果找到名称与控制器操作匹配的模板,则将呈现该模板。

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. 在这些情况下,Rails使控制器方法(动作)完全可选,这意味着连接新视图的开销更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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