繁体   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