2016-08-08 2 views
0

以下は、mocha、chai、supertestで書いたコードです。トークンに焦点を当て、その作品の下にあるコードのセグメントに関する質問があります。Supertest/mocha doneパラメータがテスト内で渡される

describe('Authenticated userTest', function() { 
    var token; 

    before(function loginAuth(done) { 
     request(app) 
      .post("/login/local") 
      .send("username=testName") 
      .send("password=qwe123QWE") 
      .expect(function (res) { 
       should.exist(res.body.token); 
       token = res.body.token; 
      }) 
      .end(done); 
    }); 

    it('should give me a defined token', function(done) { 
     console.log("token is " + token); 
     done(); 
    }); 
}); 

明らかに、トークンはすべてよくうまく定義されています。ただし、次のようにdone関数を削除すると、次のようになります。

describe('Authenticated userTest', function() { 
    var token; 

    before(function loginAuth() { //done is removed here 
     request(app) 
      .post("/login/local") 
      .send("username=testName") 
      .send("password=qwe123QWE") 
      .expect(function (res) { 
       should.exist(res.body.token); 
       token = res.body.token; 
      }) 
      .end(); //done is removed here 
    }); 

    it('should give me a defined token', function(done) { 
     console.log("token is " + token); 
     done(); 
    }); 
}); 

トークンは不定になります。私が理解していることから、doneは、ビルド前のフックから、組み込みのソースコードからit(...)で始まるさまざまなテストに渡される関数です。

このように、特定の質問(テストでのみ実行された場合、実行された場合はerrパラメータのみを受け入れる)、doneパラメータを削除した後でトークンがなぜ未定義になったのかを明確にしたいのですか?

ありがとうございます。

答えて

関連する問題