2015-09-05 72 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回调,测试得到,而不应用来执行。

我真的不知道如何解决这个问题,除了有两个不同的版本,一个用于测试环境,一个用于开发/生产。

相关问题