我想测试一个函数的无效输入并期望该函数抛出 在那个输入上。但是测试未通过,但函数仍会引发错误。我是个开玩笑的初学者,所以我不知道为什么会这样。
我的功能如下:
SET DATEFORMAT ymd
SET ARITHABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
SET NUMERIC_ROUNDABORT, IMPLICIT_TRANSACTIONS, XACT_ABORT OFF
GO
SET IDENTITY_INSERT RoomsMS.dbo.Reservation_Details ON
GO
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (1, 1, 37, '2019-02-08 00:00:00.0000000', 1, 1, 0, 0, 2, '2019-08-01', '2019-08-05', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (3, 2, 40, '2019-02-08 00:00:00.0000000', 1, 1, 0, 0, 4, '2019-08-03', '2019-08-03', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (4, 3, 39, '2019-02-12 00:00:00.0000000', 1, 2, 0, 0, 2, '2019-07-01', '2020-01-01', 2)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (24, 1, 40, '2019-08-04 20:29:52.0000000', 1, 1, 0, 0, 3, '2019-08-04', '2019-08-04', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (27, 1, 40, '2019-08-04 20:58:50.0000000', 1, 1, 0, 0, 3, '2019-08-04', '2019-08-24', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (38, 9, 45, '2019-08-15 16:37:18.0000000', 1, 3, 2, 2, 3, '2019-08-10', '2019-08-16', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (39, 10, 45, '2019-08-15 17:46:50.0000000', 1, 2, 0, 0, 3, '2019-08-15', '2019-08-19', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (40, 9, 46, '2019-08-15 18:15:12.0000000', 1, 1, 2, 0, 4, '2019-08-15', '2019-08-25', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (41, 10, 48, '2019-08-15 18:34:12.0000000', 1, 1, 0, 0, 3, '2019-08-10', '2019-08-23', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (42, 10, 38, '2019-08-15 18:34:12.0000000', 1, 1, 0, 0, 2, '2019-08-15', '2019-08-23', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (43, 10, 40, '2019-08-15 18:34:12.0000000', 1, 1, 0, 0, 2, '2019-08-15', '2019-08-23', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (44, 9, 40, '2019-08-16 10:17:34.0000000', 1, 2, 0, 0, 4, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (45, 9, 45, '2019-08-16 11:24:44.0000000', 1, 3, 2, 2, 3, '2019-08-10', '2019-08-20', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (46, 9, 50, '2019-08-17 10:51:15.0000000', 1, 2, 0, 0, 5, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (47, 9, 51, '2019-08-17 11:25:01.0000000', 1, 2, 0, 0, 2, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (48, 9, 38, '2019-08-17 11:25:01.0000000', 1, 2, 0, 0, 5, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (49, 9, 41, '2019-08-17 11:19:01.0000000', 1, 2, 0, 0, 3, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (50, 9, 67, '2019-08-17 13:14:06.0000000', 1, 2, 0, 0, 2, '2019-08-10', '2019-08-31', 1)
INSERT RoomsMS.dbo.Reservation_Details(Id, Reservation_Id, roomNoid, Trans_Date, statusid, adultNo, childNo, infantNo, BoardType_Id, checkInDate, checkOutDate, Userid) VALUES (51, 9, 42, '2019-08-17 20:07:34.0000000', 1, 1, 0, 0, 5, '2019-08-17', '2019-08-18', 1)
GO
SET IDENTITY_INSERT RoomsMS.dbo.Reservation_Details OFF
GO
Test.js:
export class MyClass{
static theFunction(tokens){
let result = [];
if (typeof tokens[0] === "string") {
return tokens;
} else {
try {
for(let token of tokens){
result.push(token.text);
}
return result;
} catch (e) {
throw new Error(e); //also tried throw e; and no try/catch aswell
}
}
}
}}
我想念什么?
答案 0 :(得分:1)
将函数包装到匿名函数中,以便Jest可以捕获错误:
describe('Test the MyClass:', () => {
test('invalid inputs for thefunction()', () => {
expect( () => { MyClass.theFunction(0) } ).toThrow();
});
});
您可能想阅读有关toThrow() in the Jest documentation的部分,并在Github上查看implementation in the Jest project。