类型错误 this.service.newPerson 不是函数 angular 单元测试 jasmine 和 jest
type error this.service.newPerson is not a fucntion angular unit testing jasmine and jest
数据-detail.spec.ts
let component: PersonDetailComponent;
let fixture: ComponentFixture<PersonDetailComponent>;
const personServiceMock: PersonService = {getStringForRelation: jest.fn()} as any;
const dateAdapterMock: DateAdapter<Date> = {setLocale: jest.fn()} as any;
const NotificationServiceMock: DatevNotificationService = {} as any;
const apiServiceMock: ApiService = {} as any;
const clientServiceMock: ClientService = {} as any;
it('btnadddata für Kind', () => {
component.isNewPerson = true;
const nodeDataArray: Array<go.ObjectData> = [
{
idPerson: 1,
s: 'M',
key: 1,
personDetail: {
firstname: 'Vorname',
name: 'Nachname',
gender: PersonDetail.GenderEnum.IsMale,
isAlive: true,
},
quotasRelation: {
relationToOwner: QuotasRelation.RelationToOwnerEnum.Kind,
},
asset: {}
}
];
const relation: RelationToOwnerEnum = 'Kind';
const mid = 1;
const newPerson: Person = {
idPerson: 1,
// @ts-ignore
key: 1,
personDetail: {
firstname: 'test',
name: 'test1',
gender: 'IsMale',
idMother: 1,
idFather: null,
idSpouse: null,
birthdate: null,
isAlive: true,
},
quotasRelation: {
relationToOwner: relation,
},
asset: {}
};
// const id = component.person.
// const mid = component.person.personDetail.idMother;
// const firstname = component.person.personDetail.firstname;
// const gender = component.person.personDetail.gender;
component.person.quotasRelation.relationToOwner = relation;
component.person.idPerson = mid;
component.nodeDataArray = nodeDataArray;
component.btnAddPerson_Click(relation);
console.log();
component.personService.newPerson(1, 'test', 'test1', 'IsMale', mid, undefined,
undefined, relation).personDetail.idMother = mid;
expect(newPerson).toEqual(component.addChildToPerson(component.selectedNode.data));
});
人-detail.component.ts
btnAddPerson_Click(relation: RelationToOwnerEnum): void {
let newPerson: Person = null;
if (relation === RelationToOwnerEnum.Kind) {
newPerson = this.addChildToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.Gatte) {
newPerson = this.addSpouseToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.BruderSchwester) {
newPerson = this.addSiblingToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.Vater) {
newPerson = this.addParentToPerson(this.selectedNode.data);
}
if (newPerson) {
this.isNewPerson = true;
this.person = newPerson;
}
}
addChildToPerson(parent: Person): Person {
let fid: number;
let mid: number;
console.log('person', parent);
if (parent.personDetail.idSpouse === undefined) {
if (parent.personDetail.gender === PersonDetail.GenderEnum.IsMale) {
fid = parent.idPerson;
mid = undefined;
} else {
fid = undefined;
mid = parent.idPerson;
}
} else if (parent.personDetail.gender === PersonDetail.GenderEnum.IsMale) {
fid = parent.idPerson;
mid = parent.personDetail.idSpouse;
} else {
fid = parent.personDetail.idSpouse;
mid = parent.idPerson;
}
console.log('addChildToPerson Vater-Key Mutter-Key', fid, mid);
// tslint:disable-next-line:max-line-length
return this.personService.newPerson(this.getNextId(this.nodeDataArray), '',
parent.personDetail.name, null, mid, fid, -1,
QuotasRelation.RelationToOwnerEnum.Kind);
}
person.service.ts
export class PersonService {
constructor() {
}
private readonly DUMMYSPOUSE = 'DummySpouse';
private static getRelationWithGender(gender: GenderEnum, male: string, female: string,
diverse: string): string {
return gender && gender === GenderEnum.IsMale ? male : gender && gender ===
GenderEnum.IsFemale ? female : diverse;
}
public newPerson(
id: number,
firstname: string,
name: string,
gender: PersonDetail.GenderEnum,
idMother: number,
idFather: number,
idSpouse: number,
relation: QuotasRelation.RelationToOwnerEnum
): Person {
return {
idPerson: id,
// @ts-ignore
key: id,
personDetail: {
firstname,
name,
gender,
idMother,
idFather,
idSpouse,
birthdate: null,
isAlive: true,
},
quotasRelation: {
relationToOwner: relation,
},
asset: {}
};
}
i have tried several ways but i am not able to identify the error and
i know that newPerson is not a function but how to oversome this
error? I am new to testing , please help me.
how can i overcome this problem do i have to mock the method as new
person is not a method i cannot do that either
只需将函数添加到
的mckservice
const personServiceMock: PersonService = {getStringForRelation: jest.fn(),
newPerson: jest.fn()} as any
这非常简单,但配置起来非常困难,因为我是测试新手,但我知道如何配置。
数据-detail.spec.ts
let component: PersonDetailComponent;
let fixture: ComponentFixture<PersonDetailComponent>;
const personServiceMock: PersonService = {getStringForRelation: jest.fn()} as any;
const dateAdapterMock: DateAdapter<Date> = {setLocale: jest.fn()} as any;
const NotificationServiceMock: DatevNotificationService = {} as any;
const apiServiceMock: ApiService = {} as any;
const clientServiceMock: ClientService = {} as any;
it('btnadddata für Kind', () => {
component.isNewPerson = true;
const nodeDataArray: Array<go.ObjectData> = [
{
idPerson: 1,
s: 'M',
key: 1,
personDetail: {
firstname: 'Vorname',
name: 'Nachname',
gender: PersonDetail.GenderEnum.IsMale,
isAlive: true,
},
quotasRelation: {
relationToOwner: QuotasRelation.RelationToOwnerEnum.Kind,
},
asset: {}
}
];
const relation: RelationToOwnerEnum = 'Kind';
const mid = 1;
const newPerson: Person = {
idPerson: 1,
// @ts-ignore
key: 1,
personDetail: {
firstname: 'test',
name: 'test1',
gender: 'IsMale',
idMother: 1,
idFather: null,
idSpouse: null,
birthdate: null,
isAlive: true,
},
quotasRelation: {
relationToOwner: relation,
},
asset: {}
};
// const id = component.person.
// const mid = component.person.personDetail.idMother;
// const firstname = component.person.personDetail.firstname;
// const gender = component.person.personDetail.gender;
component.person.quotasRelation.relationToOwner = relation;
component.person.idPerson = mid;
component.nodeDataArray = nodeDataArray;
component.btnAddPerson_Click(relation);
console.log();
component.personService.newPerson(1, 'test', 'test1', 'IsMale', mid, undefined,
undefined, relation).personDetail.idMother = mid;
expect(newPerson).toEqual(component.addChildToPerson(component.selectedNode.data));
});
人-detail.component.ts
btnAddPerson_Click(relation: RelationToOwnerEnum): void {
let newPerson: Person = null;
if (relation === RelationToOwnerEnum.Kind) {
newPerson = this.addChildToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.Gatte) {
newPerson = this.addSpouseToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.BruderSchwester) {
newPerson = this.addSiblingToPerson(this.selectedNode.data);
} else if (relation === RelationToOwnerEnum.Vater) {
newPerson = this.addParentToPerson(this.selectedNode.data);
}
if (newPerson) {
this.isNewPerson = true;
this.person = newPerson;
}
}
addChildToPerson(parent: Person): Person {
let fid: number;
let mid: number;
console.log('person', parent);
if (parent.personDetail.idSpouse === undefined) {
if (parent.personDetail.gender === PersonDetail.GenderEnum.IsMale) {
fid = parent.idPerson;
mid = undefined;
} else {
fid = undefined;
mid = parent.idPerson;
}
} else if (parent.personDetail.gender === PersonDetail.GenderEnum.IsMale) {
fid = parent.idPerson;
mid = parent.personDetail.idSpouse;
} else {
fid = parent.personDetail.idSpouse;
mid = parent.idPerson;
}
console.log('addChildToPerson Vater-Key Mutter-Key', fid, mid);
// tslint:disable-next-line:max-line-length
return this.personService.newPerson(this.getNextId(this.nodeDataArray), '',
parent.personDetail.name, null, mid, fid, -1,
QuotasRelation.RelationToOwnerEnum.Kind);
}
person.service.ts
export class PersonService {
constructor() {
}
private readonly DUMMYSPOUSE = 'DummySpouse';
private static getRelationWithGender(gender: GenderEnum, male: string, female: string,
diverse: string): string {
return gender && gender === GenderEnum.IsMale ? male : gender && gender ===
GenderEnum.IsFemale ? female : diverse;
}
public newPerson(
id: number,
firstname: string,
name: string,
gender: PersonDetail.GenderEnum,
idMother: number,
idFather: number,
idSpouse: number,
relation: QuotasRelation.RelationToOwnerEnum
): Person {
return {
idPerson: id,
// @ts-ignore
key: id,
personDetail: {
firstname,
name,
gender,
idMother,
idFather,
idSpouse,
birthdate: null,
isAlive: true,
},
quotasRelation: {
relationToOwner: relation,
},
asset: {}
};
}
i have tried several ways but i am not able to identify the error and i know that newPerson is not a function but how to oversome this error? I am new to testing , please help me.
how can i overcome this problem do i have to mock the method as new person is not a method i cannot do that either
只需将函数添加到
的mckserviceconst personServiceMock: PersonService = {getStringForRelation: jest.fn(),
newPerson: jest.fn()} as any
这非常简单,但配置起来非常困难,因为我是测试新手,但我知道如何配置。