unitarios unitarias test run pruebas integracion hacer como automatizadas angularjs unit-testing jasmine angularjs-routing

angularjs - unitarias - test unitarios angular



Prueba de unidad de ruta angularjs (3)

¿Por qué no simplemente afirmar que el objeto de ruta está configurado correctamente?

it(''should map routes to controllers'', function() { module(''phonecat''); inject(function($route) { expect($route.routes[''/phones''].controller).toBe(''PhoneListCtrl''); expect($route.routes[''/phones''].templateUrl). toEqual(''partials/phone-list.html''); expect($route.routes[''/phones/:phoneId''].templateUrl). toEqual(''partials/phone-detail.html''); expect($route.routes[''/phones/:phoneId''].controller). toEqual(''PhoneDetailCtrl''); // otherwise redirect to expect($route.routes[null].redirectTo).toEqual(''/phones'') }); });

Como vemos aquí en http://docs.angularjs.org/tutorial/step_07 ,

angular.module(''phonecat'', []). config([''$routeProvider'', function($routeProvider) { $routeProvider. when(''/phones'', {templateUrl: ''partials/phone-list.html'', controller: PhoneListCtrl}). when(''/phones/:phoneId'', {templateUrl: ''partials/phone-detail.html'', controller: PhoneDetailCtrl}). otherwise({redirectTo: ''/phones''}); }]);

se sugiere hacer una prueba de enrutamiento con la prueba e2e,

it(''should redirect index.html to index.html#/phones'', function() { browser().navigateTo(''../../app/index.html''); expect(browser().location().url()).toBe(''/phones''); });

Sin embargo, creo que la configuración ''$ routeProvider'' se hace con una única función, la función ($ routeProvider), y deberíamos poder probar la unidad sin la participación del navegador, ya que creo que la función de enrutamiento no requiere el navegador DOM.

Por ejemplo,
cuando url es / foo, templateUrl debe ser /partials/foo.html y el controlador es FooCtrl
cuando url es / bar, templateUrl debe ser /partials/bar.html y el controlador es BarCtrl

Es una función simple de OMI, y también debe probarse en una prueba simple, una prueba de unidad.

Busqué en Google y busqué esta prueba de la unidad $ routeProvider, pero aún no tuve suerte.

Creo que puedo tomar prestado algún código de aquí, pero no pude hacerlo aún, https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js .


Combinando las dos respuestas anteriores, si desea probar el enrutador como una caja negra que está enrutando con éxito donde debería (no el controlador ets las configuraciones en sí mismas), cualquiera que sean las rutas:

// assuming the usual inject beforeEach for $route etc. var expected = {}; it(''should call the right controller for /phones route'', function () { expected.controller = $route.routes[''/phones''].controller; $location.path(''/phones''); $rootScope.$digest(); expect($route.current.controller).toBe(expected.controller); }); it(''should redirect to redirectUrl from any other route'', function () { expected.path = $route.routes[null].redirectTo; $location.path(''/wherever-wrong''); $rootScope.$digest(); expect($location.path()).toBe(expected.path); });


Creo que deberías poder probar $ routeProvider así:

angular.module(''phonecat'', []). config([''$routeProvider'', function($routeProvider) { $routeProvider. when(''/phones'', {templateUrl: ''partials/phone-list.html'', controller: PhoneListCtrl}). when(''/phones/:phoneId'', {templateUrl: ''partials/phone-detail.html'', controller: PhoneDetailCtrl}). otherwise({redirectTo: ''/phones''}); }]); it(''should test routeProvider'', function() { module(''phonecat''); inject(function($route, $location, $rootScope) { expect($route.current).toBeUndefined(); $location.path(''/phones/1''); $rootScope.$digest(); expect($route.current.templateUrl).toBe(''partials/phone-detail.html''); expect($route.current.controller).toBe(PhoneDetailCtrl); $location.path(''/otherwise''); $rootScope.$digest(); expect($location.path()).toBe(''/phones/''); expect($route.current.templateUrl).toEqual(''partials/phone-list.html''); expect($route.current.controller).toBe(PhoneListCtrl); }); });