testing - 无法在 Angular 2.0.0 测试中加载 .html

标签 testing angular gulp testbed url-template

我对 Angular 2.0.0 中的 TestBed 有疑问

    beforeEach( async(() => {
        TestBed.configureTestingModule({
            declarations: [
                SomethingComponent
            ]
        }).compileComponents(); // compile template
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(SomethingComponent);
        comp = fixture.componentInstance;
    });

    it('test', async(() => {
        fixture.detectChanges();
        expect(true)
    }));

给定组件:

@Component({
    templateUrl: 'app/components/something/something.component.html'
})

我得到错误:

Firefox 48.0.0 (Mac OS X 10.11.0)
Failed: Uncaught (in promise): Failed to load app/components/something/something.component.html
resolvePromise@node_modules/zone.js/dist/zone.js:429:31
resolvePromise@node_modules/zone.js/dist/zone.js:414:17
scheduleResolveOrReject/<@node_modules/zone.js/dist/zone.js:462:17
ZoneDelegate.prototype.invokeTask@node_modules/zone.js/dist/zone.js:236:23
ProxyZoneSpec.prototype.onInvokeTask@node_modules/zone.js/dist/proxy.js:96:20
ZoneDelegate.prototype.invokeTask@node_modules/zone.js/dist/zone.js:235:23
Zone.prototype.runTask@node_modules/zone.js/dist/zone.js:136:28
drainMicroTaskQueue@node_modules/zone.js/dist/zone.js:368:25
ZoneTask/this.invoke@node_modules/zone.js/dist/zone.js:308:25

我想这可能是因为配置设置不正确。我正在使用 gulp 将 TypeScript 预编译为 JavaScript,并且我有以下项目结构:

|_ grails-app
|_ frontend
  |_ angular
    |_ app
    |    |_ components
    |    |     |_ something
    |    |          |_ something.component.ts
    |    |          |_ something.component.html
    |    |_ model
    |    |_ test
    |    |_ app.module.ts
    |    |_ app.routing.ts
    |    |_ main.ts
    |_ assets
    |_ build
    |    |_ app
    |        |_ components
    |        |      |_ something
    |        |          |_ something.component.html
    |        |          |_ something.component.js
    |        |          |_ something.component.js.map
    |        |_      
    |        |_      
    |        |      
    |_ node_modules
    |_ gulpfile.js
    |_ karma.conf.js
    |_ karma-test-shim.js
    |_ systemjs.config
    |_
    |_
    |

但是如果我假设 html 文件是预编译的,并从测试中删除命令 .compileComponents(),我会收到以下错误:

Firefox 48.0.0 (Mac OS X 10.11.0)
Error: Cannot create the component SomethingComponent as it was not imported into the testing module! 

如果我删除 async,我会得到这个错误:

Firefox 48.0.0 (Mac OS X 10.11.0)
Error: This test module uses the component SomethingComponent which is using a "templateUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test. 

karma-test-shim.js 中,我有:

System.config({
baseURL: '/base/',
paths: {
    // paths serve as alias
    'npm:': 'node_modules/'
},
map: {
    'app': '/base/build/app/',
    '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
    '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
    '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
    '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
    '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
    '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
    '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

    // angular testing umd bundles
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',

    // other libraries
    'rxjs': 'npm:rxjs'
},
packages: {
    'app': {
        defaultExtension: 'js'
    },
    'rxjs': {
        defaultExtension: 'js'
    }
}
});

我希望有人能帮我弄清楚,我在配置上做错了什么。

最佳答案

您可以将 karma.conf.js 的基本路径定义为代理:

// Proxied base paths for loading assets
proxies: {
  // required for component assets fetched by Angular's compiler
  "/app/": '/base/build/app/'
},

来源:https://github.com/angular/quickstart

关于testing - 无法在 Angular 2.0.0 测试中加载 .html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788584/

相关文章:

Angular 2 在带有 Angular 路由器的 NPM 模块中延迟加载 NgModule

html - 如何在同一个组件中定义两个管道

android - 滑动方法在android自动化测试中不起作用

java - spring-mvc:如何使用 mockMvc 测试 Rx 响应?

c++ - 使用 'using' 关键字使继承的构造函数公开

ruby-on-rails - 为 Rails 性能测试生成代表性数据

angular - 如何在 Angular 中嵌入 YouTube 视频?

node.js - 任务从未定义 : default, 要列出可用任务,请尝试运行: gulp --tasks

javascript - 使用 Gulp-Notify 在终端中显示项目文件夹路径,而不是硬盘上项目的整个路径

css - gulp-ruby-sass @import 同时编译一个独特的文件