代码之家  ›  专栏  ›  技术社区  ›  Fateh Mohamed

Jasmine 3角度5.2测试中的错误

  •  2
  • Fateh Mohamed  · 技术社区  · 6 年前

    运行后出现一些错误 ng test ,我在对所有软件包进行升级后发现,以下是版本:

    {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
     ...
     "zone.js": "^0.8.19"
     }
    

    和devDependencies

    "jasmine-core": "~3.1.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0"
    

    我遇到了以下错误:

    "message": "An error was thrown in afterAll\n[object ErrorEvent]", "str": "An error was thrown in afterAll\n[object ErrorEvent]"
    

    karma screenshot

    我的所有测试都非常简单,如:

    import { async, TestBed  } from '@angular/core/testing';
    import { UserInmailComponent } from './user-inmail.component';
    
    describe('Component: UserInmail', () => {
    it('should create an instance', () => {
      const component = new UserInmailComponent();
      expect(component).toBeTruthy();
    });
    });
    

    我不认为是我的测试文件导致了这个问题。

    3 回复  |  直到 6 年前
        1
  •  4
  •   user3292546    6 年前

    即使在“jasmine core v2.8.0”的几个单元测试中,我也遇到了这个错误消息的问题。经过一些研究,我能够复制它,并发现当您使用缺少必要变量的模拟或存根时(在我的例子中是>可观察的)会发生这种情况。

    例如,如果您像这样初始化测试台:

    TestBed.configureTestingModule({
      declarations: [HeaderComponent],
      imports: [BrowserModule],
      providers: [
        {provide: AuthService, useClass: AuthServiceStub}
      ]
    })
    

    您在AuthService中有一个observate,它在您的HeaderComponent中以某种方式使用,但您忘记在“AuthServiceStub”中定义这个observate,那么它将导致 “在之后引发了一个错误\n[对象错误事件]” 输出

    这是茉莉花/因果报应的一种非常令人困惑的行为,因为它不会给你任何错误的暗示。在我看来,它应该抛出一个空指针异常,因为该属性没有在存根中定义。

        2
  •  1
  •   Luca Luve    6 年前

    这与区域中的一个bug有关。js公司 您必须将jasmine版本降级到2.9才能正常工作

    "jasmine-core": "~2.9.0",
    
        3
  •  1
  •   Scott Williams    6 年前

    根据我的研究,这似乎是一个非常广泛的错误,可能来自许多开发人员的错误。 我的具体错误是使用未定义的url调用HttpClient(HttpClientTestingModule)。这个错误在我的浏览器中被相对抑制了,一旦我修复了它,现在每次测试都通过了。我花了两天的时间把头发拔出来,但我所需要做的就是茉莉间谍。和callThrough()而不仅仅是使用spyOn