嘲笑sqlite3数据库中唯一值的测试单元

字段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");
      }
    });

但是它不起作用,因为我的测试通过了(因为阵列中只有一辆汽车,所以它不应该通过)。任何想法如何去做?谢谢

hjjhjz 回答:嘲笑sqlite3数据库中唯一值的测试单元

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3101601.html

大家都在问