17

嗨,我正在使用 AngularJS 构建一个应用程序,但我被困在单元测试部分。我知道如何为控制器编写单元测试,但我不知道如何为 routeProvider 编写单元测试。我正在使用 Jasmine 编写单元测试。

我的路线提供者将如下所示;

    var app = angular.module('MyApp', ['ngResource'])

     app.config(function ($routeProvider) {
        $routeProvider
          .when('/', {
            templateUrl: 'app/views/main.html',
            controller: 'MainCtrl'
          })
          .when('/home/:PartyID', {
            templateUrl: 'app/views/home.html',
            controller: 'HomeCtrl'
          })
           .when('/edit/:PartyID', {
            templateUrl: 'app/views/update_profile.html',
            controller: 'EditCtrl'
          })
          .when('/route', {
            templateUrl: 'app/views/route.html',
            controller: 'RouteCtrl'
          })
          .when('/signup', {
            templateUrl: 'app/views/signup.html',
            controller: 'SignupCtrl'
          })
          .when('/login', {
            templateUrl: 'app/views/login.html',
            controller: 'LoginCtrl'
          })
          .otherwise({
            redirectTo: '/'
          });  
 });

如何使用 Jasmine 为这个 routeProvider 编写单元测试?

4

2 回答 2

24

是的,您可以,这是一个快速的答案,下面是一小段代码,可用于测试路线是否将您带到您期望的地方。

describe('Testing routes', function() {
    beforeEach(module('windscreens'));

    var location, route, rootScope;

    beforeEach(inject(
        function( _$location_, _$route_, _$rootScope_ ) {
            location = _$location_;
            route = _$route_;
            rootScope = _$rootScope_;
    }));

     describe('Login route', function() {
        beforeEach(inject(
            function($httpBackend) {
                $httpBackend.expectGET('login')
                .respond(200);
            }));

        it('should load the login page on successful load of /login', function() {
            location.path('/login');
            rootScope.$digest();
            expect(route.current.controller).toBe('LoginCtrl')
        });
    });    
});
于 2014-04-23T08:22:16.450 回答
15

把事情简单化

describe('Testing Routes', function () {

// load the controller's module
beforeEach(module('MyApp'));

it('should test routes',
inject(function ($route) {

  expect($route.routes['/'].controller).toBe('MainCtrl');
  expect($route.routes['/'].templateUrl).toEqual('app/views/main.html');

  expect($route.routes['/home/:PartyID'].controller).toBe('HomeCtrl');
  expect($route.routes['/home/:PartyID'].templateUrl).toEqual('app/views/home.html');

  expect($route.routes['/edit/:PartyID'].controller).toBe('EditCtrl');
  expect($route.routes['/edit/:PartyID'].templateUrl).toEqual('app/views/update_profile.html');

  expect($route.routes['/route'].controller).toBe('RouteCtrl');
  expect($route.routes['/route'].templateUrl).toEqual('app/views/route.html');

  expect($route.routes['/signup'].controller).toBe('SignupCtrl');
  expect($route.routes['/signup'].templateUrl).toEqual('app/views/signup.html');

  expect($route.routes['/streamconfigs/:id'].controller).toBe('LoginCtrl');
  expect($route.routes['/streamconfigs/:id'].templateUrl).toEqual('app/views/login.html');

  expect($route.routes[null].redirectTo).toEqual('/');
}));

});
于 2014-12-13T18:29:45.563 回答