在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:thoughtbot/json_matchers开源软件地址:https://github.com/thoughtbot/json_matchers开源编程语言:Ruby 100.0%开源软件介绍:JsonMatchersValidate the JSON returned by your Rails JSON APIs InstallationAdd this line to your application's group :test do
gem "json_matchers"
end And then execute:
Or install it yourself as:
UsageInspired by Validating JSON Schemas with an RSpec Matcher. First, configure it in your test suite's helper file: ConfigureRSpec
require "json_matchers/rspec"
JsonMatchers.schema_root = "spec/support/api/schemas" Minitest
require "minitest/autorun"
require "json_matchers/minitest/assertions"
JsonMatchers.schema_root = "test/support/api/schemas"
Minitest::Test.include(JsonMatchers::Minitest::Assertions) DeclareDeclare your JSON Schema in the schema directory.
Define your JSON Schema in the schema directory. {
"id": "https://json-schema.org/geo",
"$schema": "https://json-schema.org/draft-06/schema#",
"description": "A geographical coordinate",
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
}
}
} ValidateRSpecValidate a JSON response, a Hash, or a String against a JSON Schema with
describe "GET /locations" do
it "returns Locations" do
get locations_path, format: :json
expect(response.status).to eq 200
expect(response).to match_json_schema("locations")
end
end MinitestValidate a JSON response, a Hash, or a String against a JSON Schema with
def test_GET_posts_returns_Locations
get locations_path, format: :json
assert_equal response.status, 200
assert_matches_json_schema response, "locations"
end Embedding other SchemasTo re-use other schema definitions, include First, declare the singular version of your schema.
{
"id": "file:/user.json#",
"type": "object",
"required": ["id"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"address": { "type": "string" },
},
} Then, when you declare your collection schema, reference your singular schemas.
{
"id": "file:/users/index.json#",
"type": "object",
"definitions": {
"users": {
"description": "A collection of users",
"example": [{ "id": "1" }],
"type": "array",
"items": {
"$ref": "file:/user.json#"
},
},
},
"required": ["users"],
"properties": {
"users": {
"$ref": "#/definitions/users"
}
},
} NOTE: In this case To learn more about Declaring a schema in a SubdirectoryNesting a schema within a subdirectory is also supported:
{
"id": "https://json-schema.org/geo",
"$schema": "https://json-schema.org/draft-06/schema#",
"description": "A geographical coordinate",
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
}
}
}
describe "GET api/v1/locations" do
it "returns Locations" do
get locations_path, format: :json
expect(response.status).to eq 200
expect(response).to match_json_schema("api/v1/location")
end
end ConfigurationBy default, the schema directory is This can be configured via # spec/support/json_matchers.rb
JsonMatchers.schema_root = "docs/api/schemas"
Upgrading from |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论