2016-04-07 1 views
0

私はgrape apiをテストしようとしています。テストに問題があります。ブドウのAPIをテストする。 ActionController :: TestCase @controllerはゼロです

私はレールのデフォルトテストを使用します。これは私のGemfileテストパートです。

group :development, :test do 
    gem 'sqlite3' 
    gem 'byebug' # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'spring' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
    gem 'factory_girl_rails' 
    gem 'capybara' 
    gem 'selenium-webdriver' 
    gem 'capybara-angular' 
    gem 'poltergeist' 
    gem 'phantomjs', :require => 'phantomjs/poltergeist', platforms: :ruby # linux 
end 

マイコントローラ:

# app/controllers/api/v1/vehicules.rb 
module API 
    module V1 
    class Vehicules < Grape::API 

と私のテスト:

1) Error: 
API::V1::VehiculesTest#test_GET_/api/v1/vehicules?user_id=123: 
RuntimeError: @controller is nil: make sure you set it in your test's setup meth 
od. 
    test/controllers/api/v1/vehicules_test.rb:7:in `block in <class:VehiculesTes 
t>' 

私はできませんでした:私は、テストを起動すると、私はこのエラーを得た

# test/controllers/api/v1/vehicules_test.rb 
require "test_helper" 

class API::V1::VehiculesTest < ActionController::TestCase 
    @controller = API::V1::VehiculesTest.new 

    test "GET /api/v1/vehicules?user_id=123" do 
    get("/api/v1/vehicules?user_id=123") 
    assert_response :success 
    json_response = JSON.parse(response.body) 
    assert_not(json_response['principal'], "principal devrait être faux") 
    end 

    test "PUT /api/v1/vehicules?user_id=123" do 
    put("/api/v1/vehicules?user_id=123", { 'princiapl' => true }, :format => "json") 
    assert_response :success 
    json_response = JSON.parse(response.body) 
    assert_not(json_response['principal'], "principal devrait être vrais") 
    end 

end 

controllerが見当たらない理由を確認してください。 test_helper.rbに何かを追加する必要がありますか?

class ActionController::TestCase 

end 

答えて

2

あなたは、通常のRailsコントローラのテストが、ブドウのクラスのためにそのわずかに異なるようにテストを設定している:それはActionControllerには何も入っていません。

ActionController::TestCaseから継承する代わりに、ActiveSupport::TestCaseから継承してから、Rackテストヘルパを追加する必要があります。

class API::V1::VehiclesTest < ActiveSupport::TestCase 
    include Rack::Test::Methods 

    def app 
    Rails.application 
    end 

    test "GET /api/v1/vehicules?user_id=123" do 
    get("/api/v1/vehicules?user_id=123") 

    assert last_response.ok? 

    assert_not(JSON.parse(last_response.body)['principal'], "principal devrait être faux") 
    end 

    # your tests 
end 

は、さらなる詳細についてhttps://github.com/ruby-grape/grape#minitest-1https://github.com/brynary/rack-testを参照してください。

関連する問題