无法读取null jasmine angular 2的属性’injector’
发布时间:2020-12-17 17:27:30 所属栏目:安全 来源:网络整理
导读:在角度2中运行茉莉花规格时出现此错误: Cannot read property ‘injector’ of null jasmine angular 2 堆栈跟踪: TypeError: Cannot read property 'injector' of null at TestBed._createCompilerAndModule (http://localhost:3002/node_modules/@angula
在角度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'); }); }); 为什么我收到错误?没有inject关键字,但我想TestBed可能会在幕后使用它. 解决方法
在某些时候(在执行任何测试之前),您需要通过调用
TestBed.initTestEnvironment(...) 来初始化测试环境.
您通常会在karma-test-shim文件中看到这一点,如angular quickstart所示(测试文档中的快速入门).但是,如果您不使用此技术,则需要在测试文件中执行此操作.但initTestEnvironment只应调用一次,因此您还需要在每个测试文件中重置它 import { BrowserDynamicTestingModule,platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; beforeAll(() => { TestBed.resetTestEnvironment(); TestBed.initTestEnvironment(BrowserDynamicTestingModule,platformBrowserDynamicTesting()); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容