我正在尝试使用 grunt、mocha 和 chai 为 Node api 编写测试。我不知道如何需要
其他库。我是 grunt、mocha、chai 和一般测试的初学者......
我的 Grunt 文件:
// 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
└── ...
分组表:
'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 文件中的 Express 应用程序。然后我正在测试 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.
最佳答案
您的问题与您使用 super 测试的方式有关。请参阅下面的代码片段作为示例。
it('- should GET users', function(done){
request(app)
.get('/groups')
// you can add supertest expectations here
.end(function(err, res){
...
done();
});
});
关于node.js - grunt mocha 未找到目标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431443/