2016-09-27 105 views
2

從Angular 2 RC5升級到2.0 Final後,運行我的測試時看到以下錯誤。我不確定問題是什麼。TypeError:在升級到Angular 2.0後無法讀取null屬性'injector'final

TypeError: Cannot read property 'injector' of null 
    at TestBed._createCompilerAndModule (webpack:///Users/mraible/dev/ng2-demo/~/@angular/core/testing/test_bed.js:247:0 <- src/test.ts:20777:44) 
    at TestBed._initIfNeeded (webpack:///Users/mraible/dev/ng2-demo/~/@angular/core/testing/test_bed.js:213:0 <- src/test.ts:20743:39) 
    at TestBed.createComponent (webpack:///Users/mraible/dev/ng2-demo/~/@angular/core/testing/test_bed.js:297:0 <- src/test.ts:20827:14) 

這裏是我的測試中的一個例子:

import { MockSearchService } from '../shared/search/mocks/search.service'; 
import { EditComponent } from './edit.component'; 
import { TestBed } from '@angular/core/testing/test_bed'; 
import { SearchService } from '../shared/search/search.service'; 
import { MockRouter, MockActivatedRoute } from '../shared/search/mocks/routes'; 
import { ActivatedRoute, Router } from '@angular/router'; 
import { FormsModule } from '@angular/forms'; 

describe('Component: Edit',() => { 
    let mockSearchService: MockSearchService; 
    let mockActivatedRoute: MockActivatedRoute; 
    let mockRouter: MockRouter; 

    beforeEach(() => { 
    mockSearchService = new MockSearchService(); 
    mockActivatedRoute = new MockActivatedRoute({'id': 1}); 
    mockRouter = new MockRouter(); 

    TestBed.configureTestingModule({ 
     declarations: [EditComponent], 
     providers: [ 
     {provide: SearchService, useValue: mockSearchService}, 
     {provide: ActivatedRoute, useValue: mockActivatedRoute}, 
     {provide: Router, useValue: mockRouter} 
     ], 
     imports: [FormsModule] 
    }); 
    }); 

    it('should fetch a single record',() => { 
    const fixture = TestBed.createComponent(EditComponent); 

    let person = {name: 'Emmanuel Sanders', address: {city: 'Denver'}}; 
    mockSearchService.setResponse(person); 

    fixture.detectChanges(); 
    // verify service was called 
    expect(mockSearchService.getByIdSpy).toHaveBeenCalledWith(1); 

    // verify data was set on component when initialized 
    let editComponent = fixture.debugElement.componentInstance; 
    expect(editComponent.editAddress.city).toBe('Denver'); 

    // verify HTML renders as expected 
    let compiled = fixture.debugElement.nativeElement; 
    expect(compiled.querySelector('h3').innerHTML).toBe('Emmanuel Sanders'); 
    }); 
}); 

有關演示這個問題拉入請求,請參閱GitHub的:https://github.com/mraible/ng2-demo/pull/4

回答

1

更改import { TestBed } from '@angular/core/testing/test_bed';import { TestBed } from '@angular/core/testing';解決了這個問題對我來說。

相關問題