简体   繁体   English

摩卡测试:Test.serverAddress上的“ TypeError:未定义不是函数”

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

I have the following test: 我有以下测试:

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();
      });
    });
  });

Which produces this following error: 产生以下错误: 在此处输入图片说明

any ideas what might be the issue? 任何想法可能是什么问题? I have all dependencies checked and installed and required. 我已经检查并安装了所有依赖项,并且是必需的。

EDIT: 编辑:

The reason this happens is this: https://github.com/Oreqizer/mean-book/blob/master/server.js - lines 11 to 18. 发生这种情况的原因是这样的: https : //github.com/Oreqizer/mean-book/blob/master/server.js-第11至18行。

My tests start BEFORE the app connects to the db. 应用程序连接到数据库之前,我的测试开始。 I noticed this by console.logging 'app', as well as noticing that Server running at http://localhost:3000/ gets logged at different times during the first test. 我通过console.logging'app'注意到了这一点,并注意到Server running at http://localhost:3000/在第一次测试期间的不同时间被记录了下来。

How do I make mocha wait for the app to be defined before running the tests? 如何在运行测试之前让Mocha等待应用程序被定义?

OK so it goes like this: 好,它是这样的:

newer version of 'mongoose' causes express not to have a defined 'db' connection before i do var app = express(db); 较新版本的“猫鼬”导致在我执行var app = express(db);之前,express没有定义的“ db”连接var app = express(db); . However, if I add the code that goes: 但是,如果我添加如下代码:

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

where in the callback I define app , the test gets executed without app being defined. 我在回调中定义app的位置执行测试,而未定义app。

I really don't know how to fix this besides having two different versions, one for test environment and one for development/production. 除了有两个不同的版本,一个用于测试环境,一个用于开发/生产,我真的不知道如何解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM