无法使用Mocha和Supertest测试DELETE方法 [英] Cant test DELETE method using mocha and supertest

查看:143
本文介绍了无法使用Mocha和Supertest测试DELETE方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为节点应用程序构建RESTful API. 我建立了路线,一切运行正常.但是,当我尝试对其进行测试时,尽管它通常无法在测试中运行,但它无法使DELETE方法起作用.

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;

测试:

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

推荐答案

看看 supertest GitHub-page .

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

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

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

如评论和@mpm:s所示,问题是保留函数delete()的使用而不是特定于软件包的函数del().

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

这篇关于无法使用Mocha和Supertest测试DELETE方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆