如何测试运行node-fluent-ffmpeg(异步模块)的自定义模块?

时间:2015-03-18 21:33:57

标签: javascript node.js testing mocha chai

如何测试一个只使用Mocha& Chai运行node-fluent-ffmpeg命令的自定义模块?

// segment_splicer.js
var config = require('./../config');
var utilities = require('./../utilities');
var ffmpeg = require('fluent-ffmpeg');

module.exports = {
    splice: function(raw_ad_time, crop) {
        if (!raw_ad_time || !crop) throw new Error("!!!!!!!!!! Missing argument");
        console.log("@@@@@ LAST SEGMENT IS BEING SPLITTED.");
        var segment_time = utilities.ten_seconds(raw_ad_time);
        var last_segment_path = config.akamai_user_base + 'segment' + (segment_time + 1) + "_" + config.default_bitrate + "_av-p.ts?sd=10&rebase=on";
        var command = ffmpeg(last_segment_path)
            .on('start', function(commandLine) {
                console.log('@@@@@ COMMAND: ' + commandLine);
            })
            .seekInput('0.000')
            .outputOptions(['-c copy', '-map_metadata 0:s'])
            .duration(crop)
            .on('error', function(err, stdout, stderr) {
                throw new Error('@@@@@ VIDEO COULD NOT BE PROCESSED: ' + err.message);
                console.log('@@@@@ VIDEO COULD NOT BE PROCESSED: ' + err.message);
            })
            .output('public/' + 'segment' + (segment_time + 1) + "_" + config.default_bitrate + "_av-p.ts").run();
    }
}

以下是我的尝试:

// test/segment_splicer.js
var expect = require('chai').expect;
var segment_splicer = require('../lib/segment_splicer');


describe('Segment Splicer', function() {
    it('should work', function(done) {
        expect(segment_splicer.splice(1111111, 20)).to.throw(Error);
        done();
    });
});

我明白了:

  

1)Segment Splicer应该工作:        AssertionError:期望未定义为函数

因为我从segment_splicer.spice方法收到undefined

谢谢!

1 个答案:

答案 0 :(得分:0)

此测试通过。

如果您断言或期望测试中的某些内容不正确,或者测试中的主题引发未被捕获的错误,则测试将失败。

您没有在测试中断言任何内容,并且您的主题将抛出的唯一错误是您传递少于2个参数,而在您的测试中并非如此。

ffmpeg方法似乎也是异步的,这与您构建测试的方式不兼容。

有许多关于设置异步测试的例子,包括:

通过引用done参数,您已经在某种程度上做到了这一点。如果指定了这个,Mocha将等到它被调用之后才考虑完成测试。

相关问题