错误:使用非错误调用done():{" req":{......}请求完成且没有错误,但测试没有通过

时间:2018-04-08 12:24:41

标签: node.js testing mocha chai supertest

我试图解决我的测试,但似乎没有任何效果。目前我正在使用mocha,supertest和chai,但即使请求成功执行,我也看到了这个令人烦恼的错误,我无法摆脱:

  使用非错误调用

done():{“req”:{...}请求完成且没有错误,但测试没有通过

代码:



let should = require('chai').should(),
    expect = require('chai').expect,
    constants = require('./constants'),
    supertest = require('supertest'),
    api = supertest(constants.ENVIRONMENT),
    express = require('express'),
    http = require('http'),
    request = require('request'),
    app = express(),
    bodyParser = require('body-parser'),
    port = process.env.PORT || 3000,
    assert = require('assert');
    
    describe('Create device API call', () => {

    function createToken() {
        return new Promise(resolve =>
            request.post(
                'myURl',
                { 
                 json: 
                        { 
                          "username": "myUsername", 
                          "password": "myPassword" 
                        } 
                },
                function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        let token = (body.accessList[0].accessToken);
                        resolve(token);

                    }
                    else {
                        console.log("Server error get token")
                    }

                })
        )
    }

    it('should create device', async (done) => {

        let token = await createToken();

        //console.log(`Token is ${token}`);

        api.post('/deviceManifests/1/devices')
            .set('Accept', 'application/json')
            .set('Authorization', `Bearer ${token}`)
            .send({
                "name": "QA integration",
                "deviceKey": "somekey38957",
                "defaultBinding": true
            })
            .expect('content-Type', /json/)
            .expect(201)
            .expect(function (res, err) {
                assert.equal(res.body.name, 'QA integration');

            }).then(done);
    });

});    




1 个答案:

答案 0 :(得分:0)

您正在将完成传递给然后

因此,完成将传递参数。

但是,如果您将参数传递给完成,则会预期它会出错。

请改为尝试:

.then(function () {
  done()
})

...或使用箭头功能:

.then(() => done())