我正在尝试使用grunt,mocha和chai为节点api编写测试。我无法弄清楚如何require
其他图书馆。我是grunt,mocha,chai和测试的初学者...
我的Gruntfile:
// Gruntfile.js
module.exports = function(grunt){
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Mocha Test
mochaTest: {
test: {
options: {
reporter: 'list',
timeout: 15000
},
src: ['test/groupstest.js']
}
}
});
// Load grunt mocha task
grunt.loadNpmTasks('grunt-mocha');
grunt.loadNpmTasks('grunt-mocha-test');
};
文件结构:
├── package.json
├── Gruntfile.js
├── test
│ └── groupstest.js
└── ...
groupsheets:
'use strict';
var request = require('supertest');
var expect = require("chai").expect;
var app = require('../middleware/express');
describe('Routes', function(){
describe('/groups - GET', function(){
it('- should GET users', function(done){
console.log(request(app).get);
request(app)
.get('/groups', function(err, res, body){
...
done();
});
});
});
我在app.js文件中模仿快递应用。然后我正在测试一个GET路由,但它说没有定义“require”。我对如何解决这个问题有任何想法?我觉得我很亲密。
更新:
所以我输入grunt mochaTest
进行测试。问题在于它超时,我无法弄清楚原因。
这是错误:
Routes /groups - GET - should GET users: [Function]
1) Routes /groups - GET - should GET users
0 passing (15s)
1 failing
1) Routes /groups - GET - should GET users:
Error: timeout of 15000ms exceeded. Ensure the done() callback is being called in this test.
Warning: Task "mochaTest:test" failed. Use --force to continue.
Aborted due to warnings.
答案 0 :(得分:1)
您的问题与您使用超级用户的方式有关。有关示例,请参阅下面的代码段。
it('- should GET users', function(done){
request(app)
.get('/groups')
// you can add supertest expectations here
.end(function(err, res){
...
done();
});
});