Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Rspec - Kiểm tra JSON schema dùng rspec matcher
Rspec - Kiểm tra JSON schema dùng rspec matcher
Nay mình có viết TDD để check Repsonse Body của API (ở JSON format) có khớp với mẫu định sẵn hay không. Thông thường thì mình sẽ dùng 1 vài thủ thuật để check response body có đủ các key
mà mình expect hay không, qua bài viết này mình tìm thấy 1 cách khác để kiểm tra: check schema của JSON.
JSON schema là 1 mẫu biểu diễn cấu trúc dữ liệu của chuỗi JSON. Có thể coi là 1 đặc tả kỹ thuật của JSON giúp cho việc tạo documentation, validation với JSON data dễ dàng hơn [xem rõ hơn ở đây]
Tích hợp vào rspec
Ứng dụng của mình đang dùng là Rails (4).
gem json-schema
là 1 JSON schema validator cho ruby. Add nó vào Gemfile:
group :test do
gem "json-schema"
end
Định nghĩa Rspec matcher ở spec/json_helper.rb
.
Đặt tên matcher là match_response_schema
require 'rspec/expectations'
# matcher này nhận biến vào là tên schema
RSpec::Matchers.define :match_response_schema do |schema|
match do |response|
# Định nghĩa đến đường dẫn chứa schema
schema_directory = "#{Dir.pwd}/spec/support/api/schemas"
schema_path = "#{schema_directory}/#{schema}.json"
# JSON::Validator.validate! được cung cấp bởi gem json-schema
# nó so sánh body của response có khớp với schema đã định nghĩa hay không
# truyền strict: true để đảm bảo hàm sẽ fail khi response body không matching với schema
JSON::Validator.validate!(schema_path, response.body, strict: true)
end
end
Các bạn có thể xem thêm cách viết matcher ở đây nhé
Viết JSON Schema.
Giả sử đây là dữ liệu mình mong muốn nhận được:
{
"domain": {
"name": "aekt.net",
"is_verified": true,
"dkim": {
"domain": "aekt.net",
"is_verified": true,
"selector": "default",
"txt_record": "v=DKIM1;t=s;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq5sDfCre1WJlaDnokb1rPaXvFYujQQLt0P8B/Nq6D5kKGW5WvusTUD6KrQSylmPOc436yKPGmMPVkDEuxLMuRyssQ//Czq+B7Cp1QDA2yhIvhdIMBZC/iwWwNkTB56ZGIouEI2NlV3vmxzncRHcVwbO8ore89YGsP2k7vw0OzTwIDAQAB"
}
}
}
Mình sẽ tạo 1 file chứa JSON schema theo đúng đường dẫnschema_path
ở trên, đặt tên schema này là domain
:
spec/support/api/schemas/domain.json:
{
"type": "object",
"required": [
"domain"
],
"properties": {
"domain" : {
"type" : "object",
"required" : [
"name",
"is_verified"
],
"properties" : {
"name" : {
"type" : "string"
},
"is_verified" : {
"type" : "boolean"
},
"dkim" : {
"type" : "object"
}
}
}
}
}
Nếu nhìn theo kiểu trên các bạn chưa hình dung ra các viết JSON schema thì mình có thể hướng dẫn ;)
Các giá trị của
type
xem ở đây
Tiếp theo mình sẽ include json_helper
vào spec test:
spec/api/domain_spec.rb:
require 'rails_helper'
require 'json_helper'
RSpec.describe 'Domain API' do
let(:domain) {
'aekt.net'
}
describe 'response' do
it 'code should be 200' do
get "/#{domain}", {}
expect(response).to have_http_status(200)
end
it 'body should match the schema' do
get "/#{domain}", {}
expect(response).to match_response_schema("domain")
end
end
end
end
Giờ có thể chạy bundle exec rspec
để kiểm tra.
lamdt 26-06-2016



