6

Jak mogę przetestować urządzenie różne poglądy na poniższym scenariuszu

.state('app.sr.product.upload', { 
      name: 'upload', 
      url: '/upload', 
      data: { 
       tags: [], 
       userCommunities: [] 
      }, 
      views: { 
       "[email protected]": { 
        templateUrl: 'views/upload/upload.html', 
        controller: 'UploadCtrl', 
        controllerAs: 'ul' 
       }, 
       "[email protected]": { 
        templateUrl: 'views/tags/tags.html', 
        controller: 'TagsCtrl', 
        controllerAs: 'vm' 
       }, 
       "[email protected]": { 
        templateUrl: 'views/user-community/user-community.html', 
        controller: 'UserCommunityCtrl', 
        controllerAs: 'ul' 
       }, 
      } 
     }) 
  • Jeśli mój pogląd jest [email protected] następnie w jaki sposób mogę sprawdzić, czy mój kontroler jest TagsCtrl, moja wartość controllerAs jest VM itp ??

  • Jak mogę przetestować urządzenie, jeśli mój stan jest app.sr.product.upload następnie data.tags=[], data.userCommunities=[] itp

Szukałem dużo dokumentacji i tutoriali ale nie go dostać.

Każda pomoc jest dostrzegalna. Dzięki

Odpowiedz

0

To nie jest coś, co normalnie testowałem. Sam UI-Router jest testowany przez well covered.

Lepiej poradzisz sobie z testami e2e (koniec-koniec) z Protractor. Symulować kliknięcie na link, można oczekiwać url być tym, należy spodziewać się liczbę elementów w liście, aby być tak itp

Ale jeśli naprawdę trzeba go:

  • zlokalizować element główny od każdego widoku (fe dodając konkretną klasę i za pomocą selektorów)
  • powinieneś być w stanie uzyskać dostęp do zakresu i kontrolera poprzez angular.element metod otoki
+0

Czy możesz być bardziej jasne, czy można podać jakiś kod demo ani żadnego przykładu, będzie to wielka pomoc – shreyansh

5

Spróbuj tego rozmiaru. Zakładam, że używałbyś jaśminu do testów, ale koncepcja jest taka sama dla każdej struktury testowej.

Po uruchomieniu testu najpierw zasubskrybuj wydarzenie '$stateChangeSuccess', a następnie przejdź do tego stanu. Po uruchomieniu zdarzenia sprawdź wartości toState, aby sprawdzić, czy są one zgodne z oczekiwaniami.

Możesz uruchomić fragment kodu, aby zobaczyć testy w akcji.

//write a unit test 
 
describe('state changes', function() { 
 
    beforeEach(module('app')); 
 
    var $rootScope, $state; 
 
    beforeEach(inject(function(_$rootScope_, _$state_) { 
 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
 
    $rootScope = _$rootScope_; 
 
    $state = _$state_; 
 
    })); 
 

 

 
    it('loads page 1', function(done) { 
 
    //wait for the state to change, then make sure we changed to the correct state 
 
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
 
     expect(toState.controller).toEqual('Controller1'); 
 
     done(); 
 
    }); 
 
    //navigate to the state 
 
    $state.go('state1'); 
 
    //start a digest cycle so ui-router will navigate 
 
    $rootScope.$apply(); 
 
    }); 
 

 
    it('loads page 2', function(done) { 
 
    //wait for the state to change, then make sure we changed to the correct state 
 
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
 
     expect(toState.controller).toEqual('Controller2'); 
 
     done(); 
 
    }); 
 
    //navigate to the state 
 
    $state.go('state2'); 
 
    //start a digest cycle so ui-router will navigate 
 
    $rootScope.$apply(); 
 
    }); 
 

 
    it('loads page 3', function(done) { 
 
    //wait for the state to change, then make sure we changed to the correct state 
 
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
 
     expect(toState.controller).toEqual('Controller3'); 
 
     done(); 
 
    }); 
 
    //navigate to the state 
 
    $state.go('state3'); 
 
    //start a digest cycle so ui-router will navigate 
 
    $rootScope.$apply(); 
 
    }); 
 
}); 
 

 
//set up some dummy controllers and some dummy states 
 
angular.module('app', ['ui.router']).controller('Controller1', function() { 
 
    this.message = 'Page 1'; 
 
}).controller('Controller2', function() { 
 
    this.message = 'Page 2'; 
 
}).controller('Controller3', function() { 
 
    this.message = 'Page 3'; 
 
}).config(function($stateProvider, $urlRouterProvider) { 
 
    $urlRouterProvider.otherwise("/state1"); 
 

 
    $stateProvider.state('state1', { 
 
    url: "/state1", 
 
    controller: 'Controller1', 
 
    controllerAs: 'vm', 
 
    template: '<h1>{{vm.message}}</h1>' 
 
    }).state('state2', { 
 
    url: "/state2", 
 
    controller: 'Controller2', 
 
    controllerAs: 'vm', 
 
    template: '<h2>{{vm.message}}</h2>' 
 
    }).state('state3', { 
 
    url: "/state3", 
 
    controller: 'Controller3', 
 
    controllerAs: 'vm', 
 
    template: '<h3>{{vm.message}}</h3>' 
 
    }); 
 
});
h1 { 
 
    color: red; 
 
} 
 
h2 { 
 
    color: blue; 
 
} 
 
h3 { 
 
    color: green; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> 
 

 
<script src=" 
 
https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script> 
 
<link rel="stylesheet" type="text/css" href="http://jasmine.github.io/2.0/lib/jasmine.css"> 
 
<script src="http://jasmine.github.io/2.0/lib/jasmine.js"></script> 
 
<script src="http://jasmine.github.io/2.0/lib/jasmine-html.js"></script> 
 
<script src="http://jasmine.github.io/2.0/lib/boot.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-mocks.js"></script> 
 
<div ng-app="app"> 
 
    <a ui-sref="state1">State 1</a> 
 
    <a ui-sref="state2">State 2</a> 
 
    <a ui-sref="state3">State 3</a> 
 
    <div ui-view></div> 
 
</div>

+1

Problem z tymi testami polega na tym, że mogą generować fałszywe alarmy, jeśli funkcje obsługi zdarzeń '$ stateChangeSuccess' nie zostaną nigdy wywołane. –

+1

@ShaunScovil, nie sądzę, że będą wytwarzać fałszywe alarmy. Ponieważ używają wykonanego wywołania zwrotnego, jaśmin rzuci błąd po przekroczeniu limitu czasu, jeśli wykonana funkcja nie zostanie wywołana. Więc tak, testy mogą zająć dużo czasu, ale jaśmin poinformuje cię, że twój test nigdy nie powiedział, że to się stało. – TwitchBronBron

+0

Jeśli z jakiegoś powodu funkcja obsługi zdarzeń nie jest wywoływana, nie ma oczekiwań() poza wywołaniem zwrotnym, które spowodowałoby niepowodzenie testu. –

1

Jeśli się nie mylę, myślę, że brakowało punktu początkowego pytanie, co było

if my view is [email protected] then how can I test that my controller is TagsCtrl, my controllerAs value is vm etc??

i

How can I unit test if my state is app.sr.product.upload then data.tags=[], data.userCommunities=[] etc.

Oto jak możesz przetestować te:

var $rootScope, $state, $injector, state; 

beforeEach(inject(function(_$rootScope_, _$state_){ 
    $rootScope = _$rootScope_; 
    $state = _$state_; 
    state = $state.get('app.sr.product.upload'); 
})); 

it('should have the correct data parameters', function() { 

    expect(state.data.tags).toEqual(''); 
    expect(state.data.userCommunities).toEqual(''); 

}); 

it('should render the dashboard views with the right Controllers', function() { 

    var product = state.views['[email protected]']; 
    var tags= state.views['[email protected]']; 
    var userCommunity = state.views['[email protected]']; 

    expect(product.templateUrl).toEqual('views/upload/upload.html'); 
    expect(product.controller).toEqual('UploadCtrl'); 
    expect(product.controllerAs).toEqual('ul'); 

    // etc... 

}); 

Również w nowszych wersjach kątowych, można po prostu zadeklarować swój kontroler tak:

controller: 'UploadCtrl as vm' 
Powiązane problemy