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

时间:2015-02-01 17:19:14

标签: ruby-on-rails ruby ruby-on-rails-4 rspec

我已经决定回到Rails的基础知识,并且我正在通过思想机器人的在线课程进行工作。

以下是测试:

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合作过多,但据我所知,应用程序正在完成测试所需的工作。任何帮助都会受到赞赏,因为我现在有些困惑。

1 个答案:

答案 0 :(得分:0)

你有,

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

你应该:

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