Aurelia单元测试模块的路径路径

时间:2017-10-04 19:32:27

标签: unit-testing jasmine aurelia

我一直在为我在应用程序中配置的单元测试路径寻找一个好的模式,以便我知道指定的模块存在于定义的磁盘上。

以下是一个示例路由配置:

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

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

1 个答案:

答案 0 :(得分:1)

似乎@thinkOfaNumber是对的。事实证明我的测试很好,但我缺少反射元数据。当我应用this stackoverflow post中概述的修复时,我的测试通过了。