Axios返回错误,即使端点重新调整为200

时间:2018-10-30 16:31:29

标签: typescript cucumber axios cucumberjs

我编写了一个TypeScript类,试图在Cucumber测试中使用它来建立用户。我可以看到正在发出请求,但是即使Promise链似乎可以正常工作,但Promise似乎也被拒绝了。这是我的课:

import axios from 'axios';
import { UUID } from "../../src/app/utilities/uuid";

export class ApiCreateCase {

  caseDetail: any
  token: string
  uuid: string
  domain: string
  headers: any

  constructor(caseDetail: any, token: string) {
    this.caseDetail = caseDetail
    this.token = token
    this.uuid = UUID.generateUUID()
    this.domain = 'http://my-domain.com'
    this.headers = {
      headers: {
        'Authorization': this.token,
        'Accept': 'application/json'
      }
    }
  }

  public async run(): Promise<any> {
    return this.createCase()
      .then((response) => {
        return this.addFamilyMembers()
      })
      .then((response) => {
        return this.assignTeamMember()
      })
      .then((response) => {
        return this.uuid;
      })
  }

  private async createCase(): Promise<any> {
    var body = {
      "case":{
        "people":[],
        "team":{
          "members":[]
        },
        "address": this.caseDetail.address,
        "visits":[],
        "summaries":[],
        "id": this.uuid,
        "caseName": this.caseDetail.people[0].lastName,
        "familyName": this.caseDetail.people[0].lastName
      }
    }
    return axios.post(`${this.domain}/cases`, body, this.headers)
  }

  private async addFamilyMembers(): Promise<any> {
    var people = []
    for (let person of this.caseDetail.people) {
      people.push(this.addFamilyMember(person))
    }
    return await axios.all(people);
  }

  private async addFamilyMember(person: any): Promise<any> {
    var body = {
      "person":{
        "dateOfBirth":"1901-01-01T00:00:00.000Z",
        "roles":[],
        "id": UUID.generateUUID(),
        "firstName": person.firstName,
        "lastName": person.lastName,
        "email": person.email,
        "relationship": person.relationship,
        "telephoneNumber": person.telephoneNumber
      }
    }
    return axios.put(`${this.domain}/cases/${this.uuid}/family-member`, body, this.headers)
  }

  private async assignTeamMember(): Promise<any> {
    return this.getUserList().then((response) => {
      var user = response.data.users.find((user) => {
        return user.displayName == 'Dev'
      })
      var body = {
        teamMember: {
          id: user.id
        }
      }
      return axios.put(`${this.domain}/cases/${this.uuid}/team-member`, body, this.headers)
    })
  }

  private async getUserList(): Promise<any> {
    return axios.get(`${this.domain}/users/directory?groups=Social%20Workers;Case%20Managers`, this.headers)
  }

}

这就是我在步骤中使用它的方式:

Given('I am a family member', function (callback) {
  var world = this;
  world.case = {
    address: {
      streetAddress: address.streetAddress(),
      extendedAddress: '',
      locality: address.city(),
      postalCode: 'SW1A 1AA'
    },
    people: [
      {
        firstName: name.firstName(), 
        lastName: name.lastName(), 
        phoneNumber: phone.phoneNumber(), 
        relationship: 'Dad'
      }
    ]
  }

  mailslurp.createInbox()
    .then((response) => {
      world.emailId = response.id;
      world.case.emailAddress = response.email;
    })
    .then(() => { 
      return login.perform() 
    })
    .then(() => {
      return login.getToken();
    })
    .then((token) => {
      world.token = token;
      var createCase = new ApiCreateCase(world.case, token)
      return createCase.run()
    })
    .then(callback);
});

这是我得到的错误:

1) Scenario: Listing cases - e2e/features/families/visit-note.feature:7
  Step: Given I am a family member - e2e/features/families/visit-note.feature:4
  Step Definition: node_modules/cucumber/lib/index.js:176
  Message:
    Error: a5489583-4162-4cc4-8923-f89ad2965051
        at Function.<anonymous> (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/cucumber/lib/user_code_runner.js:105:21)
        at Generator.throw (<anonymous>)
        at Generator.tryCatcher (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/util.js:16:23)
        at PromiseSpawn._promiseRejected (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/generators.js:107:10)
        at Promise._settlePromise (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/promise.js:576:26)
        at Promise._settlePromise0 (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/promise.js:614:10)
        at Promise._settlePromises (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/promise.js:689:18)
        at Async._drainQueue (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/async.js:133:16)
        at Async._drainQueues (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/async.js:143:10)
        at Immediate.Async.drainQueues [as _onImmediate] (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/bluebird/js/release/async.js:17:14)
        at runCallback (timers.js:763:18)
        at tryOnImmediate (timers.js:734:5)
        at processImmediate (timers.js:716:5)

我要去哪里错了?

0 个答案:

没有答案