[英]Undefined method `get' in Minitest
我正在嘗試使用MiniTest為我的RESTful API創建功能測試:require'test_helper'
class AutomaticTests < ActiveSupport::TestCase
test "test1" do
get '/'
end
end
上面的代碼觸發錯誤:
Minitest :: UnexpectedError:NoMethodError:未定義的方法get
我的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來自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
寶石 。
您從錯誤的測試類繼承。 您要使用ActiveDispatch :: IntegrationTest而不是ActiveSupport :: TestCase。
class AutomaticTests < ActiveDispatch::IntegrationTest
test "test1" do
get '/'
end
end
有關rails提供的測試類的更多信息,請參見測試指南 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.