Node.js Mocha异步测试,重用相同的请求

Node.js Mocha异步测试,重用相同的请求,node.js,unit-testing,mocha.js,Node.js,Unit Testing,Mocha.js,我正在测试一个HTTP服务器。我希望我能把done传递给description(),而不是仅仅传递给it(),类似这样的东西: var request = require('supertest'); describe('Item', function(done){ request(app).get('/').end(function(req, res){ it('should have one thing', function(){ // Ass

我正在测试一个HTTP服务器。我希望我能把
done
传递给
description()
,而不是仅仅传递给
it()
,类似这样的东西:

var request = require('supertest');

describe('Item', function(done){
    request(app).get('/').end(function(req, res){
        it('should have one thing', function(){
            // Assert on res.body
        });

        it('should have another thing', function(){
            // Assert on res.body
        });

        it('should have more things', function(){
            // Assert on res.body
        });

        done();
    });
});
这行不通,摩卡从来不会运行测试

下面的代码确实有效,但每次发出新的HTTP请求时,我都希望使用相同的代码

describe('Item', function(){
    it('should have one thing', function(done){
        request(app).get('/').end(function(req, res){
            // Assert on res.body
            done();
        }
    });

    it('should have another thing', function(done){
        request(app).get('/').end(function(req, res){
            // Assert on res.body
            done();
        }
    });

    it('should more things', function(done){
        request(app).get('/').end(function(req, res){
            // Assert on res.body
            done();
        }
    });
});

如何针对相同的响应运行测试?

如果您测试的是完全相同的请求,那么为什么
断言
会分布在多个测试中?IMHO有两种选择:

  • 要么它们真的属于一起,然后把所有的东西都放到一个测试中,你的问题就消失了
  • 或者它们不属于一个整体,那么就不会处理单个请求,而是处理单个请求以避免副作用。不管怎样,你的问题也会解决的
目前的测试方式可能是测试之间存在相互依赖关系,这是非常糟糕的测试风格


只有我的2美分。

我试图测试如下内容:1)-是JSON,2)-响应数据属性。你说得对,它们不属于一起。而且,如果我按照我的意愿去做,那么测试完成时间就会出现偏差。