【问题标题】:How to test a custom module running node-fluent-ffmpeg (an async module)?如何测试运行 node-fluent-ffmpeg 的自定义模块(异步模块)?
【发布时间】:2015-03-19 06:06:52
【问题描述】:

如何测试一个自定义模块,该模块只是使用 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) 段拼接器应该可以工作: AssertionError: 期望 undefined 是一个函数

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

谢谢!

【问题讨论】:

    标签: javascript node.js testing mocha.js chai


    【解决方案1】:

    这个测试应该通过了。

    只有当您断言或期望测试中的某些内容不正确,或者被测对象抛出未捕获的错误时,测试才会失败。

    你没有在你的测试中断言任何东西,你的主题会抛出的唯一错误是如果你传递的参数少于 2 个,而在你的测试中不是这样。

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

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

    通过引用done 参数,您已经采取了某种方式来做到这一点。指定后,Mocha 将等到它被调用后再考虑测试完成。

    【讨论】:

    • 感谢您的回答。实际上我的问题是“如何以测试异步内容的方式构建我的测试”? (是的,我忘记了错误:))
    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多