当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - Angular 2 unit tests: How to test an expectation within a stream's "data" or "end" functions? (fs.createReadStream)

问题描述:

So I have this app code. Since it is about an asynchronous stream, I don't know how to test something that happens within the stream.

let stream: any = fs.createReadStream(filePath, {start: 12, end: 15});

stream.on('data', function (fourByteStream: any): void {

if ((fourByteStream[0] === 65) && (fourByteStream[1] === 66) &&

(fourByteStream[2] === 67) && (fourByteStream[3] === 68)) { // check the four bytes of the stream

analyzedString.passes = true;

}

});

stream.on('end', () => {

this._replaySubjectSource.next(analyzedString);

});

I just want this test expectation to pass

 spyOn(mySvc._replaySubjectSource, "next");

expect(mySvc._replaySubjectSource.next).toHaveBeenCalled();

Whether I inject the service or make it standalone pure Jasmine (sync or async with "done"), it does not. I also tried a setTimeOut function in the test, but basically nothing in that was ever run.

分享给朋友:
您可能感兴趣的文章:
随机阅读: