簡體   English   中英

摩卡測試:Test.serverAddress上的“ TypeError:未定義不是函數”

[英]Mocha testing: 'TypeError: undefined not a function' at Test.serverAddress

我有以下測試:

describe('Testing the GET methods', function() {
    it('Should be able to get the list of articles', function(done) {
      // Create a SuperTest request
      request(app).get('/api/articles/')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .end(function(err, res) {
              res.body.should.be.an.Array.and.have.lengthOf(1);
              res.body[0].should.have.property('title', article.title);
              res.body[0].should.have.property('content', article.content);

              done();
      });
    });

    it('Should be able to get the specific article', function(done) {
      // Create a SuperTest request
      request(app).get('/api/articles/' + article.id)
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .end(function(err, res) {
              res.body.should.be.an.Object.and.have.property('title', article.title);
              res.body.should.have.property('content', article.content);

              done();
      });
    });
  });

產生以下錯誤: 在此處輸入圖片說明

任何想法可能是什么問題? 我已經檢查並安裝了所有依賴項,並且是必需的。

編輯:

發生這種情況的原因是這樣的: https : //github.com/Oreqizer/mean-book/blob/master/server.js-第11至18行。

應用程序連接到數據庫之前,我的測試開始。 我通過console.logging'app'注意到了這一點,並注意到Server running at http://localhost:3000/在第一次測試期間的不同時間被記錄了下來。

如何在運行測試之前讓Mocha等待應用程序被定義?

好,它是這樣的:

較新版本的“貓鼬”導致在我執行var app = express(db);之前,express沒有定義的“ db”連接var app = express(db); 但是,如果我添加如下代碼:

db.connection.on('connected', callback);

我在回調中定義app的位置執行測試,而未定義app。

除了有兩個不同的版本,一個用於測試環境,一個用於開發/生產,我真的不知道如何解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM