2014-10-11 2 views
2

내 포도 API에 프로그래밍 방식으로 요청을하기위한 테스트 환경을 정의하려고합니다. 포도 API에 대한 테스트를 정의하는 방법은 무엇입니까?

나는 /app/api/services/v1.rb에 주요 API 파일을 가지고 내가 테스트를 위해이 내용을 가진 파일이 사양 경로로 요청라는 폴더 생성 :

describe Services::V1 do 
    describe "GET /v1/users/:id" do 
    it "returns a user by id" do 
     user = User.create! 
     get "/api/users/#{user.id}" 
     expect(response.body).to eq user.to_json 
    end 
    end 
end 

을 API에 사용자 자원에 대해 정의 된 경로가 있지만 Servicesnamespace가 "초기화되지 않은 상수 서비스 (NameError)"라는 오류가 있습니다.

RSpec :: Rails :: RequestExampleGroup 유형 : : 요청을 추가했습니다. , file_path :/spec/api/spec_helper하지만 그 포도 문서와 관련된 오해 해요 생각합니다. 설정을 | |.

RSpec.configure 할 당신이 사양에서 다음/spec_helper.rb을 추가하여 사양에 따라 그 레이아웃을 일치시킬 수 있습니다 -

"당신은 응용 프로그램/API로 이동하기 위해 API 코드를 할 수 있습니다 config.include RSpec에 :: 레일 :: RequestExampleGroup 입력 : 요청, FILE_PATH : /스펙/API/끝 "

가지 작업을 할 어떤 도움에 감사드립니다. 나는이 기술 설정을 가진 초보자입니다.

답변

1

api 버전이 아닌 리소스를 테스트해야합니다.

describe "ExampleApi::V1::Regions" do 

    describe "GET /api/v1/regions/{id}" do 
    context "without data" do 
     it "should return a status code 404" do 
     get "/api/v1/regions/100000000", nil, { 'Authorization' => "Bearer #{token.token}"} 
     expect(response.status).to eq 404 
     end 

     it "should return an empty array" do 
     get "/api/v1/regions/100000000", nil, { 'Authorization' => "Bearer #{token.token}"} 
     expect(json['error']['code']).to eq(404) 
     end 
    end 

    context "with data" do 
     it 'returns a status code 200' do 
     get "/api/v1/regions", nil, { 'Authorization' => "Bearer #{token.token}"} 
     expect(response.status).to eq 200 
     end 

     it 'returns a region' do 
     region1 = Fabricate(:region) 
     get "/api/v1/regions/#{region1.id}", nil, { 'Authorization' => "Bearer #{token.token}"} 
     expect(json["id"]).to eq(region1.id) 
     end 
    end 
    end 

end 

이 예에서는 인증 (이 경우 Oauth2)을 사용하는 경우 Authorization 헤더가 사용됩니다. 당신이 rails_helper에 config.include RSpec::Rails::RequestExampleGroup, type: :request, file_path: /spec/api/를 추가하는 경우

# Just if the tests are in spec/api folder 
config.include RSpec::Rails::RequestExampleGroup, type: :request, file_path: /spec\/api/ 
0
  1. :

    module Requests 
        module JsonHelpers 
        def json 
         @json ||= JSON.parse(response.body) 
        end 
        end 
    end 
    

    rails_helper.rb 추가 기능에 : 도우미 또는 JSON도 있습니다

    , 당신은 지원 폴더에 파일을 만들 수 있습니다 .rb는 /spec/api/이 아니라 /spec/requests/에 API 사양을 넣어야 함을 의미합니다.

  2. 당신은 당신의 /spec/api/services/v1/regious_spec.rb

require 'rails_helper'를 추가해야
관련 문제