简体   繁体   English

使用express,node.js构建的restful api的单元测试记录(使用mocha,supertest和chai)

[英]Unit test logging (use mocha, supertest, and chai) of restful api built with express, node.js

I write the a simple unit test with mocha, chai and supertest. 我用mocha,chai和supertest写了一个简单的单元测试。

describe('controller.CWEManagementAPI', function () {
it('should be able to say hello', function() { 
    var request = require('supertest')
    , express = require('express');

    var app = express();

    app.get('/user', function(req, res){
        res.send(201, { name: 'tobi' });
    });

    request(app)
    .get('/user')
    .set('Accept', 'application/json')
    .expect(200)
    .end(function(err, res){
        if (err) return done(err);
        console.log('test');
        assert.equal( res.body.name, 'tobi');
        done()
    });
});
});

But the problem is that : the console.log('test') is not executed. 但问题是: console.log('test')没有执行。 So I think the assert.equal( res.body.name, 'tobi'); 所以我认为assert.equal( res.body.name, 'tobi'); is not executed either. 也没有执行。 And so I write the code without unit testing, like : 所以我编写的代码没有单元测试,如:

var request = require('supertest')
, express = require('express');

var app = express();

app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});

request(app)
   .get('/user')
   .expect('Content-Type', /json/)
   .expect('Content-Length', '20')
   .expect(201)
   .end(function(err, res){ 
    if (err) throw err;
    console.log(res.body.name);
    console.log('done');
    process.exit();
   });

And the console.log() are all executed. 并且console.log()都已执行。 so I don't know why the first code can not show the logging info. 所以我不知道为什么第一个代码不能显示日志信息。

What you are trying to run is an asynchronous test with mocha. 您要运行的是使用mocha的异步测试。 So the test case should receive a callback parameter to invoke once it's done with its actions. 因此,测试用例应该收到一个回调参数,一旦完成其动作就会调用。

You are invoking done() at the end but haven't received it as a parameter. 您最后调用done()但未将其作为参数接收。

change 更改

it('should be able to say hello', function() {
}

to

it('should be able to say hello', function(done) {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Mocha中使用Supertest来测试Node.js Express API和MongoDB - Using Supertest in Mocha to test Node.js Express API and MongoDB 如果需要登录,如何在Node.js服务器端使用Mocha Chai测试RESTful CRUD api? - How to test RESTful CRUD api with Mocha Chai on Node.js server side if login needed? 使用Mocha,Chai和Sinon对Node.js应用程序进行单元测试 - Unit Test a Node.js application with Mocha, Chai, and Sinon 单元测试 node.js mongoose mocha chai sinon - unit test node.js mongoose mocha chai sinon 如何在 Node.js 应用程序上使用相互 SSL 和测试框架 Mocha/Chai(chai-http) - How to use mutual SSL with test framework Mocha/Chai(chai-http) on a Node.js application 如何为 Node.js express 框架应用程序在 mocha 和 chai 中编写单元测试标头代码? - How to write code for unit testing header in mocha and chai for Node.js express framework application? Node.js / Express / Mocha / Supertest Rest API - 空请求正文 - Node.js / Express / Mocha / Supertest Rest API - Empty Request Body 测试REST API-未定义主体(Node.js / Express / Mocha / Supertest) - Testing REST API - req.body undefined (Node.js / Express / Mocha / Supertest) 单元/集成测试Express REST API,猫鼬,摩卡,sinon,chai,supertest - Unit/Integration testing Express REST API, mongoose, mocha, sinon, chai, supertest 在Mocha Javascript Node.js单元测试框架中未收到来自chai .post请求的响应 - Not getting a response from a chai .post request in mocha javascript node.js unit test framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM