2014-09-10 52 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 

如果第一個斷言失敗,那麼你知道你做錯了什麼配置。

相關問題