Mocha API测试:收到"TypeError:app.address不是函数" [英] Mocha API Testing: getting 'TypeError: app.address is not a function'

查看:130
本文介绍了Mocha API测试:收到"TypeError:app.address不是函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个非常简单的CRUD API,并且最近开始使用chaichai-http进行一些测试的编码,但是在使用$ mocha运行测试时遇到了问题.

I've coded a very simple CRUD API and I've started recently coding also some tests using chai and chai-http but I'm having an issue when running my tests with $ mocha.

运行测试时,在外壳上出现以下错误:

When I run the tests I get the following error on the shell:

TypeError: app.address is not a function

这是我的一项测试(/tests/server-test.js )的示例:

Here is a sample of one of my tests (/tests/server-test.js):

var chai = require('chai');
var mongoose = require('mongoose');
var chaiHttp = require('chai-http');
var server = require('../server/app'); // my express app
var should = chai.should();
var testUtils = require('./test-utils');

chai.use(chaiHttp);

describe('API Tests', function() {
  before(function() {
    mongoose.createConnection('mongodb://localhost/bot-test', myOptionsObj);
  });

  beforeEach(function(done) {
    // I do stuff like populating db
  });

  afterEach(function(done) {
    // I do stuff like deleting populated db
  });

  after(function() {
    mongoose.connection.close();
  });

  describe('Boxes', function() {

    it.only('should list ALL boxes on /boxes GET', function(done) {
      chai.request(server)
        .get('/api/boxes')
        .end(function(err, res){
          res.should.have.status(200);
          done();
        });
    });

    // the rest of the tests would continue here...

  });

});

和我的express应用程序文件(/server/app.js ):

And my express app files (/server/app.js):

var mongoose = require('mongoose');
var express = require('express');
var api = require('./routes/api.js');
var app = express();

mongoose.connect('mongodb://localhost/db-dev', myOptionsObj);

// application configuration
require('./config/express')(app);

// routing set up
app.use('/api', api);

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('App listening at http://%s:%s', host, port);
});

和(/server/routes/api.js ):

var express = require('express');
var boxController = require('../modules/box/controller');
var thingController = require('../modules/thing/controller');
var router = express.Router();

// API routing
router.get('/boxes', boxController.getAll);
// etc.

module.exports = router;

额外笔记

在运行测试之前,我尝试注销/tests/server-test.js 文件中的server变量:

Extra notes

I've tried logging out the server variable in the /tests/server-test.js file before running the tests:

...
var server = require('../server/app'); // my express app
...

console.log('server: ', server);
...

我的结果是一个空对象:server: {}.

and I the result of that is an empty object: server: {}.

推荐答案

您不会在应用模块中导出任何内容.尝试将其添加到您的app.js文件中:

You don't export anything in your app module. Try adding this to your app.js file:

module.exports = server

这篇关于Mocha API测试:收到"TypeError:app.address不是函数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆