如何在zapier的测试案例中检查响应的状态代码

时间:2019-08-31 10:22:56

标签: zapier zapier-cli

我正在编写一些代码来测试Zapier的CLI中的操作。我想在此处再添加一个类似response.status == 200 or 201;的条件,以检查API响应代码是200还是201。

我该怎么办?当我记录响应时,它给了我整个JSON对象, API正在返回。

describe("contact create", () => {
  it("should create a contact", done => {
    const bundle = {
      inputData: {
        firstName: "Test",
        lastName: "Contact",
        email: "Contact@test.com",
        mobileNumber: "+12125551234",
        type: "contact"
      }
    };
    appTester(App.creates.contact.operation.perform, bundle)
      .then(response => {

        // Need one more condition whether response status is 200 or 201.

        response.should.not.be.an.Array();
        response.should.have.property('id');
        done();
      })
      .catch(done);
  });
});

1 个答案:

答案 0 :(得分:1)

appTester返回perform方法的结果,它不是API响应。正是这些数据被传递回Zapier。

最好的做法是在perform中添加如下一行:

// after your `z.request`
if (!(response.status === 200 || response.status === 201)) {
  throw new Error('need a 200/201 response')
}

这将确保您准确获得所需的响应。但是,更有可能的是,您可以添加response.throwForStatus()以确保它不是错误代码,并且不必担心它是否恰好是200/201。