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

时间:2014-09-10 14:52:40

标签: ruby-on-rails ruby testing rspec minitest

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

rake test:控制器

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

我的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

1 个答案:

答案 0 :(得分:4)

你做的事情有很多不妥之处。据我了解你想在你的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文件中将方法describe别名为test_helper.rb,但不幸的是,您别名的对象不在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规范DSL所需要做的全部工作。其次,它在context上将describe的别名添加到ActiveSupport::TestCase,这是所有rails测试的基础。如果您要添加不从ActiveSupport::TestCase继承的测试,那么您也可以在Minitest::Spec上对其进行别名,但这不会帮助您在控制器测试中使用context

还在吗?好的。那么为什么你的代码会给你一个与我不同的错误呢?可能用于控制器测试的测试对象不是ActionController::TestCase。我这样说是因为你的错误是undefined method getget方法是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

如果第一个断言失败,那么您就知道在配置中做错了。