2016-11-20 70 views
2

在角2運行茉莉規範時,我得到這個錯誤的特性「噴油」:無法讀取空茉莉花角2

無法讀取空茉莉的特性「噴油」角2

堆棧跟蹤:

TypeError: Cannot read property 'injector' of null 
    at TestBed._createCompilerAndModule (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:834:48) 
    at TestBed._initIfNeeded (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:800:43) 
    at TestBed.createComponent (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:884:18) 
    at Function.TestBed.createComponent (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:714:33) 
    at Object.eval (http://localhost:3002/js/app/landing-page/subcomponents/middle-row.component.spec.js:29:49) 
    at ZoneDelegate.invoke (http://localhost:3002/node_modules/zone.js/dist/zone.js:232:26) 
    at ProxyZoneSpec.onInvoke (http://localhost:3002/node_modules/zone.js/dist/proxy.js:79:39) 
    at ZoneDelegate.invoke (http://localhost:3002/node_modules/zone.js/dist/zone.js:231:32) 
    at Zone.run (http://localhost:3002/node_modules/zone.js/dist/zone.js:114:43) 
    at Object.eval (http://localhost:3002/node_modules/zone.js/dist/jasmine-patch.js:102:34) 

我從the official angular 2 testing docs複製該規範:

let comp: BannerComponent; 
let fixture: ComponentFixture<BannerComponent>; 
let de:  DebugElement; 
let el:  HTMLElement; 

describe('BannerComponent',() => { 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ BannerComponent ], // declare the test component 
    }); 

    fixture = TestBed.createComponent(BannerComponent); 

    comp = fixture.componentInstance; // BannerComponent test instance 

    // query for the title <h1> by CSS element selector 
    de = fixture.debugElement.query(By.css('h1')); 
    el = de.nativeElement; 

    }); 
}); 

,並適應它有一點點我的代碼工作:

import 'zone.js/dist/long-stack-trace-zone.js'; 
import 'zone.js/dist/async-test.js'; 
import 'zone.js/dist/fake-async-test.js'; 
import 'zone.js/dist/sync-test.js'; 
import 'zone.js/dist/proxy.js'; 
import 'zone.js/dist/jasmine-patch.js'; 

import { ComponentFixture, TestBed } from '@angular/core/testing'; 
import { By } from '@angular/platform-browser'; 
import { DebugElement } from '@angular/core'; 

import { MiddleRowComponent } from './middle-row.component'; 

let comp: MiddleRowComponent; 
let fixture: ComponentFixture<MiddleRowComponent>; 
let de: DebugElement; 
let el: HTMLElement; 

describe('MiddleRowComponent',() => { 
    beforeEach(() => { 
     TestBed.configureTestingModule({ 
     declarations: [MiddleRowComponent], // declare the test component 
     }); 

     fixture = TestBed.createComponent(MiddleRowComponent); 

     comp = fixture.componentInstance; // MiddleRowComponent test instance 

     // query for the title <h1> by CSS element selector 
     de = fixture.debugElement.query(By.css('h1')); 
     el = de.nativeElement; 
    }); 

    it('should display original title',() => { 
     fixture.detectChanges(); 
     expect(el.textContent).toContain(comp.word); 
    }); 

    it('should display a different test title',() => { 
     comp.word = 'Test Title'; 
     fixture.detectChanges(); 
     expect(el.textContent).toContain('Test Title'); 
    }); 
}); 

爲什麼會出現錯誤?沒有注入關鍵字,但我想TestBed可能會在幕後使用它。

回答

3

在某個時刻(在執行任何測試之前),您需要通過調用TestBed.initTestEnvironment(...)來初始化測試環境。

您通常會在karma-test-shim文件中看到此操作,如angular quickstart(與測試文檔相同的快速入門)所示。但是如果你不使用這種技術,那麼你需要在測試文件中做到這一點。但initTestEnvironment應該只被調用一次,所以你還需要在每個測試文件中重置它

import { BrowserDynamicTestingModule, 
     platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; 

beforeAll(() => { 
    TestBed.resetTestEnvironment(); 
    TestBed.initTestEnvironment(BrowserDynamicTestingModule, 
           platformBrowserDynamicTesting()); 
}); 
+0

謝謝。我現在只是遇到這個錯誤:'file:app/landing-page/subcomponents/middle-row.component.spec.ts' 嚴重程度:'錯誤' 消息:'參數類型'(extraProviders ?: Provider [ ])=> PlatformRef'不能分配給'PlatformRef'類型的參數。 Property'bootstrapModuleFactory'缺少類型'(extraProviders?:Provider [])=> PlatformRef'。' at:'26,34' source:'ts''我必須需要添加一些東西到我的應用程序的引導程序? 'platformBrowserDynamic()bootstrapModule(的AppModule);'? – BeniaminoBaggins

+0

對不起,第二個參數應該是_call_方法,而不僅僅是方法。我解決了它 –

+0

這可能是解決方案,我只是試圖得到[另一個問題](http://stackoverflow.com/q/40700142/3935156)整理出來,以便我可以驗證它的工作。我從@angular導入測試類時發生了一些奇怪的事情。謝謝。 – BeniaminoBaggins