2012-06-20 5 views
0

REST API 테스트 (이 경우 Sinatra 및 Rspec 사용)에 대한 우수 사례를 알고 싶습니다. 분명한 문제는 사용자 목록에 대해 GET /users을 검사하는 테스트가있는 경우 사용자 생성, 테스트 실행, 사용자 삭제 단계를 거쳐야한다는 것입니다. 그러나 작성/삭제 단계가 API에 종속적 인 경우 주문 기반 테스트 규칙을 위반하거나 한 테스트에서 여러 항목을 테스트해야합니다 (예 : 사용자를 추가 했습니까?) GET /users 사용자를 반환합니까? list? .. 사용자를 삭제 했습니까?).Sinatra REST API 방법론 테스트

답변

0

FactoryGirl을 사용할 수 있습니다. 테스트에서 API를 통해 사용자를 만들거나 FG를 사용하여 스텁을 만들고 삭제하고 수정하는 등의 작업을 할 수 있습니다. FG는 매우 유연한 ORM 테스트 도우미로 이러한 종류의 작업에 효과적입니다.

0

나는 또한 @three에 동의합니다 - FactoryGirl을 사용하십시오! 예를 들어 (첫째, 샘플 객체를 정의)으로

:

FactoryGirl.define do 

    sequence(:random_ranking) do |n| 
     @random_rankings ||= (1..10000).to_a.shuffle 
     @random_rankings[n] 
    end 

    factory :todo do 
     title { Faker::Lorem.sentence} 
     id { FactoryGirl.generate(:random_ranking) } 
     completed [true, false].sample 
     completed_at Time.new 
     created_at Time.new 
     updated_at Time.new 
    end 

end 

그리고 당신의 사양 시험에서, 목록의 작업을 설명 :

describe 'GET #index' do 

    before do 

     @todos = FactoryGirl.create_list(:todo, 10) 

     @todos.each do |todo| 
     todo.should be_valid 
     end 

     get :index, :format => :json 

    end 


    it 'response should be OK' do 
     response.status.should eq(200) 
    end 

    it 'response should return the same json objects list' do 

     response_result = JSON.parse(response.body) 

     # these should do the same 
     # response_result.should =~ JSON.parse(@todos.to_json) 
     response_result.should match_array(JSON.parse(@todos.to_json)) 

    end 

end