【问题标题】:Setting up and testing UI Router设置和测试 UI 路由器
【发布时间】:2016-01-07 15:36:52
【问题描述】:

我正在尝试使用 ncy-angular-breadcrumb,它使用 ui 路由。我设置了一个模块来容纳 ui 路由:

(function () {
    var app = angular.module('configurator.uiroutes', ['ui.router', 'ncy-angular-breadcrumb']);

    app.config(['$stateProvider', function ($stateProvider) {
        $stateProvider
            .state('home', {
                url: '/',
                ncyBreadcrumb: {
                    label: 'Series'
                }
            });
    }]);

})();

我有这个测试,看看路线是否有效:

describe('Configurator UI Routes', function () {

    beforeEach(module('configurator.uiroutes'));
    beforeEach(module('ui.router'));

    var $state, $rootscope, state = '/';

    // Inject and assign the $state and $rootscope services.
    beforeEach(inject(function (_$state_, _$rootScope_) {
        $state = _$state_;
        $rootscope = _$rootScope_;
    }));

    //Test whether the url is correct
    it('should activate the state', function () {
        $state.go(state);
        $rootscope.$digest();
        expect($state.current.name).toBe('home');
    });
});

产生此错误的原因:

Error: Could not resolve '/' from state ''

我只是没有得到 UI 路由器以及它是如何工作的,或者它应该做什么。我只想获取部分 url 并将该信息传递给 ncyBreadcrumb,但我什至无法让基本 URL 工作。

任何指示或帮助都会很棒!

编辑:我已经通过在测试中将“/”替换为“home”来通过测试。我更大的问题是:有没有办法测试当 URL '/' 被击中时, state.current.name 变成了 'home'。在我使用该应用程序时,这似乎没有发生,我希望单元测试可以帮助告诉我原因。

这是一个单页应用程序,如果相关的话。

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine angular-ui-router


    【解决方案1】:

    错误已经足够自我解释了,因为您要求 ui-router 使用 $state.go/ 状态上重定向

    基本上是$state.go,它要求输入州名而不是url

    var $state, $rootscope, state = 'home'; //<--changed from `/` to `home`
    

    【讨论】:

    • 酷。我在想 go 是在嘲笑一个 URL。现在测试通过了。有没有办法测试 URL 导航?目前在我的网站上,当我在“/”时,它似乎在我导航到下一页之前不起作用,然后出现“系列”,但直到那时才出现。
    • 我不想按状态导航,我想按 URL 导航并定义/检查状态。
    • @spuppett 我认为您可以通过使用 $location 服务(如$location.path('/'))重定向到 url 然后执行$rootscope.$digest(); 来更新状态..就是这样..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    相关资源
    最近更新 更多