키 포인트
$scope
테스트 구성 및 실행 블록 $emit/$broadcast
angular.module('configAndRunBlocks', ['ngRoute']) .config(function ($routeProvider) { $routeProvider.when('/home', { templateUrl: 'home.html', controller: 'HomeController', resolve: { bootstrap: ['$q', function ($q) { return $q.when({ prop: 'value' }); }] } }) .when('/details/:id', { templateUrl: 'details.html', controller: 'DetailsController' }) .otherwise({ redirectTo: '/home' }); }) .run(function ($rootScope, messenger) { messenger.send('Bootstrapping application'); $rootScope.$on('$locationChangeStart', function (event, next, current) { messenger.send('Changing route to ' + next + ' from ' + current); }); });
describe('config and run blocks', function () { var routeProvider, messenger; beforeEach(function () { module('ngRoute'); module(function ($provide, $routeProvider) { routeProvider = $routeProvider; spyOn(routeProvider, 'when').andCallThrough(); spyOn(routeProvider, 'otherwise').andCallThrough(); messenger = { send: jasmine.createSpy('send') }; $provide.value('messenger', messenger); }); module('configAndRunBlocks'); }); beforeEach(inject()); });
$routeProvider
<<> (테스트 범위 이벤트, 라우팅, 구문 분석 블록 및 애니메이션에 대한 후속 설명은 공간 제한으로 인해 여기에서 생략됩니다. 완전한 테스트 코드 예제는 원래 GitHub 리포지토리를 참조하십시오)
describe('config block tests', function () { it('should have called registered 2 routes', function () { //Otherwise internally calls when. So, call count of when has to be 3 expect(routeProvider.when.callCount).toBe(3); }); it('should have registered a default route', function () { expect(routeProvider.otherwise).toHaveBeenCalled(); }); });
<,>이 기사를 통해 지난 2 년간 AngularJS 코드 테스트에서 배운 대부분의 테스트 기술을 소개합니다. 이것은 끝이 아니며 실제 응용 프로그램의 비즈니스 시나리오에 대한 테스트를 작성할 때 더 많은 것을 배울 수 있습니다. AngularJS 코드를 테스트하는 것에 대한 충분한 지식을 얻었 으면 좋겠습니다. 왜 기다려야합니까? 지금까지 작성한 모든 코드 라인에 대한 테스트를 작성하십시오!
describe('run block tests', function () { var rootScope; beforeEach(inject(function ($rootScope) { rootScope = $rootScope; })); it('should send application bootstrap message', function () { expect(messenger.send).toHaveBeenCalled(); expect(messenger.send).toHaveBeenCalledWith("Bootstrapping application"); }); });
(원본 텍스트의 FAQ 부품도 공간 제한으로 인해 생략됩니다)
위 내용은 AngularJS 테스트 : 부트 스트랩 블록, 경로, 이벤트 및 애니메이션의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!