question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add possibility to extend factory

See original GitHub issue

For example I have a Subscription model

factory.define('Subscription', Subscription, {
  id: factory.seq('id'),
  price: 500,
  name: 'Pay for 1 month'
})

And want to define factory for ExpiredSubscription which extends Subsription.

The same feature is implemented in Ruby’s factory_girl using nested factories.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Sep 6, 2017

Meanwhile the only way to achieve this is to create a custom adapter. All good except that this adapter relies a bit on implementation details:

const { factory, ObjectAdapter } = require('factory-girl');

class FactoryAdapter {
  constructor(adapter) {
    this.defaultAdapter = adapter
  }

  isFactory(Model) {
    return Model.constructor.name === 'Factory' || !!Model.initializer
  }

  build(Model, props) {
    return this.isFactory(Model)
      ? Model.build(this.defaultAdapter, props)
      : this.defaultAdapter.build(Model, props);
  }

  save(...args) {
    return this.defaultAdapter.save(...args);
  }

  destroy(...args) {
    return this.defaultAdapter.destroy(...args);
  }

  get(...args) {
    return this.defaultAdapter.get(...args);
  }

  set(...args) {
    return this.defaultAdapter.set(...args);
  }
}

factory.setAdapter(new FactoryAdapter(new ObjectAdapter()));

factory.define('User', Object, {
  id: factory.seq('id'),
  name: factory.chance('name')
});

factory.define('Admin', factory.getFactory('User'), {
  is_admin: true
});

factory.build('Admin')
  .then(console.log)
  .catch(console.error)
0reactions
stalniycommented, Nov 6, 2017

Great! Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add possibility to extend factory · Issue #109 - GitHub
For example I have a Subscription model factory.define('Subscription', Subscription, { id: factory.seq('id'), price: 500, name: 'Pay for 1 ...
Read more >
What's the best way to extend the functionality of factory ...
I've now looked over the source, and I think the best way to add a new class is to edit the module directly;...
Read more >
Extend factory calendar to 2021 - SAP Community
So I go to the main menu, enter "SCAL" and go to the factory calendar, and I find that all calendars are set...
Read more >
Extended Car Warranties: When and How to Say No
If you keep or lease your car for less than the length of your factory coverage, you do not — repeat, do not...
Read more >
Is It Possible to Buy an Extended Warranty After ... - Top Speed
Don't know what to do when your car's factory warranty runs out? Fear not, here's a guide to extended warranties to help you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found