我们可以为 AngularJS routeProvider 编写单元测试吗?

编程入门 行业动态 更新时间:2024-10-28 04:26:32
本文介绍了我们可以为 AngularJS routeProvider 编写单元测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

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

推荐答案

是的,你可以,这是一个快速的答案,下面是一小段代码,可以使用和扩展来测试路线带你到你的地方'd 期望.

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.

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 编写单元测试吗?

本文发布于:2023-11-16 18:59:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我们可以   单元测试   AngularJS   routeProvider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!