부분 개체를 테이블 데이터에 일치 [중복]

Nov 20 2020

저는 현재 자체 개발 API 테스트의 대체품으로 Karate를 평가하고 있습니다. 다음과 같은 데이터를 반환하는 서비스가 있습니다.

{
  "items": [
    {
      "id": "1",
      "enabled": true,
      "foo": 1,
    },
    ...
  ],
  ...
}

각 항목의 속성은 서로 다른 기능을 수행하므로 별도로 테스트하고 싶습니다.

예를 들어 항목 활성화를 테스트하기 위해 enabled속성에 지정된 id.

나는 이것을 이렇게 설정하려고 시도했다;

Feature: Partial object matching
  Background:
    Given table items
    |id  |enabled|
    | '1'|true   |
    | '2'|true   |
    | '3'|false  |

  Scenario: match with all properties specified -- this passes
    * def response = { items: [ { id: '3', enabled: false }, { id: '1', enabled: true }, { id: '2', enabled: true } ] }
    * match $response.items contains only items Scenario: match with partial properties -- how can I make this pass (while also testing for something sensible)? * def response = { items: [ { id: '3', enabled: false, foo: 1 }, { id: '1', enabled: true, foo: 1 }, { id: '2', enabled: true, foo: 1 } ] } * match $response.items contains only items

실제 item객체는 더 많은 속성과 중첩 된 객체를 포함하는 상당히 뭉툭하며, 여러 다른 기능과 관련이 있고 일부 속성은 본질적으로 동적이므로 전체 예상 구조를 지정하지 않습니다.

match작업을 수행 하는 데 우아한 것이 있습니까 , 아니면 스크립팅에 의존해야합니까?

답변

2 HenrikGustafsson Nov 20 2020 at 16:12

이것은 옳은 일을하는 것 같습니다.

Feature: Partial object matching
  Background:
    Given def filterTableKeys = read('filterTableKeys.js')
    Given table items
    |id  |enabled|
    | '1'|true   |
    | '2'|true   |
    | '3'|false  |

  Scenario: match with all attributes
    * def response = { items: [ { id: '3', enabled: false }, { id: '1', enabled: true }, { id: '2', enabled: true } ] }
    * match $response.items contains only items Scenario: match with partial attributes * def response = { items: [ { id: '3', enabled: false, foo: 1 }, { id: '1', enabled: true, foo: 1 }, { id: '2', enabled: true, foo: 1 } ] } * def responseItems = $response.items
    * def responseItems2 = filterTableKeys(responseItems, items)
    * match responseItems2 contains only items

어디로 filterTableKeys.js정의

function fn(items, table) {
    var mapper = function(v) { return karate.filterKeys(v, table[0]) }
    return karate.map(items, mapper)
}

그래도 우아하게 느껴지지는 않기 때문에 어떻게 더 선언적 / 적은 명령 적 / "스크립트"를 수행 할 수 있는지에 대한 힌트를 주시면 감사하겠습니다.


편집하다

으로 바부 Sekaran는 아래에 언급, 이것은 잘 작동하는 것 같다

  Scenario: match with partial attributes
    * def response = { items: [ { id: '3', enabled: false, foo: 1 }, { id: '1', enabled: true, foo: 1 }, { id: '2', enabled: true, foo: 1 } ] }
    * match $response.items contains deep items