简体   繁体   English

使用json_spec gem的“在“路径”处应包括:“步骤未按我预期的那样工作

[英]Using json_spec gem's 'at “path” should include:' step is not working as I would expect

(apologies in advance for the cucumber steps, they need to be cleaned up a fair bit to flow better) (对于黄瓜步骤,请事先道歉,需要对其进行一定程度的清理以使其流动更好)

I am using a combination of Cucumber, along with the rest-client , and json_spec gems to create a test suite for a restful API. 我将Cucumber, rest-clientjson_spec gems结合使用来为Restful API创建测试套件。 The approach is similar to that given in the Cucumber Book Note that in this case, since the 'customer' is a developer, the 'language of the business' is far more technical than you would normally express in cucumber scenarios 该方法与Cucumber Book Note中给出的方法类似,在这种情况下,由于“客户”是开发人员,因此“业务的语言”比您在黄瓜方案中通常所表达的要技术得多。

I am having a problem with the json_spec cucumber step "Then the JSON at "path" should include:" 我在json_spec黄瓜步骤“然后在“路径”处的JSON应该包含:”时遇到问题

My scenario looks like this 我的情况看起来像这样

Scenario Outline:  GET to OR Packages for specific package uuid returns proper data
  Given I create a request body from the following JSON data
  """
  {
    "package":
    {
      "name": "anothertestpackage", 
      "description": "This is a test, testing 4 5 6", 
      "package_type" : <package_type>, 
      "duration": 30, 
      "start_date": "2012-03-01T08:00:00Z"
    }
  }
  """
  And I have the URI for a new: package made in: OR from the request body
  When I make a: GET request to the URI for: my new package with no query string
 Then the JSON at "package" should include:
  """
  {
    "name": "anothertestpackage", 
    "description": "This is a test, testing 4 5 6", 
    "package_type" : <package_type>, 
    "duration": 30, 
    "start_date": "2012-03-01T08:00:00Z"
  }
  """

  Examples:
    | package_type |
    | "IMPRESSIONS" |
    | "CLICKS" |
    | "LEADS" |

And the contents of last_json are like this at the point the Then step is executed 而last_json的内容在“然后”步骤执行时是这样的

{
  "package": {
    "status": "NEW",
    "account": {
      "resource_uri": "/api/v0001/accounts/fecdbb85a3584ca59820a321c3c2767d"
    },
    "name": "anothertestpackage",
    "package_type": "IMPRESSIONS",
    "margin_goal": "0.5000",
    "duration": 30,
    "resource_uri": "/api/v0001/packages/fecdbb85a3584ca59820a321c3c2767d/feea333776c9454c92edab8e73628cbd",
    "start_date": "2012-03-01T08:00:00Z",
    "description": "This is a test, testing 4 5 6"
  }
}

I should think the step would pass, but I'm getting this error instead 我应该认为这一步将会过去,但是我却收到了此错误

Expected included JSON at path "package" (RSpec::Expectations::ExpectationNotMetError)
features\OR\API\OR_API_Packages.feature:70:in `Then the JSON at "package" should include:'

It is unclear what that error is telling me in terms of what is wrong. 目前尚不清楚该错误告诉我什么是错误的。 Is this user error? 这是用户错误吗? should I be using a different means to determine if the expected key:value pairs are present in the JSON returned by the API? 我应该使用其他方法来确定API返回的JSON中是否存在预期的key:value对吗? I don't really see any examples of doing this kind of comparison in your feature files for the gem, so it is difficult to know if this is not what include was intended for. 在您的gem的功能文件中,我真的没有看到进行这种比较的任何示例,因此很难知道这是否不是针对此对象的。

Heh just got an answer from one of the gem authors via another venue. 嘿,刚刚从另一个宝石作家那里得到了一个答案。 Will post it here 将在这里发布

Include was intended more for simple value inclusion, mostly in arrays. Include主要用于简单的值包含,主要用于数组中。 If an API index response returned an array of objects, you could assert that the array includes one whole, identical object. 如果API索引响应返回了一个对象数组,则可以断言该数组包含一个完整的相同对象。 Check out the matcher specs for examples. 查看匹配器规格示例。

For what you're doing, I'd break it out into separate steps: 对于您正在做的事情,我将其分解为单独的步骤:

 Then the JSON at "package/name" should be "anothertestpackage" And the JSON at "package/description" should be "This is a test, testing 4 5 6" And the JSON at "package/package_type" should be <package_type> And the JSON at "package/duration" should be 30 And the JSON at "package/start_date" should be "2012-03-01T08:00:00Z" 

Or you could make use of a table format to make that more succinct such as 或者,您可以使用表格格式来简化代码,例如

 Then the JSON at "package" should have the following: | name | "anothertestpackage" | | description | "This is a test, testing 4 5 6" | | package_type | <package_type> | | duration | 30 | | start_date | "2012-03-01T08:00:00Z" | 

I hope that helps! 希望对您有所帮助! Thanks for the question. 谢谢你的问题。

Indeed it did help very much, thank you 'laserlemon' 的确确实有很大帮助,谢谢“ laserlemon”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM