SuperTest - App未定义

时间:2015-09-29 00:18:26

标签: javascript express mocha supertest

我是测试驱动开发的新手,我正在尝试使用超级自学。我很困惑为什么我一直得到错误“应用程序未定义”?这是我的请求(app)的调用,我在下面加粗。我试着查看文档,但似乎没有太多。我的所有路由都在一个名为“middleware”的文件中,该文件的开头如下:

 var bodyParser = require('body-parser');
    var helpers = require('./helpers.js'); // our custom middleware
    var db = require('../DB/DB.js');
    var router = require('../routes.js');
    var path = require('path');
    var fs = require('fs');
    var gm = require('gm');


    module.exports = function (app, express) {}

我使用supertest测试中间件文件路由的实际文件:

    var chai = require('chai')
    var assert = chai.assert;
    var should = chai.should();
    var expect = chai.expect;
    var helpers = require("../config/helpers.js");
    var middleware = require("../config/middleware.js");
    // for when we eventually want to test against mock data
    var fs = require('fs');
    var path = require('path');
    var supertest = require("supertest")(middleware);

describe('middleware API', function() {


  it('responds with binary data', function(done) {
    var imagePath = path.join(__dirname, '/../assets/drawings/', userName + '.png');
    **request(app)**
      .get(imagePath)
      .expect(201)
      .expect('Content-Type', 'image.png')
      .parse(binaryParser)
      .end(function(err, res) {
        if (err) return done(err);

        // binary response data is in res.body as a buffer
        assert.ok(Buffer.isBuffer(res.body));
        console.log("res=", res.body);

        done();
      });
  });

  it('sends back one image', function(done) {
    **request(app)**
      .get('/game/')
      .expect(201)
      .expect('Content-Type', 'image.png')
      .expect('Content-Length', '1')
      .parse(binaryParser)
      .end(function(err, res) {
        if (err) return done(err);

        // binary response data is in res.body as a buffer
        assert.ok(Buffer.isBuffer(res.body));
        console.log("res=", res.body);

        done();
      })

  })
})

1 个答案:

答案 0 :(得分:3)

在文件的顶部,您需要定义express和app,即:

var express        = require('express'),
    app            = express();

另外,请确保您已在节点中安装了express,即thru命令行:

npm install express --save

(使用" - save"将其添加到package.json文件中,这样可以轻松跟踪版本等,但如果其他人使用npm install安装您的项目,也会自动安装)。