柴未到达.end()

时间:2016-05-12 15:39:57

标签: node.js express mocha chai api-design

我正在使用Mocha和Chai来测试我的Node / Express API,我无法弄清楚为什么测试没有到达.end()

以下是测试:

it('should authenticate successfully with user credentials', function (done) {
    agent
        .post('/login')
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .send({ 'username': 'username', 'password': 'password'})
        .end(function (err, res) {
            console.log(res);
            console.log('***************************Authenticated*********************************************');
            expect(res).to.have.status(200);
        });
    done();
});

这是我正在打的路线:

app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/' }));

我认为我的问题可能是因为没有正式的回复,而是重定向,但我不知道如何处理它。

3 个答案:

答案 0 :(得分:3)

解决方案最终将done()回调移动到我的.end()方法中。谢谢@robertklep

答案 1 :(得分:1)

如果您正在测试异步方法int mocha,则应在回调函数中调用 call 方法,如下所示。

it('should authenticate successfully with user credentials', function (done) {
        agent
            .post('/login')
            .set('Content-Type', 'application/x-www-form-urlencoded')
            .send({ 'username': 'username', 'password': 'password'})
            .end(function (err, res) {
                console.log(res);
                console.log('***************************Authenticated*********************************************');
                expect(res).to.have.status(200);
                done();
            });

    });

答案 2 :(得分:0)

我对柴的要求也有同样的问题。我想在转到另一个函数之前等待.end回调。但我不能使用摩卡咖啡,因为我正在使用黄瓜。我如何等待chai .end回调? 实际上,我想先登录(1),但不能正常工作

When('I submit with method {string}:', function (string, docString) {
  chai.request(app)
  .post(endpoint)
  .send(docString)
  .end(function (err, res) {
    console.log(1)
    response = res
  })
});

Then('I recieved ok', function () {
  console.log(2)
  // expect(response.status).to.deep.equal(200)
});
相关问题