gpt4 book ai didi

javascript - FilesizeWatcherSpec 上的 jasmine-node 没有输出 - 新手警报

转载 作者:行者123 更新时间:2023-11-28 18:44:19 25 4
gpt4 key购买 nike

我是 Node.js 和 jasmine 的新手,而且我的 JavaScript 经验又老又生疏,所以我也是新手。我读完了 Manuel Kiessling 的书《 Node 初学者书》,并且正在阅读他的第二本书《 Node 工匠书》。我陷入了 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 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 时,我仍然没有得到任何输出。

最佳答案

首先尝试在详细模式下运行 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;
}

关于javascript - FilesizeWatcherSpec 上的 jasmine-node 没有输出 - 新手警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com