【问题标题】:Cant test DELETE method using mocha and supertest无法使用 mocha 和 supertest 测试 DELETE 方法
【发布时间】:2013-12-01 19:42:17
【问题描述】:

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

【问题讨论】:

    标签: node.js rest mocha.js supertest


    【解决方案1】:

    需要说明的是,supertest 没有删除方法,但正确的方法是 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 或字符串。

    【讨论】:

    【解决方案2】:

    看看supertest GitHub-page

    您可以将http.ServerFunction 传递给request()

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

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

    【讨论】:

    • 感谢您的回复。但我没明白。试图像这个请求(服务器)那样做,但得到了同样的错误。这不是我唯一的测试,还有其他人使用 post 并以完全相同的方式获得,但他们确实通过了。
    • 我只是在回到这里之前尝试过,它成功了!无论如何,非常感谢。这个花了我很多时间来弄清楚:/再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多