FilesizeWatcherSpec - Newbie Alert上的jasmine-node没有输出

No output from jasmine-node on FilesizeWatcherSpec - Newbie Alert

本文关键字:输出 jasmine-node 上的 Newbie Alert FilesizeWatcherSpec      更新时间:2023-09-26

我是 Node.js 和 jasmine 的新手,我的 JavaScript 经验又老又生疏,所以我也是新手。我读完了曼努埃尔·基斯林(Manuel Kiessling)的书《节点初学者书》(The Node Beginner Book),我正在研究他的第二本书《节点工匠书》(The Node Craftsman Book)。 我被困在FilesizeWatcher教程上。 我已经能够运行早期的测试,但这个不起作用。 SO上有一个类似的问题:茉莉花节点没有输出,但答案对我不起作用。

我会在这里发布我的代码,希望有人能告诉我我做错了什么。

文件大小观察者规范.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();
        });
    });
});

文件大小观察者.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。将异步代码与同步代码混合会导致在注册错误处理程序之前触发错误事件。所以我进行了更改,现在正在使用事件发射器,但我仍然收到以下错误:

如果我在路径中包含".":var path = './var/tmp/filesizewatcher.test';则文件将被写入。 否则,它不会。

如果文件没有被写入,stats=未定义,我收到此错误:

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;
}

也是新手,没有足够的声誉来评论。

我在Mac上也得到了相同的无输出,并且能够让测试使用它。

文件大小观察器.js中存在错误。

现在:

self.interval = setInterval(
    function (){ 
       ...          
       fs.stat(path, function (err, stats) {
          ...
    }, 1000); 
});

相反,它应该是:

self.interval = setInterval(
    function (){ 
       ...          
       fs.stat(path, function (err, stats) {
          ...
    }); 
},1000);

只是分享我的发现,干杯。