简体   繁体   English

测试事件sinon node.js

[英]testing events sinon node.js

I want testing my function and I stuck with mocking events. 我想测试我的功能,并且坚持使用模拟事件。 I don't know how mock event with sinon. 我不知道如何与sinon一起模拟事件。 This is my code when I stuck: 这是我卡住时的代码:

return pdfGenerator(invoice)
                            .then(content =>
                            {
                                const printer = new PdfMakePrinter(fonts);
                                let pdfDoc = {};
                                try {
                                    pdfDoc = printer.createPdfKitDocument(content);
                                } catch (error) {
                                    throw applicationException.new(applicationException.ERROR, 'Something bad in pdf content: ' + error);
                                }

                                let filepath = path.join(__dirname, '../REST/uploads/', filename);
                                let result = pdfDoc.pipe(fs.createWriteStream(filepath));
                                pdfDoc.end();
                                return new Promise(resolve =>
                                {
                                    result.on('finish', resolve);
                                })
                            })

Problem occured when I want test 我要测试时出现问题

result.on('finish',resolve);

This is my test: 这是我的测试:

let pdfGeneratorMock = sinon.stub();
let endMock = sinon.stub().callsFake(function ()
{
    return 0;
});
let pipeMock = sinon.spy();
let createPdfKitDocumentMock = sinon.spy(() =>
{
    return {
        end: endMock,
        pipe: pipeMock
    }
});
let pdfMakePrinterMock = sinon.spy(function ()
{
    return {
        createPdfKitDocument: createPdfKitDocumentMock
    }
});
let onMock = sinon.spy(function(text,callback){
    return callback();
});
let writeStreamMock = sinon.spy(() =>
{
    return {
        on: onMock
    }
});
let fs = {
    mkdirSync: sinon.spy(),
    createWriteStream: writeStreamMock
};
........

it('should call createPefKitDocument', function ()
  {
       expect(createPdfKitDocumentMock).callCount(1);
  });
it('should call fs.createWriteStream', function ()
{
    expect(writeStreamMock).callCount(1);
});

it('should call pipe', function ()
{
    expect(pipeMock).callCount(1);
});
it('should call end', function ()
{
    expect(endMock).callCount(1);
});

it('should call on', function ()
{

    expect(onMock).callCount(1);
});

Test not pass to onMock call and I don't have idea how mock this event and resolves to next then. 测试未传递给onMock调用,我不知道如何模拟此事件,然后解决下一个问题。

I resolved my problem with using yields. 我解决了使用收益的问题。 Change pipeMock to stub: 将pipeMock更改为存根:

let pipeMock = sinon.stub();

In before() pipeMock return yields: 在before()pipeMock返回收益:

pipeMock.returns({ on: sinon.stub().yields()});

and now test call calback and resolve promise 现在测试电话回拨并解决承诺

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

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