1

I'm new to Node.js, express and supertest, and can't figure out how to set up a basic test. I created an app with:

express -H -c less

added mocha and supertest to the dependencies:

"dependencies": {
  "mocha": "*",
  "supertest": "*",
  "express": "~4.2.0",
  "static-favicon": "~1.0.0",
  "morgan": "~1.0.0",
  "cookie-parser": "~1.0.1",
  "body-parser": "~1.0.0",
  "debug": "~0.7.4",
  "hjs": "~0.0.6",
  "less-middleware": "1.0.x"
}

then wrote this simple test:

var request = require('supertest')
, express = require('express');

var app = express();

describe('Home page', function() {
  it("renders successfully", function(done) {
    request(app).get('/').expect(200, done);    
  })
}

Test fails:

1) Home page renders successfully:
  Uncaught TypeError: Cannot call method 'handle' of undefined
    at Function.app.handle (/Users/sarahallen/src/exp/test-first-express/node_modules/express/lib/application.js:120:16)
    at Server.app (/Users/sarahallen/src/exp/test-first-express/node_modules/express/lib/express.js:28:9)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:527:27)

Software Versions:

node -v
v0.10.29

express --version
4.2.0
4

1 回答 1

4

以下设置代码有效——改编自本教程:http ://tinycandyhammers.com/2012/08/05/nodejs-testing-tutorial.html

var request = require('supertest')
  , express = require('express');

var app = require('../app');

describe('Home page', function() {
  it("renders successfully", function(done) {
    request(app)
      .get('/')
      .expect(200, done);    
  })
})

不知道为什么从 supertest README 复制的上一个示例不正确(https://github.com/visionmedia/supertest

于 2014-08-14T05:09:04.400 回答