5

我正在为我的角度应用程序完成测试台。但是 karma-jasmine 测试有问题,会报错

[对象错误事件] 抛出

我更新了 node_modules 作为我在以下链接中找到的解决方案 如何在我的 Karma/Jasmine 测试中调试“[object ErrorEvent] throwed”错误?

但是现在随机抛出错误,有时测试床完成时没有任何错误,有时会触发错误。有什么建议可以永久避免吗?

PS - 如果您需要更多资源,请在评论中告诉我。谢谢!

SomeComponent.spec.ts

import { RouterTestingModule } from '@angular/router/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { SomeComponent } from './some.component';
import { HttpLoaderFactory } from '../app.module';
import { AppRoutingModule } from '../app-routing.module';    
import { SomeService } from './../services/some.service';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
          }
        }),
        HttpClientModule,
        AppRoutingModule,
        FormsModule,
        ReactiveFormsModule ,
        RouterTestingModule,
        NgbModule.forRoot(),
        FormsModule, 
        ReactiveFormsModule,
      ],
      declarations: [
        SomeComponent
       ],
      providers: [
        SomeService
       ]
    })
    .compileComponents();
  }));

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

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

2 回答 2

3

我遇到了同样的问题,结果是升级到 jasmine-core 3.0.0 导致了这个问题,所以我降级到 2.5.2 并且一切正常。我认为那是因为karma-jasmine还不兼容jasmine 3.0.0

这就是我现在所拥有的:

"jasmine": "2.5.2",
"jasmine-core": "2.5.2",
"karma-jasmine": "1.1.2",

有关该问题的更多详细信息,请参阅:

https://github.com/jasmine/jasmine/issues/1523

于 2018-05-11T19:13:16.740 回答
0

我也有同样的问题, afterAll() 方法解决了我的问题。我的解决方案在这里

afterAll(() => {
 TestBed.resetTestingModule();
});
于 2018-12-11T07:39:21.313 回答