2017-01-24 2 views
0

Я совершенно новый для кармы, и мне нужна отчаянная помощь на том же.Угловой UI Router Testing With Resolve

У меня есть мои маршруты определены как:

angular 
     .module('appRouter',['ui.router']) 
     .config(function($stateProvider,$urlRouterProvider){ 
      $stateProvider 
       .state('settings',{ 
        url:'/settings', 
        templateUrl:'templates/settings.html' 
       }) 
       .state('settings.profile',{ 
        url:'/profile', 
        templateUrl:'templates/profile.html' 
       }) 
       .state('settings.account',{ 
        url:'/account', 
        templateUrl:'templates/account.html', 
        controller:function(resolveData){ 
         console.log(resolveData); 
        }, 
        resolve:{ 
         resolveData : function($http){ 
          var root = 'https://jsonplaceholder.typicode.com'; 
          return $http.get(root+'/posts/1').then(function(response){ 

           return response.data; 
          }); 
         } 
        } 
       }); 

      $urlRouterProvider.otherwise('/settings/profile'); 

Теперь, чтобы проверить основные функции маршрутизации, я пытался что-то вроде:

describe('UI Routerer Config',function(){ 
    var $state,$rootScope,$httpBackend,state = "settings.profile"; 

    beforeEach(function(){ 
     module('appRouter'); 
    }); 

    beforeEach(inject(function(_$rootScope_,_$state_,$templateCache){ 
     $rootScope = _$rootScope_; 
     $state = _$state_; 
     $templateCache.put('templates/profile.html',''); 
    })); 

    it('should respond to url',function(){ 
     expect($state.href(state)).toBeDefined(); 
     expect($state.href(state)).toEqual('#!/settings/profile'); 
    }); 

}); 

Он прошел.

Но все же не могу проверить с помощью resolve.

Я не знаю, как это сделать?

Could anyone guide me and explain me how to incorporate the tests for the same.

Пожалуйста, объясните шаги, как новичку мне трудно.

UPDATE 2

describe('UI Routerer Config For Account',function(){ 
    var $state,$rootScope,$injector,$httpBackend,state = "settings.account"; 

    beforeEach(function(){ 
     module('appRouter'); 
    }); 

    beforeEach(inject(function(_$rootScope_,_$injector_,_$state_,$templateCache){ 
     $rootScope = _$rootScope_; 
     $state = _$state_; 
     $injector = _$injector_; 
     $templateCache.put('templates/account.html',''); 
    })); 


    it('should respond to url',function(){ 
     expect($state.href(state)).toBeDefined(); 
     expect($state.href(state)).toEqual('#!/settings/account'); 
    }); 

    it('should resolve "resolveData"',function(){ 
     const state = $state.get('settings.account'); 
     const resolveFn = state.resolve.resolveData; 

     const responseData = { 
      "userId": 1, 
      "id": 1, 
      "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", 
      "body": "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto" 
     }; 

     expect($injector.annotate(resolveFn)).toEqual(['$http']); 
     expect(resolveFn($http)).toEqual(responseData); 
    }); 

}); 

но он не говорил

should resolve "resolveData" 
    UI Routerer Config For Account 
    ReferenceError: $http is not defined 
    at Object.<anonymous> (test/controllers/main-controller-spec.js:97:26) 

ответ

0

Надеется, что это помогает, и это примерно от А config.routes я тестируемый:

inject((_$injector_, _$state_) => { 
    $injector = _$injector_; 
    $state = _$state_; 
}); 

describe('foo',() => { 
    it('should resolve "resolveData"',() => { 
    const state = $state.get('settings.account'); 
    const resolveFn = state.resolve.resolveData; 
    expect($injector.annotate(resolveFn)).toEqual(['$http']); 

    // It's here that you would mock out response.data and 
    // expect the resolveFn($http) to equal it; voila, you've 
    // tested that (one) resolve! 
    expect(resolveFn($http)).toEqual(); 
    }); 
}); 
+0

Спасибо за помощь. Но он не ошибся. См. Обновления. – StrugglingCoder

Смежные вопросы