【问题标题】:Testing Chrome Storage API wrapper测试 Chrome 存储 API 包装器
【发布时间】: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


    【解决方案1】:

    我为我做了工作测试。这里有:

    describe('chromeStorageApi', function(){
        var mockWindow, chromeStorageApi;
    
        beforeEach(module('myApp'));
    
        beforeEach(function(){
            mockWindow = {
                chrome:{
                    storage:{
                        local: sinon.stub({
                            set: function(){ },
                            get: function(){ },
                            remove: function(){ },
                            clear: function(){ }
                        })
                    }
                },
                addEventListener: function(){    }
            }
            module(function($provide){
                $provide.value('$window', mockWindow);
            })
        })
    
        beforeEach(inject(function(_chromeStorageApi_){
            chromeStorageApi =_chromeStorageApi_;
        }))
    
        it('should be able to set data to the storage', function(){
            chromeStorageApi.set({'key':'value'}, function(){ }());
            expect(mockWindow.chrome.storage.local.set).toHaveBeenCalled();
            expect(mockWindow.chrome.storage.local.set).toHaveBeenCalledWith({'key':'value'}, function(){ }());
    
        });
    
        it('should be able to get data from the storage', function(){
            chromeStorageApi.get('key', function(){ });
            expect(mockWindow.chrome.storage.local.get).toHaveBeenCalled();
            expect(mockWindow.chrome.storage.local.get).toHaveBeenCalledWith('key');
        })
    })
    

    我正在使用sinonJS 创建带有方法的存根。我希望它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      相关资源
      最近更新 更多