我也是一个新手.2754846_我完成了Manuel Kiessling的书, The Node Beginner Book ,我正在完成他的第二本书 The Node Craftsman Book . 我坚持 FilesizeWatcher 教程 . 我已经能够运行早期的测试,但这个测试不起作用 . 在SO上有一个类似的问题:No output from jasmine-node但答案对我不起作用 .
我会在这里发布我的代码,希望有人可以告诉我我做错了什么 .
FilesizeWatcherSpec.js:
'use strict';
var FilesizeWatcher = require('./FilesizeWatcher');
var exec = require('child_process').exec;
describe('FilesizeWatcher', function() {
var watcher;
afterEach(function() {
watcher.stop();
});
it('should fire a "grew" event when the file grew in size', function(done) {
var path = './var/tmp/filesizewatcher.test';
exec('rm -f ' + path + ' ; touch ' + path, function() {
watcher = new FilesizeWatcher(path);
watcher.on('grew', function(gain) {
expect(gain).toBe(5);
done();
});
exec('echo "test" > ' + path, function(){});
});
});
it('should fire a "shrank" event when the file shrank in size', function(done) {
var path = './var/tmp/filesizewatcher.test';
exec('rm -f ' + path + ' ; echo "test" > ' + path, function() {
watcher = new FilesizeWather(path);
watcher.on('shrank', function(loss) {
expect(loss).toBe(3);
done();
});
exec('echo "a" > ' + path, function(){});
});
});
it('should fire an "error" if path does not start', function(done) {
var path = 'var/tmp/filesizewatcher.test';
watcher = new FilesizeWather(path);
watcher.on('error', function(err) {
expect(err).toBe('Path does not start with a slash');
done();
});
});
});
FilesizeWatcher.js:
'use strict';
var fs = require('fs');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var FilesizeWatcher = function (path) {
var self = this;
if (/^\//.test(path) === false) {
process.nextTick(function() {
self.emit('error', 'Path does not start with a slash');
});
return;
}
fs.stat(path, function (err, stats) {
console.log('stats= ' + stats);
self.lastfilesize = stats.size;
});
self.interval = setInterval(
function () {
console.log('We are in function()');
fs.stat(path, function (err, stats) {
if (stats.size > self.lastfilesize) {
self.emit('grew', stats.size - self.lastfilesize);
self.lastfilesize = stats.size;
}
if (stats.size < self.lastfilesize) {
self.emit('shrank', self.lastfilesize - stats.size);
self.lastfilesize = stats.size;
}
}, 1000);
});
};
util.inherits(FilesizeWatcher, EventEmitter);
FilesizeWatcher.prototype.stop = function () {
clearInterval(this.interval);
};
module.exports = FilesizeWatcher;
Console output:
C:\Users\pdl\Projects\NodeCraftsman>jasmine-node ./FilesizeWatcherSpec.js
C:\Users\pdl\Projects\NodeCraftsman>
Other tests run fine:
C:\Users\pdl\Projects\NodeCraftsmanTestDrivenDevelopment>jasmine-node spec\greetSpec.js
..
Finished in 0.006 seconds
2 tests, 2 assertions, 0 failures, 0 skipped
C:\Users\pdl\Projects\NodeCraftsmanTestDrivenDevelopment>
我添加 --captureExceptions 以查看是否可以获得任何信息,我得到了 TypeError: self.callbacks.error is not a function .
我的第一个问题是下面提到的Eppilo,我需要在self.callbacks 'error'上使用process.nextTick . 将异步代码与同步代码混合会导致在注册错误处理程序之前触发错误事件 . 所以我做了更改,现在使用EventEmitter,但我仍然遇到以下错误:
如果我在路径中包含".": var path = './var/tmp/filesizewatcher.test';
则会写入文件 . 否则,它没有 .
如果文件没有写入,stats = undefined我收到此错误:
TypeError: Cannot read property 'size' of undefined
at C:\Users\pdl\Projects\NodeCraftsman\FilesizeWatcher.js:19:34
at FSReqWrap.oncomplete (fs.js:82:15)
如果文件被写入,那么我收到此错误:
Error: Uncaught, unspecified "error" event. (Path does not start with a slash)
at emit (events.js:144:17)
at C:\Users\pdl\Projects\NodeCraftsman\FilesizeWatcher.js:12:18
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)
当然,它不应该以斜线开头 . 那是考验 . 但是当我从命令中删除--captureExceptions时,我仍然没有输出 .
2 回答
首先尝试在详细模式下运行Jasmine并捕获异常:
Link: https://github.com/mhevery/jasmine-node/wiki/Command-Line-Usage
还尝试使错误检查异步:
新手也没有足够的声誉评论 .
我的Mac上也没有相同的输出,并且能够让测试与此一起工作 .
FilesizeWatcher.js中存在错误 .
目前:
它应该是:
只是分享我的发现,欢呼 .