TypeError: Cannot read property 'injector' of null after upgrading to Angular 2.0 final
TypeError: Cannot read property 'injector' of null after upgrading to Angular 2.0 final
从 Angular 2 RC5 升级到 2.0 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
将 import { TestBed } from '@angular/core/testing/test_bed';
更改为 import { TestBed } from '@angular/core/testing';
为我解决了这个问题。
从 Angular 2 RC5 升级到 2.0 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
将 import { TestBed } from '@angular/core/testing/test_bed';
更改为 import { TestBed } from '@angular/core/testing';
为我解决了这个问题。