【发布时间】:2014-03-18 18:17:27
【问题描述】:
我正在使用 AngularJS 开发 Chrome 应用程序。由于我的应用使用 chrome.storage,我编写了包装器:
angular.module('myApp').factory('chromeStorageApi', function($window){
if(typeof $window.chrome.storage == 'undefined')
return false;
return{
set:function(obj, callback){
$window.chrome.storage.local.set(obj, callback)
return true;
},
.............................
}
}
我违反了 TDD 方法,现在我想测试我的包装器。但我所有的尝试都没有成功。例如,我尝试检查$window.chrome.storage.local.set() 是否具有与chromeStorageApi.set() 相同的参数,但我找不到模拟$window.chrome.storage.local 的方法。
更新:
我上一个版本的单元测试:
describe('chromeStorageApi', function(){
beforeEach(module('myApp'));
it('should be able to set data to the storage', inject(function(chromeStorageApi, $window){
spyOn($window.chrome.storage.local, 'set')
chromeStorageApi.set({'key':'value'}, function(){ }());
expect($window.chrome.storage.local.set).toHaveBeenCalled();
expect($window.chrome.storage.local.set).toHaveBeenCalledWith({'key':'value'}, function(){ }());
}));
});
但我得到一个错误:
TypeError: 'undefined' 不是一个对象(评估 '$window.chrome.storage')
【问题讨论】:
标签: angularjs unit-testing google-chrome-app