【问题标题】:How to check the field data type (only) using postman?如何使用邮递员检查字段数据类型(仅)?
【发布时间】:2017-08-15 20:35:34
【问题描述】:

我正在使用 Postman 进行一些 API 验证,其中我想验证响应中几个字段的数据类型(字符串/整数)。我不想检查字段值。 例如:如果我得到类似下面的响应,我想检查 sign_in_count 是否总是返回整数数据类型。 :

    {
  "data": {
    "id": 274,
    "age": null,
    "email_id": "ojuuw@mailinator.com",
    "email_verification_flag": true,
    "ext_account": null,
    "ext_authenticator": null,
    "ext_user_id": null,
    "firstname": "firstname",
    "forgot_password_sent": null,
    "gender": null,
    "last_sign_in_at": null,
    "last_sign_in_ip": null,
    "region": "IN",
    "registered_using": null,
    "remember_created_at": null,
    "reset_password_sent_at": null,
    "reset_password_token": null,
    "sign_in_count": 0  
  }
}

任何帮助,非常感谢..

谢谢

【问题讨论】:

    标签: api types postman


    【解决方案1】:

    Postman 有关于如何检查某些数据类型的语法。不再需要使用typeof

    var data = pm.response.json();
    
    pm.expect(data[0].id).to.be.a("number");
    pm.expect(data[0].email_id).to.be.a("string");
    pm.expect(data[0].email_verification_flag).to.be.true;
    

    以下是检查id 属性是否存在并正确返回的可靠方法:

    pm.test("Id should exist, not be null, be a number and be above zero", function () {
        pm.expect(data[0]).to.have.property('id');
        pm.expect(data[0].id).not.eql(null);
        pm.expect(data[0].id).to.be.a("number");
        pm.expect(data[0].id).be.above(0);
    });
    

    【讨论】:

      【解决方案2】:

      您需要使用typeof 函数返回未计算操作数的类型。

      var body = JSON.parse(responseBody); tests["sign_in_count is number"] = typeof(body.data.sign_in_count) === "number"; tests["firstname is string"] = typeof(body.data.firstname) === "string";

      【讨论】:

      • 对我不起作用..这两个人都通过了:` pm.expect(typeof(data.user.email)==="string") pm.expect(typeof(data.user .email)==="number")` 怎么可能? ?
      • 我使用pm.expect(parseInt(responseBody)).to.be.a('number'); 来检查正文是否为数字 - 没有 JSON
      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      相关资源
      最近更新 更多