1

我正在尝试创建一个模型来支持 Angular 2 应用程序。我是 Angular2 的新手,所以请多多包涵。

该模型是我们想要迁移到 TypeScript 的旧 Java 代码库的一个端口。在这样做时,我们正在重新创建将被归类为“隔离”测试的测试

问题:

添加隔离测试后,种子中的所有Angular2 测试都失败,并在 Karma 中出现类似错误:

Chrome 59.0.3071 (Windows 10 0.0.0) Home should have a title FAILED
    Failed: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
    Error: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
        at syntaxError (webpack:///~/@angular/compiler/@angular/compiler.es5.js:1540:21 <- config/spec-bundle.js:33745:34) [ProxyZone]
        at webpack:///~/@angular/compiler/@angular/compiler.es5.js:14542:0 <- config/spec-bundle.js:46747:40 [ProxyZone]
        at Array.forEach (<anonymous>) [ProxyZone]
        at CompileMetadataResolver.getNgModuleMetadata (webpack:///~/@angular/compiler/@angular/compiler.es5.js:14524:0 <- config/spec-bundle.js:46729:54) [ProxyZone]
        at JitCompiler._loadModules (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25630:25 <- config/spec-bundle.js:57835:66) [ProxyZone]
...snip...
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43) [<root> => ProxyZone]
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:102:0 <- config/spec-bundle.js:69881:34) [<root>]
        at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack:///~/zone.js/dist/jasmine-patch.js:132:0 <- config/spec-bundle.js:69911:42) [<root>]
        at Zone.runTask (webpack:///~/zone.js/dist/zone.js:165:0 <- config/spec-bundle.js:70438:47) [<root> => <root>]
        at drainMicroTaskQueue (webpack:///~/zone.js/dist/zone.js:593:0 <- config/spec-bundle.js:70866:35) [<root>]
        at <anonymous> [<root>]
    Error: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
        at syntaxError (webpack:///~/@angular/compiler/@angular/compiler.es5.js:1540:21 <- config/spec-bundle.js:33745:34) [ProxyZone]
        at webpack:///~/@angular/compiler/@angular/compiler.es5.js:14542:0 <- config/spec-bundle.js:46747:40 [ProxyZone]
        at Array.forEach (<anonymous>) [ProxyZone]
        at CompileMetadataResolver.getNgModuleMetadata (webpack:///~/@angular/compiler/@angular/compiler.es5.js:14524:0 <- config/spec-bundle.js:46729:54) [ProxyZone]
...snip...
        at Function.TestBed.createComponent (webpack:///~/@angular/core/@angular/core/testing.es5.js:610:0 <- config/spec-bundle.js:17189:29) [ProxyZone]
        at Object.<anonymous> (webpack:///src/app/home/home.component.spec.ts:43:0 <- config/spec-bundle.js:73448:37) [ProxyZone]
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- config/spec-bundle.js:70169:39) [ProxyZone]
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43) [<root> => ProxyZone]
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:104:0 <- config/spec-bundle.js:69883:34) [<root>]
    TypeError: Cannot read property 'title' of undefined
        at Object.<anonymous> (webpack:///src/app/home/home.component.spec.ts:54:0 <- config/spec-bundle.js:73459:22)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:365:0 <- config/spec-bundle.js:70638:26)
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- config/spec-bundle.js:70169:39)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:364:0 <- config/spec-bundle.js:70637:32)
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43)
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:104:0 <- config/spec-bundle.js:69883:34)

我遗漏了更多错误,因为它们对于每个规范文件都是相似的:https ://github.com/Nava2/angular-starter/tree/master/src/app 。此 gist中提供了完整的日志输出。

这是 WebStorm 树:

WebStorm 测试树

如果我删除了隔离测试,Angular2 测试就不会再出现错误。

可重现的步骤:

  1. 克隆这个 fork:https ://github.com/Nava2/angular-starter
  2. 跑,npm install
  3. 跑,npm run test

如果我删除app/model/entity.no-testbed.spec.ts,错误就会消失。我不确定它们为什么会发生。如果这不是我应该开发的方式,请让我知道,因为我不想在“实践不足的道路”上浪费时间。

杂项信息

  • 操作系统:Windows 10
  • 节点版本:v8.1.2
4

1 回答 1

0

查看您的规范文件的源代码会有所帮助。听起来您还没有导入 HomeComponent。

于 2017-06-29T21:50:18.713 回答