2015-06-16 124 views
2

我是相当新的角度单元测试,请耐心等待我!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页面的请求?

回答

0

这几乎可以肯定是在应用程序/测试运行时期间异步加载的部分html视图(home_page.html)。

为了处理这个问题,你可以预处理你的html partials到Javascript字符串中,然后可以通过你的测试同步加载。

看看karma-ng-html2js-preprocessor这应该可以解决你的问题。

相关问题