【问题标题】:Aurelia unit test route paths to modulesAurelia 单元测试路由路径到模块
【发布时间】:2017-10-09 20:26:07
【问题描述】:

我一直在寻找一种好的模式来对我在应用程序中配置的路由进行单元测试,以便我知道指定的模块按照定义存在于磁盘上。

这是一个示例路由配置:

import { Aurelia, PLATFORM } from 'aurelia-framework';
import { Router, RouterConfiguration } from 'aurelia-router';

export class App {
    params = new bindParameters();
    router: Router;


    configureRouter(config: RouterConfiguration, router: Router) {
        config.title = 'Aurelia';
        config.map([{
            route: ['', 'home'],
            name: 'home',
            settings: { icon: 'home' },
            moduleId: PLATFORM.moduleName('../home/home'),
            nav: true,
            title: 'Home'
        }, {
            route: 'sample',
            name: 'sample',
            settings: { icon: 'education' },
            moduleId: PLATFORM.moduleName('../sample/index'),
            nav: true,
            title: 'Sample Information'
        }]);

        this.router = router;
    }
}

class bindParameters {
    user = "user_name";
}

为了测试它,我采用了传入一个路由器实例然后检查它是否存在的方法:

import { App } from './app';
import jasmine from 'jasmine';
import { Container } from "aurelia-framework";
import { RouterConfiguration, Router } from "aurelia-router";

describe('application routes', function () {
    let app: App;
    let router: Router;
    let routerConfiguration: RouterConfiguration;
    let configureRouter: Promise<void>;

    beforeEach(() => {
        var container = new Container().makeGlobal();
        routerConfiguration = container.get(RouterConfiguration);
        router = container.get(Router);
        app = new App();
        app.configureRouter(routerConfiguration, router);
        configureRouter = router.configure(routerConfiguration);
        routerConfiguration.exportToRouter(router);
    });

    it('should exist for sample', function () {
        expect(router).not.toBeNull();
        //configureRouter.then(function () {
        //var route = router.routes.find((route) => route.name == 'sample');
        // add some assert that the sample module can be found
        //    done();
        //});
    });
});

我当前的问题是容器正在返回一个空路由器,如当前测试所示。我发现的最接近我正在尝试做的模式是this question

我在示例测试中遗漏了什么,还有更好的方法来测试路由配置吗?

【问题讨论】:

  • 当我按原样复制您的代码时,它可以工作。 router 不为空,测试通过。
  • @thinkOfaNumber 感谢您花时间运行我的测试。原来这取决于我如何设置 Karma。有时换一天换个新面貌可以解决问题。
  • 不客气!

标签: unit-testing jasmine aurelia


【解决方案1】:

看来@thinkOfaNumber 是对的。结果我的测试很好,但我缺少反射元数据。当我应用this stackoverflow post 中概述的修复程序时,我的测试通过了。

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 2013-04-06
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多