0

我正在尝试遵循此处提供的内容-https: //www.chaijs.com/plugins/chai-http/

it('fails, as expected', function(done) { // <= Pass in done callback
  chai.request('http://localhost:8080')
  .get('/')
  .end(function(err, res) {
    expect(res).to.have.status(123);
    done();                               // <= Call done to signal callback end
  });
});

我的代码片段如下

describe('Scenario: my api test', () => {
  
    let apiResponse;
 
    before('before', function(done)  {
      let chai = require('chai');
      let chaiHttp= require('chai-http')
      chai.use(chaiHttp);
      chai.request(serverurl)
          .get(/endPoint)
          .end(function(err, res) {
            apiResponse = res.body;
            console.log('property1', res.body.metadata.property1)
            console.log('property2', res.body.metadata.property2)
            console.log('property1',apiResponse.metadata.property1)
            console.log('property2', apiResponse.metadata.property2)
            done();
          })   });
  
  
    it('And: I manipulate API REsponse data',() => {
      console.log('printing from next step', apiResponse);
      console.log('printing from next step', apiResponse.metadata.someProperty);
    })
     })

即使我使用 done()it添加了回调,我的步骤中的日志也会首先执行。但是,在那之后,我可以从 chai 块内部看到具有预期值的日志,然后是done is not a function错误。

printing from next step undefined
property1  value1
property2  value2
property1  value1
property2  value2

1) Scenario: my api test And: I manipulate API REsponse data:
 Cannot read property 'metadata' of undefined
 TypeError: Cannot read property 'metadata' of undefined

2) Scenario: my api test "after each" hook:
 done is not a function
 TypeError: done is not a function

我的目标是将响应存储在一个对象中,并在接下来的多个步骤中访问它。

4

0 回答 0