【问题标题】:Undefined method `get' in MinitestMinitest 中未定义的方法“get”
【发布时间】:2014-10-29 09:02:10
【问题描述】:

我正在尝试使用 MiniTest 为我的 RESTful API 创建功能测试: 需要'test_helper'

class AutomaticTests < ActiveSupport::TestCase
  test "test1" do
    get '/'
  end
end

上面的代码触发了一个错误:

Minitest::UnexpectedError: NoMethodError: undefined method `get' for

我的 test_helper.rb 看起来像这样:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails"

class ActiveSupport::TestCase
  fixtures :all

  class << self
    alias :context :describe
  end
end

如何使“get”方法起作用?

【问题讨论】:

    标签: ruby-on-rails ruby minitest


    【解决方案1】:

    Get 来自 rack/test,因此您需要将该 gem 添加到您的 Gemfile:

    gem 'rack-test', group: :test
    

    然后,在您的测试文件或test_helper.rb 中,添加以下行:

    require "rack/test"
    

    ...和...

    class ActiveSupport::TestCase
      include Rack::Test::Methods
    end
    

    您也可以使用rack-minitest gem。

    【讨论】:

    • 谢谢,这个错误已经消失了,虽然我现在得到一个更奇怪的错误:undefined local variable or method `app' for #<0x000000031080f8>
    【解决方案2】:

    您从错误的测试类继承。你想使用 ActiveDispatch::IntegrationTest 而不是 ActiveSupport::TestCase。

    class AutomaticTests < ActiveDispatch::IntegrationTest
      test "test1" do
        get '/'
      end
    end
    

    有关 rails 提供的测试类的更多信息,请参阅testing guide。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多