简体   繁体   English

NPM Factory-Bot/Girl 如何导出工厂定义以用于我的 NodeJS 规范?

[英]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.

相关问题 是否可以在茶匙中使用factory_girl.js? - Is it possible to use factory_girl.js with teaspoon? 节点JS | 开玩笑 | 工厂女孩 | Faker:Faker 生成相同的值 - NodeJS | Jest | Factory Girl | Faker: Faker generating the same values 在创建另一个工厂女孩实例的内部创建工厂女孩实例 - create factory girl instance inside of creating another factory girl instance 在测试中使用工厂女孩后如何重置猫鼬? - how to reset mongoose after using factory girl in test? 带伪造者的工厂女孩​​在节点快递中生成相同的数据 - factory girl with faker generate same data in node express 如何在MS Bot对话框(NodeJS)中使用npm的readline-Sync输入屏蔽功能? - How to use npm's readline-Sync input masking function in MS Bot Dialog(NodeJS)? NodeJS中如何导入、导出、合并多个GraphQL类型定义 - How to import, export, and merge several GraphQL type definition in NodeJS 如何修复我的 nodejs 上的 npm 启动错误? - How to fix npm start errors on my nodejs? nodejs 永远:如何运行我的 npm 应用程序 - nodejs forever : How to run my npm application 如何在Node.js中使用multer-imager npm模块并将调整大小后的图像上传到Amazon S3存储桶? - How to use multer-imager npm module in nodejs and upload my image resized to amazon s3 bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM