簡體   English   中英

如何使用Jasmine監視從一個類調用的方法,該方法存在於另一個類中?

[英]How do I spy on methods called from one class that exist in another class using Jasmine?

我有辦法

thisSvc.asyncOperation: function(id) {
   return thatSvc.getById(id);

是否有可能創建一個間諜來告訴我是否已調用thatSvc.getById,或者該設計是否是反模式? AFAIK,只能在單個對象上創建間諜。

來自http://www.htmlgoodies.com/html5/javascript/spy-on-javascript-methods-using-the-jasmine-testing-framework.html#fbid=ib4OX6qA3oS

“只有在對象上已經存在該方法時,才可以使用spyOn()。對於簡單測試,這是最好的選擇。”

您可以監視任何想要的東西,在茉莉花測試中只需確保獲得該服務即可:

var thisSvc, thatSvc;

beforeEach(inject(function(_thisSvc_, _thatSvc_){
    thisSvc = _thisSvc_;
    thatSvc = _thatSvc_;
});

it('.asyncOperation should call thatSvc.getById', function(){
    spyOn(thatSvc, 'getById');
    var id = 4;

    thisSvc.asyncOperation(id);

    expect(thatSvc.getById).toHaveBeenCalledWith(id);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM