2016-06-20 4 views
0

ActiveModelSerializer에서 추가 된 객체의 속성 + 속성으로 응답하는 엔드 포인트가 있습니다. 응답에 키가 있는지 확인하는 테스트를 작성하려고합니다.API 응답에 RSpec 키가 있음을 테스트하는 방법

하자 가정 해 개체 (예를 들어 나무가) 내가 제대로이 테스트를 작성하려면 어떻게해야이 키

expected_tree_attributes = [:height, :age, :color]

을 가지고 있다고? 쓸 수 있습니까?

subject { post :obtain_tree_info, { id: tree.id } } 

response = JSON.parse(subject.body) 
expected(response).to include(*expected_tree_attributes) 

해당 사항 ... 허용할만한?

답변

0

당신이 할 수있는 이들과 함께 rspec-api-matchers gem

또는 airborne gem

를 사용하는 것을 고려하십시오 :

# api_matchers 
response = JSON.parse(subject.body) 
expect(response).to be_success 
expect(response).to have_json_node(:height).with(tree.height) 
expect(response).to have_json_node(:age).with(tree.age) 
expect(response).to have_json_node(:color).with(tree.color) 

# or 

expect(response).to have_json_node(:age).with("123") 

공수

describe 'sample spec' do 
    it 'should validate types' do 
    post '/api/v1/obtain_tree_info', {id: tree.id} 
    expect_json_types(height: :int, age: :int_or_null, color: :string) 
    end 
end 
관련 문제