2

我对邮递员很陌生。我想比较 JSON 主体对象响应。

json_response = JSON.parse(responseBody);
x=json_response.counter
pm.expect(x).to.equal("400");

它给了我相应的“计数器”的 400 值(或键“计数器”的“400”值)

但我想将“counter”与“counter”本身进行比较。(例如,Counter、Counter 等)

{

    "counter": 400,
    "validInMinutes": 660,
    "currentCounter": 322,

}

基本上,我想测试收到的所有 JSON 密钥都等于我正在寻找的!有没有简单的方法来做到这一点?

4

2 回答 2

2

Postmanpm.expect使用 Chai 库进行断言(由于某种原因,此链接指向pm.cookies但条目位于该标题下)。pm.expectA 使用Chai API ,您可以通过说出并定义密钥应该是什么来验证响应是否只有您期望的密钥:expect(someObject).to.have.keys(expectedKeys)

//for convenience - in Postman, these would be readily available. 
var pm = { 
  expect: chai.expect, 
  response: {
    json: function() {
      // the response you would get
      return { "counter": 400, "validInMinutes": 660, "currentCounter": 322 };
    }
  }
};

/* --- test script begins --- */

var expectedKeys = ["counter", "validInMinutes", "currentCounter"];

pm.expect(pm.response.json()).to.have.keys(expectedKeys); // OK
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>

以下是一些示例,说明这种期望如何失败:

var pm = { 
  expect: chai.expect, 
  response: {
    json: function() {
      // "Counter" starts with capital C
      return { "Counter": 400, "validInMinutes": 660, "currentCounter": 322 };
    }
  }
};

/* --- test script begins --- */

var expectedKeys = ["counter", "validInMinutes", "currentCounter"];

pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>

var pm = { 
  expect: chai.expect, 
  response: {
    json: function() {
      // "counter" is missing
      return { "validInMinutes": 660, "currentCounter": 322 };
    }
  }
};

/* --- test script begins --- */

var expectedKeys = ["counter", "validInMinutes", "currentCounter"];

pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>

var pm = { 
  expect: chai.expect, 
  response: {
    json: function() {
      //same object you have...
      return { "counter": 400, "validInMinutes": 660, "currentCounter": 322 };
    }
  }
};

/* --- test script begins --- */

//...but there is an extra expected key
var expectedKeys = ["counter", "validInMinutes", "currentCounter", "otherKey"];

pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>

于 2019-05-08T13:52:47.927 回答
1

您的脑海中有一个特定的模式,您想检查它。(例如强制字段、类型等)另一种可能的方法是根据预定义的 JSON 模式进行验证:

//Schema definition (can be available as a global var)
var schema = {
    "type": "object",
    "properties": {
        "counter": {
            "type": "number"
        },
        "validInMinutes": {
            "type": "number"
        },
        "currentCounter": {
            "type": "number"
        }
    },
    "required": ["counter", "validInMinutes", "currentCounter"]
};

//Test
pm.test('Schema is valid', function() {
    var jsonData = pm.response.json();
    pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});

Line"required": ["counter", "validInMinutes", "currentCounter"]正是您对必须存在哪种属性的定义。但是您也可以定义更多的 strct 规则,如“必须是 Numer 类型”,或者必须恰好有 3 个数字,等等。

于 2019-05-09T05:16:28.333 回答