2014-09-10 65 views
0

当我用minitest-rails gem运行非常简单的测试时,我被卡住了错误。 我有轨4.1.5和5.4.0 MINITESTMinitest - NoMethodError:undefined方法'get'

耙测试:控制器

1)错误: DashboardController ::索引动作#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 
+0

看起来像你需要[this](http://stackoverflow.com/questions/11667552/minitest-undefined-method-get) – dddd1919 2014-09-11 03:01:23

回答

4

你在做什么有很多错误。据我了解,你想在你的Rails测试中使用Minitest的spec 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文件中混淆了contextdescribe的方法,但不幸的是,别名的对象不在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导轨和minitest导轨水豚的要求。这就是为了在您的Rails测试中启用Minitest规范DSL所需要做的。其次,它将context的别名添加到ActiveSupport::TestCasedescribe,这是所有轨道测试的基础。如果您想要添加不从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 

如果第一个断言失败,那么你知道你做错了什么配置。