ActionController :: RoutingError:未初始化的常量GenresController

时间:2015-07-27 19:09:46

标签: ruby-on-rails ruby-on-rails-4 model-view-controller integration-testing rails-api

目前正在关注Code School的Soup to Bits: Surviving APIs with Rails教程。

在实施新的Genre模型时,我首先遇到以下错误:

11:56:18 - INFO - Running: test/integration/listing_genres_test.rb
Started

ERROR["test_lists_genres", ListingGenresTest, 2015-07-26 18:26:25 -0700]
 test_lists_genres#ListingGenresTest (1437960385.93s)
ActionController::RoutingError:         ActionController::RoutingError: No route matches [GET] "/genres"
            test/integration/listing_genres_test.rb:5:in `block in <class:ListingGenresTest>'
        test/integration/listing_genres_test.rb:5:in `block in <class:ListingGenresTest>'

  1/1: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.06051s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

因此,我在resources :genres文件中创建了config/routes.rb来修复它,这导致了一个新错误:

11:59:45 - INFO - Running: all tests
Started

ERROR["test_lists_genres", ListingGenresTest, 2015-07-26 18:26:26 -0700]
 test_lists_genres#ListingGenresTest (1437960386.04s)
ActionController::RoutingError:         ActionController::RoutingError: uninitialized constant GenresController
            test/integration/listing_genres_test.rb:5:in `block in <class:ListingGenresTest>'
        test/integration/listing_genres_test.rb:5:in `block in <class:ListingGenresTest>'

  8/8: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.23204s
8 tests, 23 assertions, 0 failures, 1 errors, 0 skips

然而,在本教程中,测试通过了。

以下是listing_genres_test.rb文件:

require 'test_helper'

class ListingGenresTest < ActionDispatch::IntegrationTest
  test 'lists genres' do
    get '/genres'

    assert_equal 200, response.status
    assert_equal Mime::json, response.content_type
  end
end

这是genres_controller.rb文件:

class GenresController < ApplicationController
  def index
    render json: Genre.all, status: 200
  end
end

我做错了什么?

0 个答案:

没有答案