我们可以写出AngularJS routeProvider单元测试? [英] Can we write unit test for AngularJS routeProvider?

查看:220
本文介绍了我们可以写出AngularJS routeProvider单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我建立使用AngularJS一个应用程序,我停留在单元测试部分。我知道如何编写控制器和所有的单元测试,但我不知道该怎么做它routeProvider。我使用的茉莉花编写单元测试。

Hi I am building an app using AngularJS and I am stuck at unit test section. I know how to write unit testing for controllers and all but I don't know how to do it for routeProvider. I am using Jasmine for writing unit test.

我的路线提供商将看起来像这样;

My route provider will look like this;

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

我怎么能写单元测试使用茉莉花此routeProvider?

How can I write unit test for this routeProvider using Jasmine?

推荐答案

当然可以,就是快速的答案,下面是一个可以使用和扩展来测试路线带你到code的一小片你所期望的地方。我可以详细说明这多一点,我的来源我怎么摆在首位了解到这一点,如果你有兴趣?

Yes you can, is the quick answer and below is a little piece of code that can be used and extended to test that routes take you to the places you'd expect. I can elaborate on this a little more and my sources for how I learnt this in the first place, if you are interested?

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

这篇关于我们可以写出AngularJS routeProvider单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆