1

编辑:我正在构建一些向 API 提交 POST 请求、执行数据库查询并最终将数据库输出与 API 响应匹配的测试,我有执行这些单独任务的代码,但是如何在特定的命令?

var frisby = require('frisby');
var oracledb = require('oracledb');
var url = require('endpoints.js');
var auth = require('users.js');
var dbConnect = require('dbconfig.js');

var myDetails = undefined;

function submitRequest(){
  frisby.create('Update contact details')
  .put(url.myAccount, {
    "addressLine-1": 'String',
    "addressLine-2": 'String',
    addressTown: 'String',
    addressCounty: 'String',
    addressPostcode: 'String',
    homePhone: 'String',
    mobilePhone: 'String'
  }, {json: true})
  .expectStatus(200)
  .expectHeaderContains('content-type', 'application/json')
  .auth(auth.username1, auth.password1)
.toss();
readDatabase();
}

function readDatabase(){
  oracledb.getConnection(
    {
      user          : dbConnect.user,
      password      : dbConnect.password,
      connectString : dbConnect.connectString
    },
    function(err, connection){
      if (err) {
        console.error(err.message);
        return;
      }
      connection.execute(
      "SELECT addressLine1, addressLine2, addressLine3, town, county, postcode, homePhone, mobilePhone"
      + "FROM addressDetails"
      + "WHERE accountNo = 1",

      function(err, result){
        if (err) {
          console.error(err.message);
          return;
        }
        myDetails = JSON.stringify(result.rows);
        myDetails = JSON.parse(myDetails);
    });
});
matchValues();
}

function matchValues(){
frisby.create('Match Database and API Values')
  .get(url.myAccount)
  .expectStatus(200)
  .expectHeaderContains('content-type', 'application/json')
  .auth(auth.username1, auth.password1)
  .afterJSON(function (body) {
    expect(body.addressLine1).toMatch(myDetails[0][0])
    expect(body.addressLine2).toMatch(myDetails[0][1])
    expect(body.addressLine3).toMatch(myDetails[0][2])
    expect(body.town).toMatch(myDetails[0][3])
    expect(body.county).toMatch(myDetails[0][4])
    expect(body.postcode).toMatch(myDetails[0][5])
    expect(body.homePhone).toMatch(myDetails[0][6])
    expect(body.mobilePhone).toMatch(myDetails[0][7])
  })
.toss();
}

submitRequest();

当我运行它时,测试有时会通过,有时会失败,因为代码并不总是按顺序执行。我按照建议进行了更改,但仍然有同样的问题,我发现很难为这种情况应用回调/承诺

4

0 回答 0