Karma + Jasmine: Cannot read property 'getComponentFromError'

后端 未结 1 809
抹茶落季
抹茶落季 2020-12-11 03:33

I am following this tutorial: https://angular.io/guide/testing#component-test-scenarios for karma+jasmine unit testing. Here my code:

import { AppComponent }         


        
相关标签:
1条回答
  • 2020-12-11 03:47

    I ran into something similar and found the answer here: Cannot read property 'injector' of null jasmine angular 2.

    I just added this to my beforeEach() method which solved this error for me (there were a few others I had to overcome before I got this all working entirely).

     TestBed.resetTestEnvironment();
     TestBed.initTestEnvironment(BrowserDynamicTestingModule,
        platformBrowserDynamicTesting());
    

    Basically it was just a matter of changing this:

    beforeEach(() => {
      TestBed.configureTestingModule({
        declarations: [ AppComponent ],
       });
      fixture = TestBed.createComponent(AppComponent);
      component = fixture.componentInstance; 
      h1 = fixture.nativeElement.querySelector('h1');
    });
    

    To

    import { BrowserDynamicTestingModule,
    platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
    
    beforeEach(() => {
     TestBed.resetTestEnvironment();
     TestBed.initTestEnvironment(BrowserDynamicTestingModule,
        platformBrowserDynamicTesting());
    
      TestBed.configureTestingModule({
        declarations: [ AppComponent ],
       });
      fixture = TestBed.createComponent(AppComponent);
      component = fixture.componentInstance; 
      h1 = fixture.nativeElement.querySelector('h1');
    });
    
    0 讨论(0)
提交回复
热议问题