【问题标题】:mocha chai test on node api always receive 404 status节点 api 上的 mocha chai 测试总是收到 404 状态
【发布时间】:2017-10-26 06:43:22
【问题描述】:

我正在创建这个测试文件

let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../index');
chai.use(chaiHttp);

describe('Users', () => {
  describe('/GET users', () => {
    it('it should GET all the users', (done) => {
      chai.request(server).get('/user/get-all-users').end((err, res) => {
          res.should.have.status(200)
          done()
        });
    });
  });
});

这条路线工作得很好,但是在测试时我总是得到测试失败 404

我正在使用 restify 来运行我的服务器,

global.server = restify.createServer({
    url     : config.base_url,
    name    : config.name,
    version : config.version,
    log     : bunyanWinston.createAdapter(log),
  })

【问题讨论】:

  • 您的服务器是否包含正确的服务器 url?
  • 我的网址是 localhost:3000
  • 上下文中的服务器变量看起来像一个对象 show console.log(server)
  • 如果我使用 console.log(server),我会得到这个 pastebin.com/3MxpQr1H

标签: node.js mocha.js chai restify


【解决方案1】:

我想通了

这是我的服务器:

'use strict'

/**
 * Module Dependencies
 */
const config        = require('./config'),
  restify       = require('restify'),
  bunyan        = require('bunyan'),
  winston       = require('winston'),
  bunyanWinston = require('bunyan-winston-adapter'),
  mongoose      = require('mongoose')

/**
 * Logging
 */
global.log = new winston.Logger({
  transports: [
    new winston.transports.Console({
      level: 'info',
      timestamp: () => {
        return new Date().toString()
      },
      json: true
    }),
  ]
})

/**
 * Initialize Server
 */
global.server = restify.createServer({
  url     : config.base_url,
  name    : config.name,
  version : config.version,
  log     : bunyanWinston.createAdapter(log),
})

/**
 * Middleware
 */
server.use(restify.jsonBodyParser({ mapParams: true }))
server.use(restify.acceptParser(server.acceptable))
server.use(restify.queryParser({ mapParams: true }))
server.use(restify.fullResponse())


/**
 * Error Handling
 */
server.on('uncaughtException', (req, res, route, err) => {
  log.error(err.stack)
  res.send(err)
});

/**
 * Lift Server, Connect to DB & Bind Routes
 */
server.listen(config.port, 'localhost', function() {

  mongoose.connection.on('error', function(err) {
    log.error('Mongoose default connection error: ' + err)
    process.exit(1)
  })

  mongoose.connection.on('open', function(err) {

    if (err) {
      log.error('Mongoose default connection error: ' + err)
      process.exit(1)
    }

    log.info(
      '%s v%s ready to accept connections on port %s in %s environment. %s',
      server.name,
      config.version,
      config.port,
      config.env,
      server.url
    )

    require('./api/routes/')

  })

  global.db = mongoose.connect(config.db.uri)

})

module.exports = server

我需要回调监听函数中的路由文件。这种方式的路线有效,但柴找不到路线。只需从回调中删除路由并将其放在我的 index.js 上,它就可以正常工作。

【讨论】:

  • 救命稻草。谢谢你:)
  • 愿编码之神保佑你
猜你喜欢
  • 2018-04-17
  • 1970-01-01
  • 2019-05-17
  • 2017-02-04
  • 1970-01-01
  • 2016-09-20
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多