2014-09-10 2 views
0

minitest-rails gem으로 매우 간단한 테스트를 실행할 때 오류가 발생했습니다. 내가 레일 4.1.5 및 minitest 5.4.0Minitest - NoMethodError : 정의되지 않은 메소드`get '

레이크 시험이 : 컨트롤러

1) 오류 : DashboardController :: index 액션 번호의 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

답변

4

많은 일들이 잘못되었습니다. 내가 알기로 레일 테스트에서 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 파일에서 contextdescribe으로 앨리어싱했지만 불행히도 앨리어싱 된 객체는 레일 테스트 객체의 상속 체인에 없습니다. 레일 테스트 개체는 Minitest::Spec::DSL까지 확장되지만 Minitest::Spec에서 상속되지 않습니다. 따라서 제공 한 코드가 실제로 제시 한 결과를 산출하고 있음을 강력하게 의심합니다.

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이다 : 그것은 여기에 테스트를 실행합니다 내 test_helper.rb의 코드입니다 말했다. 첫째, 최소한의 레일과 최소의 레일 - 카피 바라에 대한 요구가 있습니다. 레일 테스트에서 Minitest 사양 DSL을 사용하려면 이렇게해야합니다. 둘째, 의 별칭을 ActiveSupport::TestCasedescribe에 추가합니다.이 모든 값은 모든 레일 테스트의 기초입니다. 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 

첫 번째 어설 션이 실패하면 구성에 문제가 있음을 알게됩니다.

관련 문제