SinonJS 存根在 .on 范围内不起作用


it('POST /direct/bulk', function () {
const file = getFile('notif-direct-bulk.csv')
sinon.stub(notificationService.constructor.prototype, 'validateNotification').resolves(true)
sinon.stub(notificationService.constructor.prototype, 'createInAppNotification').resolves(true)
sinon.stub(fileService.constructor.prototype, 'storeFile').resolves(file.path)
sinon.stub(fileService.constructor.prototype, 'deleteFile').returns(true)
notificationService.validateNotification() // it's a stub
.then((valid) => { 
return fileService.storeFile() // it's a stub 
})
.then((storedFilePath) => {
const reader = new require('line-by-line')(storedFilePath)
console.log(notificationService.createInAppNotification()) // it's still a stub 
console.log(fileService.deleteFile()) // it's still a stub 
reader
.on('line', (line) => {
// inside this scope the stubs are not working
notificationService.createInAppNotification() // calling the original method, not a stub
})
.on('end', () => {
// inside this scope the stubs are not working
fileService.deleteFile() // calling the original method, not a stub
})
})
})

为什么所有存根在 .on 范围函数中都不起作用?

我想知道为什么会这样? .on 作用域与 sinonjs 作用域是否不同?

有趣的是,所有存根都应该按预期工作。

我不知道你的实现,但我可以根据你的测试代码给你这个例子。我假设几个类(通知服务,文件服务(来简化和满足测试。

高亮:

  • 我根据 npm 逐行示例启动阅读器
  • 在读者,我确保调用所有存根。
  • 我使用 done 实现摩卡异步代码。
const sinon = require('sinon');
const { expect } = require('chai');
const LineByLinereader = require('line-by-line');
// Example class to show that real method not get called.
const notificationService = {
validateNotification() {
console.log('Real validateNotification get called');
// Return the opposite value based on your test (true).
return new Promise((r) => r(false));
},
createInAppNotification() {
console.log('Real createInAppNotification get called');
// Return the opposite value based on your test (true).
return new Promise((r) => r(false));
},
};
// Example class to show that real method not get called.
const fileService = {
storeFile() {
console.log('Real storeFile get called');
// Return the opposite value based on your test (string not empty).
return new Promise((r) => r(''));
},
deleteFile() {
console.log('Real deleteFile get called');
// Return the opposite value based on your test (true).
return false;
},
};
function getFile() {
// Return real dummy file path.
return {
path: './test.txt',
};
}
// Mocha asynchronous code using done.
it('POST /direct/bulk', (done) => {
const file = getFile('notif-direct-bulk.csv');
const stubValidateNotification = sinon.stub(notificationService, 'validateNotification');
stubValidateNotification.resolves(true);
const stubCreateInAppNotification = sinon.stub(notificationService, 'createInAppNotification');
stubCreateInAppNotification.resolves(true);
const stubStoreFile = sinon.stub(fileService, 'storeFile');
stubStoreFile.resolves(file.path);
const stubDeleteFile = sinon.stub(fileService, 'deleteFile');
stubDeleteFile.returns(true);
notificationService.validateNotification() // it's a stub
.then(() => fileService.storeFile()) // it's a stub
.then((storedFilePath) => {
// Expect the result is correct with file above.
expect(storedFilePath).to.equal(file.path);
// Initiate synchronous processing of lines.
const reader = new LineByLinereader(storedFilePath);
console.log(notificationService.createInAppNotification()); // it's still a stub
console.log(fileService.deleteFile()); // it's still a stub
reader
.on('line', async () => {
// inside this scope the stubs are not working
const result = await notificationService.createInAppNotification(); // calling the original method, not a stub
expect(result).to.equal(true);
})
.on('end', () => {
// inside this scope the stubs are not working
const result = fileService.deleteFile(); // calling the original method, not a stub
expect(result).to.equal(true);
// Expect all stub get called once.
expect(stubValidateNotification.calledOnce).to.equal(true);
expect(stubCreateInAppNotification.calledOnce).to.equal(true);
expect(stubStoreFile.calledOnce).to.equal(true);
// You expect this stub to get called twice.
expect(stubDeleteFile.calledTwice).to.equal(true);
// Finish the test.
done();
});
});
});

当我运行它时,没有调用真正的方法。

$ npx mocha stackoverflow.js

Promise { true }
true
✓ POST /direct/bulk
1 passing (35ms)
$

希望这有帮助。

最新更新