我是相當新的角度單元測試,請耐心等待我!AngularJS:單元測試UI路由器狀態變化
我有一個$ stateProvidor設置爲我的應用程序,並想測試路由部分確實工作正常。
說我有這種配置的:
angular.module("app.routing.config", []).config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state("home", {
url: "/",
templateUrl: "app/modules/home/page/home_page.html",
controller: "HomePageController",
resolve: {
setPageTitle: function($rootScope) {
return $rootScope.pageTitle = "Home";
}
}
}).state("somethingelse", {
url: "/",
templateUrl: "app/modules/home/page/somethingelse.html",
controller: "SomeThingElseController",
resolve: {
setPageTitle: function($rootScope) {
return $rootScope.pageTitle = "Some Thing Else";
}
}
});
return $urlRouterProvider.otherwise('/');
});
我碰到這個博客post關於如何建立單元測試的UI路由器配置來了,所以我試圖採取同樣的方法,在這裏在我的測試中,我嘗試:
'use strict';
describe('UI-Router State Change Tests', function() {
var $location, $rootScope, $scope, $state, $templateCache;
beforeEach(module('app'));
beforeEach(inject(function(_$rootScope_, _$state_, _$templateCache_, _$location_) {
$rootScope = _$rootScope_;
$state = _$state_;
$templateCache = _$templateCache_;
$location = _$location_;
}));
describe('State Change: home', function() {
beforeEach(function() {
$templateCache.put(null, 'app/modules/home/page/home_page.html');
});
it('should go to the home state', function() {
$location.url('home');
$rootScope.$digest();
expect($state.href('home')).toEqual('#/');
expect($rootScope.pageTitle).toEqual('Home');
});
});
});
當運行我在輸出中收到此錯誤測試:
Error: Unexpected request: GET app/modules/home/page/home_page.html
很明顯,我在這裏做錯了什麼,所以任何幫助或指針將不勝感激。
我確實遇到了$ httpBackend,這是我應該在這裏使用的東西,所以告訴我的測試期望對我的狀態更改測試正在進行的HTML頁面的請求?