Rails 4 - 将根路由路由到索引操作 - Rspec 测试失败

Rails 4 - Routing the root route to an index action - Rspec Tests Failing

我决定稍微回到 Rails 的基础知识,我正在学习 Thoughtbot 的 upcase 在线课程。

测试如下:

describe "routes" do
  it "routes '/' to the index action of the pages controller" do
    expect(get: "/").to route_to(controller: "pages", action: "index")
  end
end

describe "root request" do
  it "shows a welcome message" do
    get "/"
    expect(response.body).to include("Welcome to My Guestbook")
  end
end

describe PagesController do
  describe "#index" do
    it "renders the welcome page" do
      get :index
      expect(response).to render_template("welcome")
    end
  end
end

这是 Rspec 的输出:

Failures:

  1) PagesController#index renders the welcome page
     Failure/Error: get :index
     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"pages"}
     # ./spec/controllers/pages_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

  2) routes routes '/' to the index action of the pages controller
     Failure/Error: expect(get: "/").to route_to(controller: "pages", action: "index")
       The recognized options <{"controller"=>"pages", "action"=>"welcome"}> did not match <{"controller"=>"pages", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"controller"=>"pages", "action"=>"index"}
       +{"controller"=>"pages", "action"=>"welcome"}
     # ./spec/routing/routes_spec.rb:5:in `block (2 levels) in <top (required)>'

我的路线:

Rails.application.routes.draw do
  root 'pages#welcome'
end

我的控制器:

class PagesController < ApplicationController

  def index
    render 'pages/welcome'
  end

end

我使用 Rspec 的次数不多,但据我所知,应用程序正在做测试想要它做的事情。任何帮助将不胜感激,因为我现在有些困惑。

你有,

Rails.application.routes.draw do
  root 'pages#welcome'
end

你应该有:

Rails.application.routes.draw do
  root 'pages#index'
end