我正在尝试为一个Node应用构建一个RESTful API。我已经构建了路由,一切都运行正常。但是当我尝试进行测试时,DELETE方法无法正常工作,而在没有测试的情况下,它可以正常工作。
这是服务器和测试的代码:
这是服务器和测试的代码:
// set up
var express = require('express');
var app = express(); // create our app w/ express
var path = __dirname; //root path
// configuration
app.configure(function() {
app.use(express.static(path));
//app.use(express.logger('dev')); // log every request to the console
app.use(express.json());
app.use(express.urlencoded()); // pull information from html in POST
app.use(express.methodOverride()); // simulate DELETE and PUT
});
function start() {
// routes
require('./app/routes.js')(app);
// listen (start app with node server.js)
app.listen(process.env.PORT || 5000);
console.log("Server listening for incoming conections..");
}
//************************
exports.start = start;
exports.server = app;
测试:
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var mongoose = require('mongoose');
var express = require('express');
var server = require(__dirname + './../index.js');
describe('Routing', function() {
var url = 'http://localhost:5000';
it('should return status 200 after DELETING a bus', function(done) {
request(url)
.delete('/api/buses/' + bus.id)
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(200);
done();
});
});
});
这是它抛出的错误信息:
Routing
1) should return status 200 after DELETING a bus
✖ 1 of 1 test failed:
1) Routing should return status 200 after DELETING a bus:
TypeError: Object #<Object> has no method 'delete'
at Context.<anonymous> (/home/roger/Documents/Buse/test/test.js:63:16)
at Test.Runnable.run (/home/roger/Documents/Buse/node_modules/mocha/lib/runnable.js:196:15)
at Runner.runTest (/home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:351:10)
at /home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:397:12
at next (/home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:277:14)
at /home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:286:7
at next (/home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:234:23)
at Object._onImmediate (/home/roger/Documents/Buse/node_modules/mocha/lib/runner.js:254:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
make: *** [test] Error 1
delete
方法,并且从v0.13.0版本开始支持旧的别名del()
。参考链接:https://github.com/visionmedia/supertest/commit/87412836488a4ccab97abe8b5ac5cd4338aeb5a3 - brandonjp