- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试运行 karma TS spec 文件时出错。所有模块和导入似乎都工作正常,没有冲突。尝试将 component.ngOninit()
添加到 beforeEach() 和 it() 中,但没有任何成功。查看类似问题与 HttpClientTestingModule 导入冲突或其他提供程序/声明冲突有关。我无法确定为什么 TypeError: Cannot read property 'get' of undefined is not allowing this spec file to pass
规范文件
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {AcaPersonComponent} from './aca-person.component';
import {
MatCardModule, MatDividerModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatOptionModule,
MatRadioModule,
MatSelectModule
} from '@angular/material';
import {ReactiveFormsModule} from '@angular/forms';
import {MFGDatePickerModule} from 'mfg-datepicker';
import {TextMaskModule} from 'angular2-text-mask';
import {AppRoutingModule} from '../../../../../app-routing.module';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {LandingPageComponent} from '../../../../../landing-page/landing-page.component';
describe('AcaPersonComponent', () => {
let component: AcaPersonComponent;
let fixture: ComponentFixture<AcaPersonComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AcaPersonComponent,
LandingPageComponent],
imports: [MatFormFieldModule,
ReactiveFormsModule,
MatIconModule,
MatInputModule,
MFGDatePickerModule,
MatOptionModule,
MatSelectModule,
MatRadioModule,
MatCardModule,
TextMaskModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientTestingModule,
MatDividerModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AcaPersonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.ngOnInit();
});
it('should create', () => {
expect(component).toBeTruthy();
component.ngOnInit();
});
});
错误
TypeError: Cannot read property 'get' of undefined
error properties: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 33669121, rootNodeFlags: 33554433, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 114688, directChildFlags: 114688, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 1, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: '', name: 'app-aca-person', attrs: [ ], template: null, componentProvider: Object({ nodeIndex: 1, parent: <circular reference: Object>, renderParent: <circular reference: Object>, bindingIndex: 0, outputIndex: 0, checkIndex: 1, flags: 114688, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0, references: Object, ngContentIndex: -1, childCount: 0, bindings: Array, bindingFlags: 0, outputs: ...
at <Jasmine>
at AcaPersonComponent.get type [as type] (http://localhost:9876/_karma_webpack_/src/app/aca/aca-stepper/aca-steps/aca-step-2/aca-person/aca-person.component.ts:70:28)
at AcaPersonComponent.ngOnInit (http://localhost:9876/_karma_webpack_/src/app/aca/aca-stepper/aca-steps/aca-step-2/aca-person/aca-person.component.ts:24:14)
at checkAndUpdateDirectiveInline (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:31909:1)
at checkAndUpdateNodeInline (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:44366:1)
at checkAndUpdateNode (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:44305:1)
at debugCheckAndUpdateNode (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:45327:36)
at debugCheckDirectivesFn (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:45270:1)
at Object.eval [as updateDirectives] (ng:///DynamicTestModule/AcaPersonComponent_Host.ngfactory.js:7:5)
at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:45258:1)
at checkAndUpdateView (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:44270:1)
TypeError: Cannot read property 'get' of undefined
at <Jasmine>
at AcaPersonComponent.get type [as type] (http://localhost:9876/_karma_webpack_/src/app/aca/aca-stepper/aca-steps/aca-step-2/aca-person/aca-person.component.ts:70:28)
at AcaPersonComponent.ngOnInit (http://localhost:9876/_karma_webpack_/src/app/aca/aca-stepper/aca-steps/aca-step-2/aca-person/aca-person.component.ts:24:14)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/aca/aca-stepper/aca-steps/aca-step-2/aca-person/aca-person.component.spec.ts:58:15)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:305:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:554:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:569:1)
at <Jasmine>
TS 类
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {FormGroup, Validators} from '@angular/forms';
import {merge} from 'rxjs';
@Component({
selector: 'app-aca-person',
templateUrl: './aca-person.component.html',
styleUrls: ['./aca-person.component.css']
})
export class AcaPersonComponent implements OnInit {
@Input() personType: string;
@Input() peopleForm: FormGroup;
@Input() nextWasClicked: boolean;
@Input() index: number;
@Output() removePerson = new EventEmitter<boolean>();
cardTitle: string;
personAge: number;
constructor() {
}
ngOnInit() {
if (this.type.value == 'primary') {
this.cardTitle = 'Your Information';
} else if (this.type.value == 'spouse') {
this.cardTitle = 'Spouse';
} else if (this.type.value == 'dependent') {
this.cardTitle = 'Dependent';
}
this.dobAndIsPregnantVC();
}
dobAndIsPregnantVC() {
merge(
this.dob.valueChanges,
this.gender.valueChanges
)
.subscribe(() => {
this.calculateAge(this.dob.value);
if (this.gender.value == 'F') {
this.is_pregnant.setValidators(Validators.required);
if (this.personAge < 14) {
this.is_pregnant.setValue(null);
this.is_pregnant.clearValidators();
}
} else {
this.is_pregnant.setValue(null);
this.is_pregnant.clearValidators();
}
this.is_pregnant.updateValueAndValidity();
});
}
// Calculate the age of the client from their birth date to display in the census details
private calculateAge(date: string) {
if (date) {
const timeDiff = Math.abs(Date.now() - new Date(date).getTime());
this.personAge = Math.floor(timeDiff / (1000 * 3600 * 24) / 365.25);
}
}
onRemovePerson() {
this.removePerson.emit(true);
}
// GETTERS
get type() {
return this.peopleForm.get('type');
}
get first_name() {
return this.peopleForm.get('first_name');
}
get last_name() {
return this.peopleForm.get('last_name');
}
get gender() {
return this.peopleForm.get('gender');
}
get dob() {
return this.peopleForm.get('dob');
}
get uses_tobacco() {
return this.peopleForm.get('uses_tobacco');
}
get affordable_care() {
return this.peopleForm.get('affordable_care');
}
get is_pregnant() {
return this.peopleForm.get('is_pregnant');
}
}
最佳答案
它的发生是因为你的测试用例中没有定义@Input
,所以你需要按如下方式创建它:
beforeEach(() => {
fixture = TestBed.createComponent(AcaPersonComponent);
component = fixture.componentInstance;
component.peopleForm = fb.group({
type: ['Other Name', Validators.required],
first_name: ['f_name', Validators.required],
//.. and so on for all params
});
component.personType = 'Some Type';
component.nextWasClicked = true;
component.index = 1; // or whatever is required
fixture.detectChanges();
});
这将帮助您定义 this.peopleForm
并且像 this.peopleForm.get('type');
这样的行不会给出这样的错误
无需添加component.ngOnInit()
关于angular - 类型错误 : Cannot read property 'get' of undefined (Fixed conflicting imports not working),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58805441/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!