Cant test DELETE method using mocha and supertest

2019-04-22 21:56发布

问题:

I'm trying to build a RESTful API for a node app. I built the routes and everything is running fine. But when I try to test it, it cant get the DELETE method to work, despite of it working normally not under tests.

Here are the codes for the server and test. Server:

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

Test:

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();
        });
    });
});

And this is the error message it throws:

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

回答1:

Take a look at the supertest GitHub-page.

You may pass an http.Server, or a Function to request()

You are passing a string to the function request. Try passing your express-server object as the function parameter.

EDIT: as seen in comments and @mpm:s answer, the issue was the usage of reserved function delete() instead of package-specific function del().



回答2:

Just to be clear there is no method delete with supertest but the correct method is del.

so a delete request should be tested like this:

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);
  })
});

And one needs to pass the app (express app) , not a url or a string.