【问题标题】:Template Parse Errors (only) When Running Karma Tests运行 Karma 测试时的模板解析错误(仅)
【发布时间】:2019-05-04 14:49:07
【问题描述】:

我有一个小的 Angular 应用程序,我想为其编写单元测试的 AppComponent 方法。我的应用程序已按预期编译并运行,但是当我尝试为其运行默认测试时(我正在使用 webstorm,所以我在运行/调试配置下拉列表中切换到Tests)我收到模板解析错误与我的html 文件有关。 Karma 报告违规行如下:

Failed: Template parse errors:
    Can't bind to 'ngModel' since it isn't a known property of 'textarea'. ("
      <div style="height:10px"></div>
      <div style="text-align:center">
        <textarea [ERROR ->][(ngModel)]="data"></textarea>
      </div>
      <div style="height:10px"></div>
    "): ng:///DynamicTestModule/AppComponent.html@13:14

我刚开始的时候遇到了这个问题,因为我没有导入FormsModule 并将其添加到我的app.module.ts 中的@NgModule(as in here)。我需要添加该导入的特定于测试的地方吗?我尝试将其导入我的app.component.spec.ts,但这并没有什么区别。

【问题讨论】:

  • 请提供minimal reproducible example,您确定要在测试中加载表单模块吗?
  • 你在单元测试中导入 formsModule 了吗?
  • @Akhlesh 这就是问题所在,谢谢。

标签: javascript angular karma-runner webstorm


【解决方案1】:

app.component.spec.ts 也需要导入 FormsModule:

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports: [
        FormsModule  // <- important!
      ]
    }).compileComponents();
  }));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2021-04-27
    • 2017-01-31
    • 2018-08-29
    • 2016-04-14
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多