How to test a custom module running node-fluent-ffmpeg (an async module)?

泄露秘密 提交于 2019-12-25 04:24:09

问题


How do I test a custom module which is simply running a node-fluent-ffmpeg command with Mocha&Chai?

// 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();
    }
}

Here is what I tried:

// 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();
    });
});

I get this:

1) Segment Splicer should work: AssertionError: expected undefined to be a function

Because I receive undefined from segment_splicer.spice method.

Thank you!


回答1:


This test should be passing.

A test will only fail if you either assert or expect something in the test which is not true, or if the subject under test throws an uncaught error.

You are not asserting anything in your test, and the only error your subject will throw is if you pass less than 2 arguments, which is not the case in your test.

The ffmpeg method also seems to be asynchronous, which is not compatible with the way you have structured your test.

There are many examples available on setting up async tests, including:

  • How Mocha Makes Testing Asynchronous JavaScript Processes Fun
  • Asynchronous Unit Tests With Mocha, Promises, And WinJS
  • Testing Asynchronous JavaScript

You've gone some way to doing this by referencing the done argument. When this is specified, Mocha will wait until it is called before considering the test finished.



来源:https://stackoverflow.com/questions/29133140/how-to-test-a-custom-module-running-node-fluent-ffmpeg-an-async-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!