当我使用 minitest-rails gem 运行非常简单的测试时,我遇到了错误。 我有 rails 4.1.5 和 minitest 5.4.0
rake 测试: Controller
1) 错误:
DashboardController::index Action #test_0001_anonymous:
NoMethodError:未定义的方法 get' for #<#<Class:0x00000008e28170>:0x00000008eeb9b8>
test/controllers/dashboard_controller_test.rb:6: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
我的测试助手:
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>
我得到的错误与你的不同。您在 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 规范 DSL 所需要做的全部工作。其次,它将context
的别名添加到ActiveSupport::TestCase
上的describe
,这是所有rails 测试的基础。如果你想添加不继承自 ActiveSupport::TestCase
的测试,那么你也可以在 Minitest::Spec
上为其添加别名,但这对你使用 没有帮助> Controller 测试中的上下文
。
还在吗?好的。那么,为什么您的代码给您的错误与我的不同?可能用于 Controller 测试的测试对象不是 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
如果第一个断言失败,那么您就知道您在配置中做错了什么。
关于ruby-on-rails - Minitest-NoMethodError : undefined method `get' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768615/