6

Come posso unit test diversi punti di vista per lo scenario di seguito

.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' 
       }, 
      } 
     }) 
  • Se il mio punto di vista è [email protected] allora come faccio a testare che mio controller è TagsCtrl, il mio valore controllerAs è vm etc ??

  • Come posso unità di prova se il mio stato è app.sr.product.upload poi data.tags=[], data.userCommunities=[] ecc

ho cercato per il lotto di documenti e tutorial sbagliando di poco capito.

Qualsiasi aiuto è apprezzabile. Grazie

risposta

0

Non è qualcosa che vorrei testare normalmente. UI-Router stesso è well covered da test.

Si farebbe meglio con i test e2e (end-to-end) con Protractor. Di simulare un clic su un link, ci si aspetta URL da questa operazione, utilizzare il numero di elementi aspettarsi in un elenco da cui ecc

Ma se si realmente bisogno:

  • individuare elemento radice di ciascuna vista (fe aggiungendo una classe specifica e selettori)
  • si dovrebbe essere in grado di accedere portata e controller tramite angular.element metodi involucro
+0

Per favore, puoi essere più chiaro, se è possibile fornire un codice demo o qualsiasi esempio, sarà un ottimo aiuto – shreyansh

5

Prova questo per dimensioni. Presumo che tu stia usando il gelsomino per i tuoi test, ma il concetto è lo stesso per qualsiasi framework di test.

Quando si esegue il test, sottoscrivere prima l'evento '$stateChangeSuccess' e quindi navigare in quello stato. Una volta che l'evento si attiva, controlla i valori di toState per vedere se sono ciò che ti aspetti che siano.

È possibile eseguire lo snippet per visualizzare i test in azione.

//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

Il problema con questi test è che potrebbero produrre falsi positivi se le funzioni del gestore di eventi '$ stateChangeSuccess' non vengono mai chiamate. –

+1

@ShaunScovil, non penso che produrrebbero falsi positivi. Poiché stanno utilizzando il callback fatto, il gelsomino genera un errore dopo un timeout se la funzione done non viene chiamata. Quindi sì, i test potrebbero richiedere molto tempo per essere eseguiti, ma Jasmine ti farà sapere che il tuo test non ha mai detto che è stato fatto. – TwitchBronBron

+0

Se per qualche motivo la funzione gestore di eventi non viene chiamata, non ci sono aspettative() al di fuori del callback che potrebbero causare il fallimento del test. –

1

Se non sbaglio, penso che abbiamo perso il punto della questione iniziale, che era

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

e

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

Ecco come puoi provare questi:

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... 

}); 

Inoltre, nelle versioni più recenti angolari, si può semplicemente dichiarare il controller in questo modo:

controller: 'UploadCtrl as vm' 
Problemi correlati