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

roo*_*tar 0 ruby testing rspec ruby-on-rails minitest

当我使用minitest-rails gem运行非常简单的测试时,我陷入了错误。我有Rails 4.1.5和minitest 5.4.0

抽水测试:控制器

1)错误:DashboardController :: index action#test_0001_anonymous:NoMethodError:“中的未定义方法get' for #<#<Class:0x00000008e28170>:0x00000008eeb9b8> test/controllers/dashboard_controller_test.rb:6:in块(3个级别)

测试:

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
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

blo*_*age 5

您所做的事情有很多错误。据我了解,您想在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>
Run Code Online (Sandbox Code Playgroud)

我得到的错误不同于您的错误。您化名方法contextdescribe在你的test_helper.rb文件,但不幸的是你的别名的对象不是继承链中的轨测试对象。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
Run Code Online (Sandbox Code Playgroud)

这是test_helper.rb具有两个更改的标准。首先,它需要minitest-rails和minitest-rails-capybara。这是您需要做的所有事情,以便在rails测试中启用Minitest spec DSL。其次,它将on 的别名添加contextdescribeon ActiveSupport::TestCase,这是所有rails测试的基础。如果要添加不从其继承的测试,ActiveSupport::TestCase则还可以在上对它进行别名化Minitest::Spec,但这不会帮助您context在控制器测试中使用。

还在?好的。那么,为什么您的代码给您的错误不同于我的错误?用于控制器测试的测试对象可能不是ActionController::TestCase。我说这是因为您的错误是undefined method get。该get方法是已ActionController::TestCase定义的东西,而不是on 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
Run Code Online (Sandbox Code Playgroud)

如果第一个断言失败,则说明您在配置中做错了什么。