[英]NPM Factory-Bot/Girl how to export a factory definition for use in my NodeJS specs?
JS newbie here. JS新手在这里。 I am using Jasmine to test a NodeJS application which uses MongoDB and Mongoose, and I would like to replace my static test fixtures with dynamic factories.
我正在使用 Jasmine 测试使用 MongoDB 和 Mongoose 的 NodeJS 应用程序,我想用 4 个工厂动态测试夹具替换我的 ZA81259CEF8E9559C297EDF1。 https://github.com/ratson/factory-bot looks good to me.
https://github.com/ratson/factory-bot对我来说看起来不错。
However, all of the examples are from a single file and don't demonstrate exporting/importing between files, so I don't understand what to modules.exports =
in order to use a factory in my specs.但是,所有示例都来自单个文件,并且没有演示文件之间的导出/导入,所以我不明白
modules.exports =
什么才能在我的规范中使用工厂。
I'm also using ES5 if that matters.如果这很重要,我也在使用 ES5。
My question is: how do I export this definition?我的问题是:我如何导出这个定义?
spec/factories/user.js
const factory = require('factory-bot').factory;
factory.setAdapter(new FactoryBot.MongooseAdapter());
const User = require('../models/user');
factory.define('user', User, {
username: 'Bob',
expired: false
});
factory.extend('user', 'expiredUser', {
expired: true
});
And then how do I use my export so that I can make sampleUsers
?然后我如何使用我的导出来制作
sampleUsers
?
spec/controllers/user.js
const reqs = require("../support/require")
describe("GET /users", () => {
describe("index", () => {
var data = {};
var sampleUsers = factory.createMany('user', 5);
beforeEach((done) => {
reqs.Request.get(/users", (error, response, body) => {
data.status = response.statusCode;
data.body = JSON.parse(body);
done();
});
});
it("returns a 200 response status", () => {
expect(data.status).toBe(200);
});
it("responds with the users collection", async () => {
expect(data.body.users).toBe(sampleUsers);
});
});
});
Thanks in advance for any advice.提前感谢您的任何建议。
You just need to require your factory definitions before using them.您只需要在使用它们之前要求您的工厂定义。
Here's an example of what you could do:这是您可以执行的操作的示例:
spec/factories/user.js
const { factory } = require('factory-bot');
const User = require('../models/user');
factory.setAdapter(new FactoryBot.MongooseAdapter());
factory.define('user', User, {
username: 'Bob',
expired: false
});
factory.extend('user', 'expiredUser', {
expired: true
});
spec/factories/index.js
: spec/factories/index.js
:
const { factory } = require("factory-bot");
// Require factories to use with the exported object
require("./user.js");
module.exports = factory;
spec/controllers/user.js
: spec/controllers/user.js
:
const factory = require("../../factories");
...
const sampleUsers = factory.createMany('user', 5);
The key difference between the example above and your sample code is the index.js
file which requires factory-bot
and all the factory definitions.上面的示例和您的示例代码之间的主要区别在于
index.js
文件,它需要factory-bot
和所有工厂定义。 By requiring the definitions, you will be able to use them.通过要求定义,您将能够使用它们。
If you require('factory-bot')
directly instead of require('spec/factories')
, you will need to require the factory definitions you want to use.如果您直接
require('factory-bot')
而不是require('spec/factories')
,则需要要求要使用的工厂定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.