gpt4 book ai didi

unit-testing - 在 angular2 和 jasmine 中运行测试时,无法读取 TestBed.createComponent 中 null 的属性 'injector'

转载 作者:太空狗 更新时间:2023-10-29 17:33:52 25 4
gpt4 key购买 nike

当使用 karma 和 browserify 运行简单测试时,我的测试在 TestBed.createComponent() 函数内崩溃。

这是崩溃的 beforeEach:

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


beforeEach(() => {

// refine the test module by declaring the test component
TestBed.configureTestingModule({
declarations: [ AboutPage ],
});

// create component and test fixture
fixture = TestBed.createComponent(AboutPage);

// get test component from the fixture
comp = fixture.componentInstance;
});

收到的错误:

Chrome 53.0.2785 (Mac OS X 10.11.6) About component should display original title FAILED
TypeError: Cannot read property 'injector' of null
at TestBed._createCompilerAndModule (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/node_modules/@angular/core/bundles/core-testing.umd.js:834:0 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:3968:48)
at TestBed._initIfNeeded (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/node_modules/@angular/core/bundles/core-testing.umd.js:800:0 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:3934:43)
at TestBed.createComponent (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/node_modules/@angular/core/bundles/core-testing.umd.js:884:0 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:4018:18)
at Function.TestBed.createComponent (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/node_modules/@angular/core/bundles/core-testing.umd.js:714:0 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:3848:33)
at Object.<anonymous> (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/src/pages/about/about.spec.ts:24:22 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:50004:37)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:203:28)
at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:72:39)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:202:34)
at Zone.run (node_modules/zone.js/dist/zone.js:96:43)
at Object.<anonymous> (node_modules/zone.js/dist/jasmine-patch.js:91:50)
TypeError: Cannot read property 'detectChanges' of undefined
at Object.<anonymous> (/var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/src/pages/about/about.spec.ts:33:12 <- /var/folders/by/05whlcq9247dyx85bd0l183w0000gn/T/57bca79162ed1bcbdc3d746a5d9211d7.browserify:50010:16)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:203:28)
at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:72:39)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:202:34)
at Zone.run (node_modules/zone.js/dist/zone.js:96:43)
at Object.<anonymous> (node_modules/zone.js/dist/jasmine-patch.js:91:50)
at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (node_modules/zone.js/dist/jasmine-patch.js:119:42)

这是代码。

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

@Component({
selector: 'page-about',
templateUrl: 'about.html'
})
export class AboutPage {

helloText: string;

constructor(public navCtrl: NavController) {

}

sayHello(){
this.helloText = "hello";
}
}

我们将不胜感激。

  • 操作系统:osx 10.11.6
  • Angular 版本: 2.0.1“zone.js”:“^0.6.21”"浏览器化": "13.1.0"

  • 节点: node --version = v6.7.0

最佳答案

我猜你忘了初始化平台:

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

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

另见 Plunker Example

关于unit-testing - 在 angular2 和 jasmine 中运行测试时,无法读取 TestBed.createComponent 中 null 的属性 'injector',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40080027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com