问题描述
限时送ChatGPT账号..正如我们在 http://docs.angularjs/tutorial/step_07 中看到的,
As we see here in http://docs.angularjs/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'});
}]);
路由测试建议使用e2e测试,
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"配置是通过一个函数完成的,function($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.
例如,
当 url 为/foo 时,templateUrl 必须为/partials/foo.html 并且控制器为 FooCtrl
当 url 为/bar 时,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
这是一个简单的函数 IMO,它也应该在一个简单的测试中进行测试,一个单元测试.
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.
我想我可以从这里借用一些代码,但还没有完成,https://github/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/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屋!
更多推荐
[db:关键词]
发布评论