“组件应创建”测试用例的Angular 5单元测试抛出错误

时间:2018-12-04 17:54:31

标签: angular typescript unit-testing jasmine

我第一次尝试Angular 5单元测试。虽然我已经创建了该应用程序,然后决定在其中运行测试。但是我遇到了这些错误:

AppComponent should create the app
AppComponent should have as title 'app'
AppComponent should render title in a h1 tag
GalleryComponent should create
UploadComponent should create

和错误详细信息,例如:

Failed: Template parse errors:
'app-upload' is not a known element:
1. If 'app-upload' is an Angular component, then verify that it is part of this module.
2. If 'app-upload' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. (" class="row">
        <div class="col-md-3" style="padding:5%; box-sizing: border-box">
            [ERROR ->]<app-upload></app-upload>
        </div>
        <div class="col-md-9">
"): ng:///DynamicTestModule/AppComponent.html@3:12

我的package.json开发依赖项:

devDependencies": {
    "@angular/compiler-cli": "^6.0.2",
    "@angular-devkit/build-angular": "~0.6.3",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.3",
    "@angular/language-service": "^6.0.2",
    "@types/jasmine": "2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  } 

测试文件app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));
  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to Typito-photo-app!');
  }));
});

我不知道如何解决这些问题。我没有对规范文件进行任何更改,也没有编写任何测试用例。所有这些都不应该按照角度文档中描述的那样按预期运行吗?

2 个答案:

答案 0 :(得分:3)

您要测试的组件具有一个或多个Testbed。一个好的做法是忽略这些组件并将它们分开测试。

对此进行归档的一种方法是,在Testbed内部使用NO_ERRORS_SCHEMA来告诉您的角度TestBed.configureTestingModule({ declarations: [ AppComponent ], schemas: [ NO_ERRORS_SCHEMA ] }).compileComponents(); 在组件构建过程中跳过这些角度。

您的测试平台应如下所示:

component.html

它应该忽略出现在android:configChanges="keyboard|keyboardHidden" 中的所有自定义元素(标签)。

另一种方法是模拟子组件。例如this

答案 1 :(得分:0)

您应该验证组件是否可用:

 beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
});

it('should be created', () => {
    fixture.detectChanges();
    expect(component)
        .toBeTruthy();
});

此外,在编写测试时,还应在测试模块中注入所有依赖项。