字段make
设置为不接受数据库中的重复项。我想编写一个笑话测试单元,以验证具有相同值的字段无法发布。
我第一次尝试
it("should not post car with an existing make",async () => {
await Cars.insert({ make: "toyota" });
await Cars.insert({ make: "toyota" });
const cars = await db("cars");
expect(cars).toHaveLength(1);
});
但是sqlite3约束的唯一错误会制动单元测试。然后我尝试了方法来处理错误
it("should not post car with an existing make",async () => {
try {
await Cars.insert({ make: "toyota" });
await Cars.insert({ make: "toyota" });
const cars = await db("cars");
expect(cars).toHaveLength(2);
} catch (error) {
expect(error.code).toBe("SQLITE_CONSTRAINT");
}
});
但是它不起作用,因为我的测试通过了(因为阵列中只有一辆汽车,所以它不应该通过)。任何想法如何去做?谢谢