angularjs路线单元测试 [英] angularjs route unit testing

查看:166
本文介绍了angularjs路线单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在这里看到的<一个href=\"http://docs.angularjs.org/tutorial/step_07\">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'});
}]);

路由测试应当与端到端测试完成后,

routing test is suggested to be done with e2e test,

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

不过,我认为'$ routeProvider'的配置与功能单一,功能($ routeProvider)完成的,我们应该能够单元测试无需浏览器的参与,因为我觉得路由功能,不需要浏览器的DOM。

However, I think the '$routeProvider' config is done with a single function, function($routeProvider), and we should be able to unit test without involvement of browser since I think routing function does not require browser DOM.

例如,搜索
  当网址是/ foo,templateUrl必须/partials/foo.html和控制器FooCtrl结果
  当网址是/条,templateUrl必须/partials/bar.html和控制器BarCtrl

For example,
when url is /foo, templateUrl must be /partials/foo.html and controller is FooCtrl
when url is /bar, templateUrl must be /partials/bar.html and controller is BarCtrl

有一个简单的函数国际海事组织,而且它也应该在一个简单的检验,一个单元测试

It is a simple function IMO, and it should also be tested in a simple test, a unit test.

我一派,搜索此$ routeProvider单元测试,但没有运气呢。

I googled and searched for this $routeProvider unit test, but no luck yet.

我想我可以从这里借一些code,但不能使它的是,<一个href=\"https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js\">https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js.

I think I may borrow some code from here but couldn't make it yet, https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js.

推荐答案

为什么不干脆断言路由对象的配置是否正确?

Why not just assert the route object is configured correctly?

it('should map routes to controllers', function() {
  module('phonecat');

  inject(function($route) {

    expect($route.routes['/phones'].controller).toBe('PhoneListCtrl');
    expect($route.routes['/phones'].templateUrl).
      toEqual('partials/phone-list.html');

    expect($route.routes['/phones/:phoneId'].templateUrl).
      toEqual('partials/phone-detail.html');
    expect($route.routes['/phones/:phoneId'].controller).
      toEqual('PhoneDetailCtrl');

    // otherwise redirect to
    expect($route.routes[null].redirectTo).toEqual('/phones')
  });
});

这篇关于angularjs路线单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆