我想存根類Sinon stub私有變量在打字稿中?
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));
}
}
我怎樣才能存根私有變量publisher
,這裏面類中的私有變量? 所以我可以測試代碼如下所示
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])
})
但我怎能ST UB呢? – Tim
您的意思是:'sinon.stub(ipc,'publisher');'? –
我想測試我的代碼,如上圖所示,但我無法實現它,它會抱怨'不能讀取屬性'的getCall'的undefined',所以我不知道如何實現我想要的測試.. – Tim