【问题标题】:jasmine unit test "An error was thrown in afterAll"茉莉花单元测试“在afterAll中引发了错误”
【发布时间】:2019-03-22 22:09:55
【问题描述】:

我只是对组件是否已创建进行单元测试。以下是我的规范文件

import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { MatTableModule } from '@angular/material';
import { HttpClientModule, HttpXhrBackend } from '@angular/common/http';
import { MockBackend } from '@angular/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { ReleaseListComponent } from './release-list.component';
import { ReleaseService } from '../release.service';

describe('ReleaseListComponent', () => {
  let component: ReleaseListComponent;
  let fixture: ComponentFixture<ReleaseListComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      imports: [RouterTestingModule, MatTableModule, HttpClientModule ],
      declarations: [ReleaseListComponent],
      providers: [ReleaseService]
    })
      .compileComponents();
  });

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

  it('should create', () => {
    expect(component).toBeDefined();
  });

我的组件

import { Component, OnInit, ViewChild } from '@angular/core';
import { ReleaseService } from '../release.service';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';

@Component({
  selector: 'bw-release-list',
  templateUrl: './release-list.component.html'
})
export class ReleaseListComponent implements OnInit {
  title = 'Release notes';
  displayedColumns = ['title'];
  dataSource;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor( private releaseNotes: ReleaseService ) { }

  ngOnInit() {
    this.releaseNotes.getReleaseNotes()
      .subscribe(data => {
        this.dataSource = new MatTableDataSource(data.results);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
      });
  }
  applyFilter(filterValue: string) {
    filterValue = filterValue.trim();
    filterValue = filterValue.toLowerCase();
    this.dataSource.filter = filterValue;
  }
}

我收到了An error was thrown in afterAll 我尝试调试了几个小时但没有成功。

我在asyncsync 中都尝试了beforeEach,我还把所有的依赖文件都导入了spec 文件。仍然抛出相同的错误。

请建议我如何解决此问题。

【问题讨论】:

    标签: angular jasmine karma-jasmine


    【解决方案1】:

    您似乎导入了HttpClientTestingModule,但随后正在使用:
    imports: [RouterTestingModule, HttpClientModule ],.这应该是HttpClientTestingModule

    然后,在浏览器中启动您的测试并打开您的console,您可能会在浏览器控制台中看到一些额外的问题,当您仅在 cmd 提示符下运行时不会显示这些问题。

    例如,我在使用源映射时遇到了一些问题,我不得不为我的测试禁用它 使用ng test --source-map=false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2022-01-23
      • 2020-11-25
      • 2020-08-19
      • 2015-10-01
      相关资源
      最近更新 更多