angularjs route unit testing

前端 未结 3 820
Happy的楠姐
Happy的楠姐 2020-12-04 13:23

As we see here in http://docs.angularjs.org/tutorial/step_07,

angular.module(\'phonecat\', []).
  config([\'$routeProvider\', function($routeProvider) {
  $r         


        
3条回答
  •  温柔的废话
    2020-12-04 13:47

    Combining the two previous answers, if you want to test the router as a black box that is successfully routing where it should (not the controller ets configs in themselves), whatever the routes might be:

    // 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);
    });
    

提交回复
热议问题