Aurelia 单元测试路由路径到模块

Aurelia unit test route paths to modules

我一直在寻找一个好的模式来对我在我的应用程序中配置的路径进行单元测试,这样我就知道磁盘上存在指定的模块。

这是一个路由配置示例:

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();
        //});
    });
});

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

我在示例测试中遗漏了什么以及是否有更好的方法来测试路由配置?

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