繁体   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