我跟着RailsSpace: Building a Social Networking Website with Ruby on Rails by Michael Hartl。滑轨v2.3.2。

我已经到了介绍测试的第5章。以下内容应该使用get方法将各个页面的标题与字符串匹配:

require File.dirname(__FILE__) + '/../test_helper'
require 'site_controller'

    # Re-raise errors caught by the controller.
    class SiteController; def rescue_action(e) raise e end; end

    class SiteControllerTest < Test::Unit::TestCase
      def setup
        @controller = SiteController.new
        @request     = ActionController::TestRequest.new
        @response   = ActionController::TestResponse.new
      end

      def test_index
        get :index
        title = assigns(:title)
        assert_equal "Welcome to RailsSpace!", title
        assert_response :success
        assert_template "index"
        end

      def test_about
        get :title
        title = assigns(:title)
        assert_equal "About RailsSpace", title
        assert_response :success
        assert_template "about"
      end

      def test_help
        get :help
        title = assigns(:title)
        assert_equal "RailsSpace Help", title
        assert_response :success
        assert_template "help"
      end
    end


在编译时,我得到:

Loaded suite site_controller_test
Started
EEE
Finished in 0.057 seconds.

  1) Error:
test_about(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b30>
    site_controller_test.rb:23:in `test_about'

  2) Error:
test_help(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b1c>
    site_controller_test.rb:31:in `test_help'

  3) Error:
test_index(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x485470c>
    site_controller_test.rb:15:in `test_index'

3 tests, 0 assertions, 0 failures, 3 errors


其他人有this issue,唯一建议的解决方案是重新安装。我不喜欢这个。由于这是一本较旧的书,所以这可能只是rails版本之间的损坏。对于Rails v2.3.2,这相当于什么?

最佳答案

替换以下所有代码

# Re-raise errors caught by the controller.
class SiteController; def rescue_action(e) raise e end; end

class SiteControllerTest < Test::Unit::TestCase
  def setup
    @controller = SiteController.new
    @request     = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end




class SiteControllerTest < ActionController::TestCase


您使用的代码是指Rails 2.0 / 2.1。

08-25 19:03