0
のは、我々は次のような角度のコントローラを持っていると言いましょう:
.controller('Ctrl', function ($scope, localStorageService) {
$scope.vma = localStorageService.get('vma_vma') || 10;
}
それは初期化時に、外部サービス(localStorageService)を使用しています。 Basicelly localStorageService.getは、以前に格納された 'vma_vma'の値を返します。存在しない場合はnullを返します。
私はlocalStorageServiceのモックを作ったし、ユニットテストは、次のようになります。
describe('Controller: Ctrl', function() {
// load the controller's module
beforeEach(module('App', function($provide){
localStorage = {};
localStorage.get = jasmine.createSpy();
localStorage.set = jasmine.createSpy();
$provide.value('localStorageService', localStorage);
}));
var Ctrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
Ctrl = $controller(' Ctrl', {
$scope: scope,
localStorageService: localStorageService
});
}));
私は2例をテストできるようにしたいと思います:
it('should get vma and return 10 when vma not present', function() {
???
expect($scope.vma).toBe(10);
});
it('should get vma in localstorage when present', function() {
???
expect($scope.vma).toBe(15);
});
});
はあなたをありがとう助けて。