我最近开始使用Angular6进行开发,现在有一段时间出现错误。
编写单元测试时,我会创建一个json http调用的模拟,然后编写测试。
我的模型(feiertag.model.ts):
export class Feiertag implements Deserializable {
ID: number;
Feiertag: string;
Datum: string;
deserialize(input: any) {
Object.assign(this, input);
return this;
}
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
我的服务中的功能:
isHoliday(date: NgbDateStruct, holidays: Array<Feiertag>) {
let holiday = false;
let holidayName = '';
holidays.forEach(element => {
if (holiday == false) {
let d = new Date(element.Datum);
if (d.getFullYear() === date.year && d.getMonth() === date.month - 1 && d.getDate() === date.day) {
holiday = true;
holidayName = element.Feiertag;
}
}
});
return holiday;
}
模拟的http响应:
const FEIERTAGE = [
{
"ID":1,
"Feiertag":"Neujahr",
"Datum":"2016-01-01T00:00:00.000Z"
},
{
"ID":2,
"Feiertag":"Neujahr",
"Datum":"2017-01-01T00:00:00.000Z"
},
{
"ID":3,
"Feiertag":"Neujahr",
"Datum":"2018-01-01T00:00:00.000Z"
}
];
我尝试运行测试:
fit('isVacation() should return Vacation note on Vacation', inject([UrlaubMock], (service: UrlaubMock) => {
expect(service.isVacation({year: 2018, month: 1, day: 1}), ).toEqual("Urlaub");
}));
当我尝试执行此代码时,我收到一条错误消息:
TS2345: Argument of type '{ "ID": number; "Feiertag": string; "Datum": string; }' is not assignable to parameter of type 'Feiertag[]'. Property 'includes' is missing in type '{ "ID": number; "Feiertag": string; "Datum": string; }'.
这是什么问题?