const关键字在Rspec json_matchers中不起作用

时间:2019-03-06 15:27:49

标签: json rspec-rails

为什么const不能使用enum关键字?

使用enum,rspec成功失败:

{
  "id": "https://json-schema.org/geo",
  "$schema": "https://json-schema.org/draft-06/schema#",
  "description": "unauthorized authentication",
  "type": "object",
  "required": ["error"],
  "properties": {
    "error": { "enum": ["Xinvalid credentials"] }
  }
}

Failure/Error: expect(response).to match_json_schema('api/v1/user/unauthorized_authentication')

   #/error: failed schema #/properties/error: Invalid credentials is not a member of ["Xinvalid credentials"].

   ---

   expected

   {
     "error": "Invalid credentials"
   }

   to match schema "api/v1/user/unauthorized_authentication":

   {
     "id": "https://json-schema.org/geo",
     "$schema": "https://json-schema.org/draft-06/schema#",
     "description": "unauthorized authentication",
     "type": "object",
     "required": [
       "error"
     ],
     "properties": {
       "error": {
         "enum": [
           "Xinvalid credentials"
         ]
       }
     }
   }

 # ./spec/requests/api/v1/users/authenticate_spec.rb:34:in `block (3 levels) in <main>'

如果我将此enum更改为const(或constant)(不带括号)不会失败,为什么?

{
  "id": "https://json-schema.org/geo",
  "$schema": "https://json-schema.org/draft-06/schema#",
  "description": "unauthorized authentication",
  "type": "object",
  "required": ["error"],
  "properties": {
    "error": { "const": "Xinvalid credentials" }
  }
}

测试还可以,但是根据docs应该失败

[POST] /api/v1/users/authenticate
  User tries to authenticate with invalid redentials
    returns an invalid credentials message
    behaves like unauthorized endpoint
      returns 403 http status code
      behaves like json header
        returns content-type: application/json header

Finished in 0.35215 seconds (files took 4.13 seconds to load)
3 examples, 0 failures

谢谢

0 个答案:

没有答案