1

Rails newbはこちら。

インデックス ルートの 200 ステータス コードを RSpec テストしようとしています。

私のindex_controller_spec.rb で

require 'spec_helper'

describe IndexController do

    it "should return a 200 status code" do
    get root_path
    response.status.should be(200)
  end

end

ルート.rb:

Tat::Application.routes.draw do

    root to: "index#page"

end

インデックスコントローラー:

class IndexController < ApplicationController

    def page
    end

end

ブラウザでアクセスするとすべて問題ありませんが、RSpec コマンドラインでエラーが発生します

IndexController should return a 200 status code
     Failure/Error: get '/'
     ActionController::RoutingError:
       No route matches {:controller=>"index", :action=>"/"}
     # ./spec/controllers/index_controller_spec.rb:6:in `block (2 levels) in <top (required)>

'

理解できない?!

ありがとう。

4

1 に答える 1

3

Railsの世界へようこそ!テストにはさまざまな種類があります。コントローラー テストとルーティング テストを混同しているようです。

root_pathが を返しているため、このエラーが表示されます/。RSpecget :actionコントローラ内のテストは、そのコントローラでそのメソッドを呼び出すことを意図しています。

エラーメッセージに気付いた場合は、次のように表示されます:action => '/'

コントローラーをテストするには、テストを次のように変更します。

require 'spec_helper'

describe IndexController do
  it "should return a 200 status code" do
    get :page
    response.status.should be(200)
  end
end

ルーティング テストに興味がある場合は、https://www.relishapp.com/rspec/rspec-rails/docs/routing-specsを参照してください 。例は次のとおりです。

{ :get => "/" }.
  should route_to(
    :controller => "index",
    :action => "page"
  )
于 2013-04-22T23:55:20.690 に答える