【发布时间】:2016-08-02 06:55:13
【问题描述】:
我正在使用业力、角度和摩卡咖啡。
我要注入的MyServices 模块。它是addThing 方法也调用了$localStorage 提供程序,我想测试当我调用MyServices.addThing(data) 时$localStorage.setItem 方法已被隐式调用
这是我的代码:
describe('MyServices', function() {
var $q;
var $rootScope;
var $scope;
var MyServices;
var $timeout;
var $httpBackend;
// inject modules
beforeEach(function(){
module('my.stuff');
var _LocalStorage_ = {
setItem: function(){
return 'foo';
}
};
// mock the dependencies of the module we are testing
module(function ($provide) {
$provide.value('$localStorage', _LocalStorage_); //here is where the error occurs
});
});
beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_, _MyServices_, _$httpBackend_) {
$q = _$q_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
MyServices = _MyServices_;
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
}));
it.only('should call a method and access localStorage', function(){
var spyAdd = sinon.spy(MyServices, 'addThing');
var spySetItem = sinon.spy($localStorage, 'setItem');
MyServices.addThing({ name: 'Thing' });
expect(spyAdd.callCount).to.equal(1);
expect(spySetItem.callCount).to.equal(1);
})
});
当我运行它时,我得到一个错误,$localStorage 未定义。
【问题讨论】:
标签: angularjs unit-testing karma-runner karma-mocha