我正在尝试使用Jasmine为我的角度4应用程序编写测试。我需要测试Ok方法我设置的值不正确。我已经部分编写了测试但不确定如何使用jasmine验证OK方法。所以基本上当值无效时,isMinValid函数或isMaxValid函数会将相应的私有变量_isMinValid和_isMaxValid设置为false。
我已经为单独的函数编写了测试但不确定如何测试Ok方法
private _isMinValid: boolean = false;
private _isMaxValid: boolean = false;
public data: any;
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 ok() {
if (this._isMinValid && this._isMaxValid) {
this.data.domicileId = this.domicileId;
this.data.domicileInfo = this.domicileInfo;
this.hide(true);
}
}
isMinValid(currentItem: any, item_IDX: number) {
if (item_IDX === 0) {
this._isMinValid = true;
return true;
}
let previousItem = this.domicileInfo.taxAssesment.items[item_IDX - 1];
if (+currentItem.minSize !== +previousItem.maxSize) {
this._isMinValid = false;
return false;
}
return true;
}
isMaxValid(currentItem: any, item_IDX: number) {
if (item_IDX === 0) {
this._isMaxValid = true;
return true;
}
if (+currentItem.maxSize <= +currentItem.minSize) {
this._isMaxValid = false;
return false;
}
return true;
}
Tests
describe('DomicileSelectionComponent', () => {
let comp: DomicileSelectionComponent;
let fixture: ComponentFixture<DomicileSelectionComponent>;
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 false because the current item min value is not equal to the previous max value', () => {
comp.domicileInfo.taxAssesment.items = [{maxSize: 40000000 , minSize: 30000000 , values: [2 , 2]}];
let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items , 1);
//expect(isMin).toBe(false);
//expect(comp.ok()).toHaveBeenCalled();
});
});
答案 0 :(得分:0)
你的ok()
函数看起来非常简单。它的if
块只检查两个变量,看它们是否为真,然后再继续。所以要测试一下,只需设置这些变量并进行测试:
describe('ok()', () => {
it('should set variables and call "hide()" function', () => {
spyOn(comp, 'hide');
// first check to make sure that they aren't already set
expect(comp.data.domicileId).toEqual(null);
expect(comp.data.domicileInfo).toEqual(null);
comp._isMinValid = true;
comp._isMaxValid = true;
comp.domicileId = 1;
comp.domicileInfo = 'foo';
comp.ok();
expect(comp.data.domicileId).toEqual(1);
expect(comp.data.domicileInfo).toEqual('foo');
expect(comp.hide).toHaveBeenCalledWith(true);
});
});