簡體   English   中英

Minitest中未定義的方法“ get”

[英]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提供的測試類的更多信息,請參見測試指南

Rails Minitest 未定義方法 `greater_than_or_equal_to' for # <shoulda::matchers< div><div id="text_translate"><p> 我想在 Minitest 中使用 ShouldaMatchers gem 來檢查簡單的 model 驗證:</p><pre> class Portfolio &lt; ApplicationRecord validates:share_ratio, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } end</pre><p> 為此,我有一個 Minitest:</p><pre> require 'test_helper' class PortfolioTest &lt; ActiveSupport::TestCase context 'validations' do should validate_numericality_of(:share_ratio).greater_than_or_equal_to(0) should validate_numericality_of(:share_ratio).is_less_than_or_equal_to(100) end end</pre><p> 但我收到一個錯誤:</p><blockquote><p> class:PortfolioTest':#Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) 的未定義方法“greater_than_or_equal_to”</p></blockquote><pre> #Gemfile: gem 'shoulda', '~&gt; 4.0' gem 'shoulda-matchers', '~&gt; 4.0'</pre><p> 我嘗試更改 model 內部的驗證:</p><pre> validates_numericality_of:share_ratio, greater_than_or_equal_to: 0, less_than_or_equal_to: 100</pre><p> 但是錯誤是一樣的。 </p></div></shoulda::matchers<>

[英]Rails Minitest undefined method `greater_than_or_equal_to' for #<Shoulda::Matchers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 minitest:未定義的方法`get&#39; Minitest-NoMethodError:未定義的方法`get&#39; 使用minitest未定義方法“有效”測試設計模型 NoMethodError:minitest 和 mocha 的未定義方法“模擬” Rails Minitest:ActiveStorage NoMethodError:未定義的方法“下載” minitest gem出錯:NoMethodError:NoMethodError:nil的未定義方法`env&#39;:NilClass * 類Minitest :: Unit的未定義方法_run_suites(NameError) MiniTest中的undefined方法`link_to_remote&#39;用於application_helper測試 Rails Minitest ActionView :: Template :: Error:nil:NilClas的未定義方法 Rails Minitest 未定義方法 `greater_than_or_equal_to' for # <shoulda::matchers< div><div id="text_translate"><p> 我想在 Minitest 中使用 ShouldaMatchers gem 來檢查簡單的 model 驗證:</p><pre> class Portfolio &lt; ApplicationRecord validates:share_ratio, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } end</pre><p> 為此,我有一個 Minitest:</p><pre> require 'test_helper' class PortfolioTest &lt; ActiveSupport::TestCase context 'validations' do should validate_numericality_of(:share_ratio).greater_than_or_equal_to(0) should validate_numericality_of(:share_ratio).is_less_than_or_equal_to(100) end end</pre><p> 但我收到一個錯誤:</p><blockquote><p> class:PortfolioTest':#Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) 的未定義方法“greater_than_or_equal_to”</p></blockquote><pre> #Gemfile: gem 'shoulda', '~&gt; 4.0' gem 'shoulda-matchers', '~&gt; 4.0'</pre><p> 我嘗試更改 model 內部的驗證:</p><pre> validates_numericality_of:share_ratio, greater_than_or_equal_to: 0, less_than_or_equal_to: 100</pre><p> 但是錯誤是一樣的。 </p></div></shoulda::matchers<>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM