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范围不同吗?
有趣的是,所有存根都应按预期工作。
我不知道您的实现,但是我可以根据您的测试代码为您提供此示例。我假设多个类(notificationService,fileService)以简化并满足测试要求。
突出显示:
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) $
希望这会有所帮助。