0

API に対していくつかのテストを実行しようとしましたが、この例では、アカウントの詳細を更新する必要がありました。テストを実行すると、putRequest の前に retrieveAccount 呼び出しが実行され、テストが失敗することがあります。私は何を間違っていますか?

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

var myDetails = undefined;

  var putRequest = function() {
    frisby.create('Put update contact details - required values')
      .put(url.myAccount, {
        addressLine1: 'String',
        addressTown: 'String',
        addressCounty: 'String'
      }, {json: true})
      .expectStatus(200)
      .expectHeaderContains('content-type', 'application/json')
      .auth(auth.username, auth.password)
    .toss();
  }

  var retrieveAccount = function() {
    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, addressTown, addressCounty "
          + "FROM accounts "
          + "WHERE account_id = 1",

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

  var matchValues = function() {
    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.addressCounty).toMatch(myDetails[0][1])
        expect(body.addressTown).toMatch(myDetails[0][0])
      })
    .toss();
  }

  function Promise(fn) {
    var state = 'pending';
    var value;
    var deferred = null;

    function resolve(newValue) {
      value = newValue;
      state = 'resolved';

      if(deferred) {
        handle(deferred);
      }
    }

    function handle(handler) {
      if(state === 'pending') {
        deferred = handler;
        return;
      }

      if(!handler.onResolved) {
        handler.resolve(value);
        return;
      }

      var ret = handler.onResolved(value);
      handler.resolve(ret);
    }

    this.then = function(onResolved) {
      return new Promise(function(resolve) {
        handle({
          onResolved: onResolved,
          resolve: resolve
        });
      });
    };

    fn(resolve);
  }

  function sendRequest() {
    return new Promise(function(resolve){
      var value = putRequest();
      resolve(value);
    });
  }

  function readDatabase() {
    return new Promise(function(resolve){
      var value = retrieveAccount();
      resolve(value);
    });
  }

  function getAccount() {
    return new Promise(function(resolve){
      var value = matchValues();
      resolve(value);
    });
  }

sendRequest()
  .then(readDatabase)
  .then(getAccount);
4

1 に答える 1