如何通过Mocha& Chai测试仅运行node-fluent-ffmpeg命令的自定义模块?
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(); } }这是我尝试过的:
// 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:预计未定义为函数
1) Segment Splicer should work: AssertionError: expected undefined to be a function
因为我从segment_splicer.spice方法中收到了undefined.
Because I receive undefined from segment_splicer.spice method.
谢谢!
推荐答案该测试应该通过.
仅当您断言或期望测试中的某些内容不正确或被测对象抛出未捕获的错误时,测试才会失败.
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.
您没有在测试中声明任何内容,并且您的主题将抛出的唯一错误是,如果您传递的参数少于2个,那么测试中就不会出现这种情况.
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.
ffmpeg方法似乎也是异步的,这与您构造测试的方式不兼容.
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:
- 摩卡如何进行测试异步JavaScript进程 好玩
- 异步单元测试与摩卡咖啡,诺言和 WinJS
- 测试异步 JavaScript
- How Mocha Makes Testing Asynchronous JavaScript Processes Fun
- Asynchronous Unit Tests With Mocha, Promises, And WinJS
- Testing Asynchronous JavaScript
通过引用done参数,您已经做了一些事情.如果指定了此选项,则Mocha将等到调用它之后再考虑测试已完成.
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.
更多推荐
如何测试运行node
发布评论