簡體   English   中英

Minitest-NoMethodError:未定義的方法`get'

[英]Minitest - NoMethodError: undefined method `get'

當我使用minitest-rails gem運行非常簡單的測試時,我陷入了錯誤。 我有Rails 4.1.5和minitest 5.4.0

抽水測試:控制器

1)錯誤:DashboardController :: index action#test_0001_anonymous:NoMethodError:undefined method get'for get' for #<#<Class:0x00000008e28170>:0x00000008eeb9b8> test/controllers/dashboard_controller_test.rb:6:in block(3 level)in'

測試:

require "test_helper"

describe DashboardController do
  context "index action" do
    before do
      get :index
    end
    it { must_respond_with :success }
    it "must render index view" do
      must_render_template :index
    end
  end
end

我的test_helper:

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


class MiniTest::Spec
  class << self
    alias :context :describe
  end
end


class RequestTest < MiniTest::Spec
  include Rails.application.routes.url_helpers

  register_spec_type(/request$/, self)
end


class ActionDispatch::IntegrationTest
  # Register "request" tests to be handled by IntegrationTest
  register_spec_type(/Request( ?Test)?\z/i, self)
end

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!

  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
  extend MiniTest::Spec::DSL
end

您所做的事情有很多錯誤。 據我了解,您想在Rails測試中使用Minitest的DSL規范,對嗎? 看來您在做不需要完成的事情。 我不明白為什么test_helper.rb文件中的一半代碼在那里。 我還懷疑您還有其他代碼在執行未顯示的操作。

這是我為重現您的設置所做的工作:

$ echo "Creating a new Rails app"
☣ [rails41:rails41] $ rails new undefined_get
☣ [rails41:rails41] $ cd undefined_get/
$ echo "Generate a Dashboard controller"
$ rails g controller dashboard index
$ echo "Add minitest-rails dependencies"
$ echo 'gem "minitest-rails"' >> Gemfile
$ echo 'gem "minitest-rails-capybara"' >> Gemfile
$ bundle install
$ echo "The test runs fine now:"
$ rake test
Run options: --seed 47210

# Running:

.

Finished in 0.457972s, 2.1835 runs/s, 2.1835 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
$ echo "Update to your test code and test_helper code"
$ echo "Use whatever editor you want. Not shown here."
$ echo "Now rerun the tests:"
$ rake test
rake aborted!
NoMethodError: undefined method `context' for #<Class:0x007f860258ae50>

我得到的錯誤不同於您的錯誤。 您對方法context進行了別名化,以在test_helper.rb文件context進行describe ,但是不幸的是,您別名的對象不在Rails測試對象的繼承鏈中。 rails測試對象擴展了Minitest::Spec::DSL ,但是它們不繼承自Minitest::Spec 因此,我非常懷疑您提供的代碼確實在產生您提供的結果。 就是說,這是我的test_helper.rb中的代碼,它將運行您的測試:

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

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Allow context to be used like describe
  class << self
    alias :context :describe
  end

  # Add more helper methods to be used by all tests here...
end

這是標准的test_helper.rb有兩個更改。 首先,它需要minitest-rails和minitest-rails-capybara。 這是您需要做的所有事情,以便在rails測試中啟用Minitest spec DSL。 其次,它為context添加別名以在ActiveSupport::TestCase上進行describe ,這是所有rails測試的基礎。 如果要添加不繼承自ActiveSupport::TestCase測試,則還可以在Minitest::Spec上為其添加別名,但這不會幫助您在控制器測試中使用context

還在? 好的。 那么,為什么您的代碼給您的錯誤不同於我的錯誤? 用於控制器測試的測試對象可能不是ActionController::TestCase 我說這是因為您的錯誤是undefined method get get方法是ActionController::TestCase定義的,並且不在Minitest::Spec 因此,您以某種方式弄亂了Minitest配置。 確保測試使用正確的測試對象的一種簡單方法是在測試中添加其他斷言。 像這樣:

require "test_helper"

describe DashboardController do
  context "index action" do
    before do
      # Make sure we are using the correct test class
      self.class.ancestors.must_include ActionController::TestCase
      # Continue with setup
      get :index
    end
    it { must_respond_with :success }
    it "must render index view" do
      must_render_template :index
    end
  end
end

如果第一個斷言失敗,則說明您在配置中做錯了什么。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM