gpt4 book ai didi

javascript - 如何模拟 readline.createInterface()?

转载 作者:行者123 更新时间:2023-11-28 20:18:43 24 4
gpt4 key购买 nike

我正在尝试在 jasmine 上编写测试以检查是否调用了 readline.createInterface(),但我不断收到一条错误消息:TypeError: readline.createInterface is not一个函数

这是我在游戏课上的大致内容:

run() {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'OHAI> '
});

rl.prompt();
// ... and the rest ...
}

和我的测试:

describe('run', () => {
it('should create readline interface', () => {
let readline = jasmine.createSpyObj('readline', ['createInterface']);

game.run();

expect(readline.createInterface).toHaveBeenCalled();
});
});

有人有什么建议吗?

最佳答案

尝试以下代码(见上文)并使用 rewire

const rewire = require('rewire')
const game = rewire('path/to/game')

describe('run', () => {
it('should create readline interface', () => {
const readline = jasmine.createSpyObj('readline', ['createInterface']);
const revert = game.__set__('readline', readline);

game.run();

expect(readline.createInterface).toHaveBeenCalled();

revert();
});
});

关于javascript - 如何模拟 readline.createInterface()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com