摩卡测试总是通过

时间:2016-05-24 10:29:46

标签: javascript testing mocha

我想使用mocha在我的网页上测试基本身份验证:

describe('Test GET /messages', function(){

    describe('Test HTTP basic authentication', function(){

       it('should return 200 because right credentials', function(done){
           request.get(createCustomRequest(true, "/messages", "test", "test"), function(err, res, body){
               if (err) {
                   console.error(err)
               } else {
                    // Whatever, it will always pass
                    assert.equal(2400, res.statusCode);
               }
           }, done());
        });
  });

function createCustomRequest(withAuth, service, username, password){
    if(!withAuth){
        return {
            url: URL+service,
            followRedirect: false
        };
    } else {
        return {
            url: URL+service,
            followRedirect: false,
            headers: {
                'Authorization': createCredentials(username, password)
            }
        };
    }
}

从前面的代码中,我不明白为什么测试总是通过。我认为它与done()回调有关。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

应在回调done()

的过程中调用

request.get()

it('should return 200 because right credentials', function(done) {
  request.get(createCustomRequest(true, "/messages", "test", "test"), function(err, res, body){
    if (err) {
      done(err);
      return;
    }
    assert.equal(2400, res.statusCode);
    done();
  });
});