FileizeizeWatcherSpec上的jasmine-node没有输出-新手警报 [英] No output from jasmine-node on FilesizeWatcherSpec - Newbie Alert

查看:83
本文介绍了FileizeizeWatcherSpec上的jasmine-node没有输出-新手警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node.js和jasmine的新手,我的JavaScript经验又旧又生锈,所以我也是那里的新手.我完成了Manuel Kiessling的书 The Node Beginner Book ,而我正在研究他的第二本书 The Node Craftsman Book .我被困在 FilesizeWatcher 教程中.我已经能够运行较早的测试,但是此测试无法正常工作. SO上也有类似的问题: 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时,仍然没有输出.

解决方案

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

I'm new to Node.js and jasmine, and my JavaScript experience is old and rusty, so I'm a newbie there too. I finished Manuel Kiessling's book, The Node Beginner Book, and I am working my way through his second book, The Node Craftsman Book. I'm stuck on the FilesizeWatcher tutorial. I've been able to run earlier tests but this one is not working. There is a similar question on SO: No output from jasmine-node but the answer isn't working for me.

I'll post my code here and hopefully somebody can tell me what I'm doing wrong.

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>

I added --captureExceptions to see if I could get any information and I got the TypeError: self.callbacks.error is not a function.

My first problem was as Eppilo suggested below, that I needed to use process.nextTick on self.callbacks'error'. Mixing async code with sync code causes the error event to be fired before the error handler is registered. So I made the changes and am now using the EventEmitter but I'm still getting the following errors:

If I include the "." in the path: var path = './var/tmp/filesizewatcher.test'; then the file gets written. Otherwise, it does not.

If the file does NOT get written, stats= undefined and I receive this error:

TypeError: Cannot read property 'size' of undefined
    at C:\Users\pdl\Projects\NodeCraftsman\FilesizeWatcher.js:19:34
    at FSReqWrap.oncomplete (fs.js:82:15)

If the file DOES get written, then I receive this error:

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)

Of course, it's not supposed to start with a slash. That is the test. But when I remove the --captureExceptions from the command, I still get no output.

解决方案

First of all try and run Jasmine on verbose mode and capture exceptions:

jasmine-node ./FilesizeWatcherSpec.js --verbose --captureExceptions

Link: https://github.com/mhevery/jasmine-node/wiki/Command-Line-Usage

Also try to make the error checking asynchronous:

if (/^\//.test(path) === false) {
    process.nextTick(function() {
        self.callbacks['error']('Path does not start with a slash');
    });
    return;
}

这篇关于FileizeizeWatcherSpec上的jasmine-node没有输出-新手警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆