[英]How can i mock underscorejs in unit testing? [angularjs]
我在我的應用程序中添加了下划線作為模塊。 我現在正在進行單元測試,我不知道我怎么能模擬下划線。
下划線模塊
var underscore = angular.module('underscore', []);
underscore.factory('_', function () {
// assumes underscore has already been loaded on the page
return window._;
});
單元測試
這有效,但我不想實現每個下划線功能...是否有另一種方法來使用underscorejs?
$provide.factory('_', function () {
var und = {};
und.findWhere = function () {
};
return und;
});
是的,它可以做到:
步驟1:在每個功能之前注入服務。
第2步:然后我們需要模擬下划線服務,所以寫:
spyOn(underscoreService,"methodName").and.callFake(function(){
return //whatever you want;
})
要么
spyOn(underscoreService,"methodName").and.callThrough()
我希望這對你有用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.