我想在一个类内存一个私有(private)变量
class IPC {
private publisher: redis.RedisClient;
constructor() {
this.publisher = redis.createClient();
}
publish(text: string) {
const msg = {
text: text
};
this.publisher.publish('hello', JSON.stringify(msg));
}
}
我如何在此类中对私有(private)变量
publisher
stub ?所以我可以测试如下所示的代码
it('should return text object', () => {
const ipc = sinon.createStubInstance(IPC);
ipc.publish('world!');
// this will throw error, because ipc.publisher is undefined
assert.deepStrictEqual({
text: 'world!'
}, ipc.publisher.getCall(0).args[0])
})
最佳答案
您可以使用类型断言来访问私有(private)变量。喜欢:
(ipc as any).publisher
关于unit-testing - typescript 中的Sinon stub 私有(private)变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42390900/