您好,我正在使用 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 编写单元测试吗?
发布评论