Angular 运行jasmine测试时返回null的变量
我已经实现了jasmine测试并测试了我的方法。在运行测试时,previousItem的值为null。如果我将previousitem的值硬编码为20000000,它将返回true,这很好。如何在测试中初始化previousitem值 请注意,数组中的第一个元素将包含minSize:0和maxSize:20000000Angular 运行jasmine测试时返回null的变量,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我已经实现了jasmine测试并测试了我的方法。在运行测试时,previousItem的值为null。如果我将previousitem的值硬编码为20000000,它将返回true,这很好。如何在测试中初始化previousitem值 请注意,数组中的第一个元素将包含minSize:0和maxSize:20000000 isMinValid(currentItem: any, item_IDX: number) { if (item_IDX === 0) {
isMinValid(currentItem: any, item_IDX: number) {
if (item_IDX === 0) {
return true;
}
let previousItem = this.domicileInfo.taxAssesment.items[item_IDX - 1];
if (+currentItem.minSize !== +previousItem.maxSize ) {
return false;
}
return true;
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TooltipModule.forRoot(),
FormsModule,
TranslateModule.forRoot({
loader: { provide: TranslateLoader, useClass: TranslateFakeLoader }
})
],
providers: [
{ provide: BsModalRef, useClass: BsModalRefStub },
{ provide: BackendProxy.ReferenceProxy, useClass: ReferenceProxyStub },
{ provide: RunService, useValue: runServiceStub }
],
declarations: [DomicileSelectionComponent, YesNoPipe, CLICK_INPUT_DIRECTIVE, ShortNumberFormatPipe]
});
});
beforeEach(() => {
fixture = TestBed.createComponent(DomicileSelectionComponent);
comp = fixture.componentInstance;
comp.domicileInfo = domicileInformationDataResult.data;
fixture.detectChanges();
});
fit('should return true because the current item min value is equal to the previous max value', () => {
comp.domicileInfo.taxAssesment.items = [{maxSize: 30000000 , minSize: 20000000 , values: [2 , 2]}];
let isMax: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items[0] , 1);
console.log(isMax);
console.log(comp.domicileInfo.taxAssesment.items);
expect(isMax).toBe(true);
});
组件代码
import { Component, OnInit } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap';
import { Base } from '@wtw/toolkit';
import * as BackendDto from '../../../../api/dtos';
import * as BackendProxy from '../../../../api/proxies';
import { IModalConfirmation } from '@wtw/platform/interfaces';
import { TranslateService } from '@ngx-translate/core';
import * as Rx from 'rxjs/Rx';
import { RunService } from '@wtw/platform/services';
import { CONSTS } from '../../../../config';
const minCapRequirement = 'minCapReq';
const premuimTaxCap = 'premTaxCap';
@Component({
selector: 'app-domicile-selection',
templateUrl: './domicile-selection.component.html'
})
export class DomicileSelectionComponent extends Base.ReactiveComponent implements OnInit, IModalConfirmation {
domicileInfo: BackendDto.DomicileInformation;
public data: any;
public onClose: Rx.Subject<boolean>;
public active = false;
public currentSelectedCurrency = '';
public domicileId: number;
public domiciles: BackendDto.Domicile[];
public amendAssumptions: string;
fieldCategories: typeof BackendDto.DynamicFieldCategory = BackendDto.DynamicFieldCategory;
private domicile: BackendDto.Domicile;
private _selectedIndustries: BackendDto.Industry[];
constructor(
private _bsModalRef: BsModalRef,
private _refProxy: BackendProxy.ReferenceProxy,
private _runs: RunService,
private _translate: TranslateService
) {
super();
}
public ngOnInit() {
this.onClose = new Rx.Subject();
return [this._runs.activeRun.subscribe(r => {
this.currentSelectedCurrency = r.currencyInfo.currentSelectedCurrency;
})];
}
public show() {
this.domicileId = this.data.domicile.id;
this.domicile = this.data.domicile;
this.domiciles = this.data.domiciles;
this._selectedIndustries = this.data.selectedIndustries;
this.domicileInfo = this.domicile.domicileInformation;
this.amendAssumptions = this._translate.instant('CAPTIVES.DOMICILES.AMENDASSUMPTIONS', { domicile: this.domicile.name });
this.active = true;
}
public close() {
this.hide(null);
}
public udpatevalue(item, value) {
item.maxSize = value;
}
public ok() {
this.data.domicileId = this.domicileId;
this.data.domicileInfo = this.domicileInfo;
this.hide(true);
}
public cancel() {
this.hide(false);
}
isMinValid(currentItem: any, item_IDX: number) {
if (item_IDX === 0) {
return true;
}
debugger;
let previousItem = this.domicileInfo.taxAssesment.items[item_IDX - 1];
if (+currentItem.minSize !== +previousItem.maxSize ) {
return false;
}
return true;
}
isMaxValid(currentItem: any, item_IDX: number) {
if (item_IDX === 0) {
return true;
}
if (+currentItem.maxSize <= +currentItem.minSize ) {
return false;
}
return true;
}
domicileChanged($event) {
if (this.domicileId === this.domicile.id) {
this.domicileInfo = this.domicile.domicileInformation;
} else {
this._loadInformation();
}
}
private _loadInformation() {
this._refProxy.getDefaultDomicileInformationForClient(this.domicileId, this._selectedIndustries[0].id)
.uiSignal('infos')
.subscribe(ret => {
this.domicileInfo = ret.data;
const domicileName = this.domiciles.find(x => x.id === this.domicileId).name;
this.amendAssumptions = this._translate.instant('CAPTIVES.DOMICILES.AMENDASSUMPTIONS', { domicile: domicileName });
});
}
private hide(nextVal?: boolean) {
this.active = false;
this.onClose.next(nextVal);
this._bsModalRef.hide();
}
get addnDomicileDetailMinCapReq(): BackendDto.DomicileAddnDetail {
return this.domicileInfo.addnDomcileDetails.find(x => x.fieldInfo.key === minCapRequirement);
}
get addnDomicileDetailPremuimTaxCap(): BackendDto.DomicileAddnDetail {
return this.domicileInfo.addnDomcileDetails.find(x => x.fieldInfo.key === premuimTaxCap);
}
get maxCurrency() {
return CONSTS.general.maximumCurrencyValue;
}
}
从'@angular/core'导入{Component,OnInit};
从“ngx引导”导入{BsModalRef};
从'@wtw/toolkit'导入{Base};
将*作为后端DTO从“../../../../api/DTO”导入;
从“../../../../api/proxies”导入*作为后端代理;
从'@wtw/platform/interfaces'导入{IModalConfirmation};
从'@ngx translate/core'导入{TranslateService};
从“rxjs/Rx”导入*作为Rx;
从'@wtw/platform/services'导入{RunService};
从“../../../../config”导入{CONSTS};
const minCapRequirement='minCapReq';
const premuimTaxCap='premTaxCap';
@组成部分({
选择器:'应用程序住所选择',
templateUrl:“./hombiesselection.component.html”
})
导出类DomicileSelectionComponent扩展了Base.ReactiveComponent实现OnInit、IModalConfirmation{
住所信息:后端到住所信息;
公共数据:任何;
公共关系:Rx.Subject;
公共活动=假;
public currentSelectedCurrency='';
公共住所ID:编号;
公共住所:后端至住所[];
公共修正假设:字符串;
字段类别:BackendDto.DynamicFieldCategory的类型=BackendDto.DynamicFieldCategory;
私人住所:后台至住所;
私人_选择的行业:后端到行业[];
建造师(
私人(bsModalRef):,
private _refProxy:BackendProxy.ReferenceProxy,
私人运行:运行服务,
私人翻译:翻译服务
) {
超级();
}
公共ngOnInit(){
this.onClose=new Rx.Subject();
return[this.\u runs.activeRun.subscribe(r=>{
this.currentSelectedCurrency=r.currencyInfo.currentSelectedCurrency;
})];
}
公开展览(){
this.domincileId=this.data.homily.id;
this.directory=this.data.directory;
this.domiciles=this.data.domiciles;
this.\u selectedIndustries=this.data.selectedIndustries;
this.domincileinfo=this.homily.domincileinfo;
this.amendAssumptions=this.\u translate.instant('CAPTIVES.DOMICILES.amendAssumptions',{horibility:this.horibility.name});
this.active=true;
}
公共关闭(){
this.hide(null);
}
公共价值(项目、价值){
item.maxSize=值;
}
公共ok(){
this.data.domicileId=this.domicileId;
this.data.domicileInfo=this.domicileInfo;
这个。隐藏(真);
}
公共取消{
这个。隐藏(假);
}
ISMIVALID(当前项目:任何,项目编号){
如果(项_IDX==0){
返回true;
}
调试器;
让previousItem=this.domincileInfo.taxassement.items[item_IDX-1];
如果(+currentItem.minSize!==+previousItem.maxSize){
返回false;
}
返回true;
}
isMaxValid(当前项目:任何,项目编号){
如果(项_IDX==0){
返回true;
}
如果(+currentItem.maxSize{
this.domicileInfo=ret.data;
const domincileName=this.dominciles.find(x=>x.id==this.domincileId).name;
this.amendAssumptions=this.\u translate.instant('CAPTIVES.DOMICILES.amendAssumptions',{住所:domicileName});
});
}
私有隐藏(nextVal?:布尔值){
this.active=false;
this.onClose.next(nextVal);
这是。bsModalRef.hide();
}
get-addnDomicileDetailMinCapReq():BackendDto.domincileaddnDetail{
返回这个.domincileInfo.addnDomcileDetails.find(x=>x.fieldInfo.key==minCapRequirement);
}
获取addnDomicileDetailPremuimTaxCap():BackendDto.DomicileAdnDetail{
返回此.domincileInfo.addnDomcileDetails.find(x=>x.fieldInfo.key==premuimTaxCap);
}
获取maxCurrency(){
返回CONSTS.general.maximumCurrencyValue;
}
}
您错过了fixture.detectChanges()代码>
我希望下面的解决方案能解决您的问题
fit('should return true because the current item min value is equal to the previous max value', () => {
comp.domicileInfo.taxAssesment.items = [{maxSize: 30000000 , minSize: 20000000 , values: [2 , 2]}];
fixture.detectChanges();
let isMax: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items[0] , 1);
expect(isMax).toBe(true);
});
编辑:
此外,您还没有在testbed中配置测试模块。所以请检查下面的内容
beforeEach(() => {
TestBed.configureTestingModule({
imports: [YourModules],
declarations: [YourComponent],
providers: [
{ provide: CreateVersionService, useClass: MockCreateVersion }//providers
]
});
fixture = TestBed.createComponent(DomicileSelectionComponent);
comp = fixture.componentInstance;
comp.domicileInfo = domicileInformationDataResult.data;
fixture.detectChanges();
});
你能在发布之前发布你的beforeach()吗。我不确定这是否是因为空值问题,因为当我尝试在chrome浏览器中调试时,所有正常工作的测试方法都会得到空值。可能是我错过了什么,或者我不明白。那没什么区别。你可以发布你的组件构造函数吗?我已经更新了我的答案。让我知道你的其他方法都很有效??我之前错过了分享一个。请看我更新的帖子。我不认为这个问题是与之前。正如我在文章中提到的,如果我在方法中为前一个项硬编码值,我会得到正确的响应。因此,出于某种原因,我觉得在执行测试时,前面的项目不能被初始化