相关疑难解决方法(0)

angularjs路由单元测试

正如我们在http://docs.angularjs.org/tutorial/step_07中看到的,

angular.module('phonecat', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/phones', {templateUrl: 'partials/phone-list.html',   controller: PhoneListCtrl}).
      when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}).
      otherwise({redirectTo: '/phones'});
}]);
Run Code Online (Sandbox Code Playgroud)

建议使用e2e测试进行路由测试,

  it('should redirect index.html to index.html#/phones', function() {
    browser().navigateTo('../../app/index.html');
    expect(browser().location().url()).toBe('/phones');
  });
Run Code Online (Sandbox Code Playgroud)

但是,我认为'$ routeProvider'配置是使用单个函数,函数($ routeProvider)完成的,我们应该能够在不涉及浏览器的情况下进行单元测试,因为我认为路由功能不需要浏览器DOM.

例如,
当url是/ foo时,templateUrl必须是/partials/foo.html,
当url是/ bar时,controller是FooCtrl ,templateUrl必须是/partials/bar.html,控制器是BarCtrl

它是一个简单的IMO函数,它也应该在一个简单的测试,单元测试中进行测试.

我用谷歌搜索并搜索了这个$ routeProvider单元测试,但还没有运气.

我想我可以从这里借一些代码,但还不能实现,https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js.

unit-testing jasmine angularjs angularjs-routing

41
推荐指数
3
解决办法
3万
查看次数