首页 > 解决方案 > 外键茉莉花测试失败

问题描述

我有 2 个名为usersorders提供模型的表:

export type User = {
    id?: number;
    firstname: string;
    lastname: string;
    password: string;
    role: string;
    email: string;
};


export type Order = {
    id?: number;
    userId: number;
    status: string;
};

我在创建用户时测试失败:

  it('create method should add an order', async () => {
    

    const result = await store.create({
        id: 1,
        userId: 1,
        status: 'open',
    });

    expect(result).toEqual({
        id: 1,
        userId: 1,
        status: 'open',
    });
});

错误堆栈:

1) Order Model create method should add an order
  Message:
    Error: Could not add new order for user ID 1 for Error: error: insert or update on table "orders" violates foreign key constraint "orders_user_id_fkey"

显然,问题是数据库中不存在下订单的用户。因此,作为一种解决方案,我尝试在此测试中创建一个用户,但这会破坏 users_spec.ts 文件中的顺序测试。

如何正确编写此测试?

标签: node.jstypescriptjasmine

解决方案


推荐阅读