我一直在尝试使用mocha测试我的测试服务器.这是我使用的以下代码,几乎与另一个类似帖子中的代码相同.
beforeEach(function(done) {
// Setup
console.log('test before function');
ws.on('open', function() {
console.log('worked...');
done();
});
ws.on('close', function() {
console.log('disconnected...');
});
});
afterEach(function(done) {
// Cleanup
if(readyState) {
console.log('disconnecting...');
ws.close();
} else {
// There will not be a connection unless you have done() in beforeEach, socket.on('connect'...)
console.log('no connection to break...');
}
done();
});
describe('WebSocket test', function() {
//assert.equal(response.result, null, 'Successful Authentification');
});
Run Code Online (Sandbox Code Playgroud)
问题是,当我执行此草稿时,在命令提示符下看不到预期会看到的console.log.你能告诉我我做错了什么吗?
Lou*_*uis 23
Georgi是正确的,你需要一个it指定测试的电话,但describe如果你不想,你不需要在你的文件中有一个顶级.你可以用describe一堆it电话替换你的单曲:
it("first", function () {
// Whatever test.
});
it("second", function () {
// Whatever other test.
});
Run Code Online (Sandbox Code Playgroud)
如果您的测试套件很小并且只由一个文件组成,那么这种方法非常有效.
如果您的测试套件是较大或多个文件中传播,我会强烈建议你把你beforeEach和afterEach与您一同it里面的describe,除非你是绝对肯定的是,在套房每一个测试需要所做的工作beforeEach或afterEach.(我已经用Mocha编写了多个测试套件,我从来没有一个beforeEach或者afterEach我需要为每个测试运行.)类似于:
describe('WebSocket test', function() {
beforeEach(function(done) {
// ...
});
afterEach(function(done) {
// ...
});
it('response should be null', function() {
assert.equal(response.result, null, 'Successful Authentification');
});
});
Run Code Online (Sandbox Code Playgroud)
如果你不把你beforeEach和afterEach内部describe尚且如此,那么让我们假设你有一个文件来测试网络套接字和其他文件来测试一些数据库操作.包含数据库运行试验,将文件中的测试也有你beforeEach和afterEach前后执行它们.把beforeEach和afterEach里面的describe如上图所示一样将确保他们为您的网络插座测试只进行.
您的示例中没有测试.如果没有要运行的测试,则不会调用挂钩之前和之后.尝试添加如下测试:
describe('WebSocket test', function() {
it('should run test and invoke hooks', function(done) {
assert.equal(1,1);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45222 次 |
| 最近记录: |