gpt4 book ai didi

sequelize.js - 工厂女孩设置了 Sequelize 适配器

转载 作者:行者123 更新时间:2023-12-03 22:18:47 25 4
gpt4 key购买 nike

我在做什么

我正在使用带有 Sequelize 适配器的工厂女孩​​进行摩卡测试。我知道我的设置有问题,但无法解决问题。

这是我的代码:

模态:

"use strict";

module.exports = (sequelize, DataTypes) => {
const Region = sequelize.define("Region", {
name: {
type: DataTypes.STRING,
allowNull: false
},
open: {
type: DataTypes.BOOLEAN,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: true
}
});
Region.associate = models => {
Region.hasMany(models.Area, {
foreignKey: "regionId",
as: "areas"
});
};
return Region;
};

工厂:
var factory = require("factory-girl");
const adapter = new factory.SequelizeAdapter();
factory = factory.factory;
factory.setAdapter(adapter);

const Region = require("../../src/models/region");

factory.define("region", Region, () => {
return {
name: factory.chance("first"),
open: "true",
description: factory.chance("sentence", { words: 5 })
};
});

工厂的索引文件:
import factory from "factory-girl";

import "./regions";
import "./areas";

global.factory = factory;

export default factory;

我的摩卡测试文件:
import chai from "chai";
import assert from "assert";
import app from "../../src/app";
import chaiHttp from "chai-http";
import factories from "../factories";
// import dotenv from "dotenv";
// dotenv.config({path: ".env.test"});
chai.use(chaiHttp);

describe("'regions'service", () => {
it("should POST area", async () => {
const region = await factories.create("region");
const dummy = {
name: "New area",
open: true,
description: "This is a cool area!",
gps: "122.123, 123.4123",
regionId: region.dataValues.id
};
const res = await chai
.request(app)
.post("/api/areas")
.send(dummy);
console.log(res.body);
assert.equal(res.body.name, dummy.name);
});
});

** 我得到什么错误 **

TypeError: Model.build is not a function



所以我知道这与 Model 没有作为构造函数返回有关。但是如何让它返回一个构造函数呢?

如果我在工厂中更改 require 方法如下:
var factory = require("factory-girl");
const adapter = new factory.SequelizeAdapter();
factory = factory.factory;
factory.setAdapter(adapter);
const sequelize = require("sequelize");
const DataTypes = sequelize.DataTypes;

const Region = require("../../src/models/region")(sequelize, DataTypes);

factory.define("region", Region, () => {
return {
name: factory.chance("first"),
open: "true",
description: factory.chance("sentence", { words: 5 })
};
});

然后我得到错误 factory.define is not a function。

最佳答案

我希望这更清楚……但是当您对工厂进行 define 时,需要传入一个 FUNCTION CONSTRUCTOR。

const Region = require("../../src/models").Region;

如果您定义上面列出的模型,这将是一种方法。但关键是我需要传入模型的构造函数。

关于sequelize.js - 工厂女孩设置了 Sequelize 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47255125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com