0
我正在用Jasmine框架進行一些測試,我想窺探我正在使用的pub sub publish方法。然而,發佈方法發射兩次,我只是想第二次監視它。我想只是publish
方法複製到另一個方法,然後窺視原來的方法,像這樣:複製一個沒有「引用」原始方法的對象方法
pubsub.publishCopy = pubsub.publish;
spyOn(pubsub, "publish");
// this call has another call to pubsub.publish. it is this
// 2nd pub sub call that I want to spy on
pubsub.publishCopy(args);
// this shows an error because the pubsub.publishCopy is 'pointing to
// pubsub.publish instead of being a new function with the same functionality
expect(pubsub.publish).toHaveBeenCalledWith(differentArgs);
然而,所有我做的只是引用pubsub.publish
方法當我打電話pubsub.publishCopy
它拋出一個錯誤在我的測試中。如何正確複製發佈方法以適應我的需求?
它仍然給我,因爲this.publish同樣的錯誤仍然工作原指pubsub.publish方法。 – 2012-02-15 01:15:57
經過進一步調查,您的方法正在工作,問題與我如何設置我的測試有關。謝謝! – 2012-02-15 01:32:26