如果你没有数组,你如何在“json_spec”gem中使用路径?

问题描述:

我在Rails应用程序中使用“json_spec”gem,我试图创建一些Cucumber步骤来记录/测试我的API代码。如果你没有数组,你如何在“json_spec”gem中使用路径?

我有以下情形:

Scenario: Create new authorization 
    Given an existing user and merchant 
    When I post to "/api/v1/authorizations.json" with: 
    """ 
    {"email":"[email protected]", "code": "43434", "merchant_account_id":1, "amount": 45.00} 
    """ 
    And I keep the JSON response at "authorization/id" as "AUTH_ID" 
    Then the JSON response should be: 
    """ 
    {"authorization":{"id":1,"email":"[email protected]","amount":45.0}} 
    """ 

我的预期“授权/ ID”给我的“ID”键的值,在“授权”哈希值。 json_spec文档中的所有示例都包含数组,但在这种情况下,响应只涉及一个实体 - 在JSON中有一个数组没有任何意义。没有数组时,有没有办法使用路径?

当然,你使用的语法是正确的。文档示例中的场景使用对帖子的响应,然后检查该帖子是否为集合的一部分 - 因此是数组。但是,如果您要检查例如您的帖子是否为最后一篇文章(您只需检索一个值),则可以使用散列语法:

Scenario: Create new authorization 
    Given an existing user and merchant 
    And I post to "/api/v1/authorizations.json" with: 
    """ 
    {"email":"[email protected]", "code": "43434", "merchant_account_id":1, "amount": 45.00} 
    """ 
    And I keep the JSON response at "authorization/id" as "AUTH_ID" 
    When I get from "/api/v1/authorizations/last.json" 
    Then the JSON response at "authorization/id" should be %{AUTH_ID}