简体   繁体   English

单元测试 -> Sinon + fluent-ffmpeg

[英]Unit test -> Sinon + fluent-ffmpeg

I need some help with the following code:我需要以下代码的帮助:

const ffmpeg = require('fluent-ffmpeg'),
{PassThrough} = require('stream');

ffmpeg.setFfmpegPath('/opt/bin/ffmpeg');
ffmpeg.setFfprobePath('/opt/bin/ffprobe');

exports.wavCompilation = async (wavObjList, s3OutputFullPath) => {
    let command = ffmpeg();
    return new Promise(async (resolve, reject) => {

        const pt = new PassThrough()
        command
            .complexFilter(complexFilterList)
            .audioFrequency(44100)
            .audioChannels(2)
            .outputOptions('-map [audio]')
            .toFormat("wav")
            .output(pt, {end: true})
            .on('error', function (err, stdout, stderr) {
                console.log("stdout:\n" + stdout);
                console.log("stderr:\n" + stderr);
                if (err) {
                    console.log(err.message);
                    return reject("Error");
                }
            })
            .on('start', (commandLog) => printLog(commandLog))
            .on(`end`, () => console.info("end ffmpeg"))
            .run()

        resolve(***);
    });
}

Does anyone know how can I stub/mock/spy/fake "ffmpeg"?有谁知道我怎样才能存根/模拟/间谍/伪造“ffmpeg”?

I use Sinon for unit tests and I'm trying to use proxyquire to mock "fluent-ffmpeg"我使用 Sinon 进行单元测试,我正在尝试使用 proxyquire 来模拟“fluent-ffmpeg”

const sinon = require('sinon'),
proxyquire = require('proxyquire').noCallThru();

let ffmpegStub = {
    setFfmpegPath: () => {},
    setFfprobePath: () => {}
}

const ffmpegService = proxyquire('../services/ffmpeg-service/index', {
    'fluent-ffmpeg': ffmpegStub
});

for the methods setFfmpegPath and setFfprobePath it works but for the line:对于 setFfmpegPath 和 setFfprobePath 方法,它可以工作,但对于以下行:

let command = ffmpeg();

it says "ffmpeg is not a function"它说“ffmpeg不是一个函数”

I tried to add a default function such as:我尝试添加一个默认的 function 例如:

let ffmpegStub = {
    default: () => {},
    setFfmpegPath: () => {},
    setFfprobePath: () => {}
}

but it also doesn't work and I have no idea now what I could do.但它也不起作用,我现在不知道我能做什么。

Please I need some fresh ideas, my goal is to catch the argument for "complexFilter" and assert it.请我需要一些新鲜的想法,我的目标是抓住“complexFilter”的论点并断言它。

No need of proxyquire.不需要代理查询。

I've got what I wanted with the following:我已经得到了我想要的以下内容:

filterStub = sandbox.stub(ffmpeg.prototype, "complexFilter").returnsThis();
sandbox.stub(ffmpeg.prototype, "run").returnsThis();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM