10

我正在尝试为节点应用程序构建 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
4

2 回答 2

17

只是要清楚,没有使用 supertest 的方法 delete ,但正确的方法是del

所以删除请求应该像这样测试:

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 或字符串。

于 2013-12-30T11:06:19.260 回答
9

看看supertest 的 GitHub-page

您可以传递 ahttp.Server或 aFunctionrequest()

您正在将字符串传递给函数请求。尝试将您的 express-server 对象作为函数参数传递。

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

于 2013-12-01T20:04:58.860 回答