2015-09-05 71 views
1

我有以下測試:摩卡測試:「類型錯誤:未定義不是一個函數」在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(); 
     }); 
    }); 
    }); 

將會產生此以下錯誤: enter image description here

任何想法可能是什麼問題?我檢查並安裝了所有依賴關係。

編輯:

出現這種情況的原因是這樣的:https://github.com/Oreqizer/mean-book/blob/master/server.js - 線11至18

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

在運行測試之前,我如何讓摩卡等待應用程序的定義?

+0

你在哪裏定義了'app' –

+0

https://github.com/Oreqizer/mean-book/blob/master/app/tests/articles.server.controller.tests.js這裏。您可以檢查整個源代碼是否有幫助 – Idefixx

+0

當您查看調用堆棧中提到的第一個文件(結束於supertest \ lib'test.js)的第55行時,是否顯示:var add = app.address() ;'? – regular

回答

2

OK所以它是這樣的:

較新版本的「貓鼬」的原因表示不具有已定義的「DB」連接我做之前。不過,如果我添加代碼雲:被定義

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

凡在我定義app回調,測試得到,而不應用來執行。

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

相關問題