我是Node.js和jasmine的新手,我的JavaScript体验老旧而生锈,所以我也是新手。我完成了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;
控制台输出:
C:\Users\pdl\Projects\NodeCraftsman>jasmine-node ./FilesizeWatcherSpec.js
C:\Users\pdl\Projects\NodeCraftsman>
其他测试运行良好:
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不是函数。
我的第一个问题是下面提到的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时,我仍然没有输出。
答案 0 :(得分:2)
首先尝试在详细模式下运行Jasmine并捕获异常:
jasmine-node ./FilesizeWatcherSpec.js --verbose --captureExceptions
链接: https://github.com/mhevery/jasmine-node/wiki/Command-Line-Usage
还尝试使错误检查异步:
if (/^\//.test(path) === false) {
process.nextTick(function() {
self.callbacks['error']('Path does not start with a slash');
});
return;
}
答案 1 :(得分:1)
新手也没有足够的声誉来发表评论。
我的Mac上也没有相同的输出,并且能够使用此测试。
FilesizeWatcher.js中存在错误。
目前:
self.interval = setInterval(
function (){
...
fs.stat(path, function (err, stats) {
...
}, 1000);
});
应该是:
self.interval = setInterval(
function (){
...
fs.stat(path, function (err, stats) {
...
});
},1000);
只是分享我的发现,欢呼。