使用摩卡和supertest不能testingDELETE方法

我正在尝试为节点应用程序构build一个RESTful API。 我build立了路线,一切运行良好。 但是,当我尝试testing它,它不能让DELETE方法工作,尽pipe它通常不在testing中正常工作。

这里是服务器和testing的代码。 服务器:

// 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; 

testing:

 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 

       

网上收集的解决方案 "使用摩卡和supertest不能testingDELETE方法"

看看超级GitHub页面 。

你可以传递一个http.Server或一个Functionrequest()

您正在传递一个string到函数请求。 尝试传递您的快递服务器对象作为函数参数。

编辑:如在评论和@mpm:s答案中看到的,问题是使用保留的函数delete()而不是特定于包的函数del()

只是要清楚,没有方法删除超级,但正确的方法是删除

所以一个删除请求应该像这样testing:

 var app=require('./app') var request=require('supertest') //with mocha for instance. describe('test delete',function(){ it('should respond 200',function(done){ request(app).del('/path').expect(200).end(done); }) }); 

而且需要通过应用程序(快速应用程序),而不是url或string。