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.

asyncPopulate and custom object types

See original GitHub issue

Description: I my application I use js-money package which allows to manage money easily. So, when I try to create a factory and Money object as factory attribute it returns me a plain object instead of Money

Steps

  1. Define a factory
  2. Invoke factory.build and pass in balance property Money instance
  3. Output result.balance.constructor

Actual result Shows Object and all Money methods are lost

Expected result: Shows Money

Reproduction code:

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

factory.setAdapter(new ObjectAdapter());

factory.define('Account', Object, {
  name: factory.seq('name'),
  balance: new Money(10, Money.USD)
});

async function run() {
  const account = await factory.build('Account');
  console.log(account.balance.constructor);
}

run();

Workaround:

The workaround is to use a function for custom object types:

factory.define('Account', Object, {
  name: factory.seq('name'),
  balance: () => new Money(10, Money.USD)
});

But this increases boilerplate and may become an issue when you want to pass result of one factory as properties into another factory.

Possible solution:

  1. Copy only plain objects, constructor of which is Object
  2. Allow to pass an option/hook in FactoryGirl class that makes a decision which objects may be copied

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
simonexmachinacommented, Sep 12, 2017

I’ve pushed v5.0.0-beta.1, which doesn’t traverse into objects unless they’re instances of Object, so Dates and other types are preserved. Please give this version a go and let me know if this causes any other issues. It’s likely to cause some edge cases for a few people, but a few people have encountered issues with this, and I think the new behaviour is less unexpected.

0reactions
oliviertassinaricommented, Oct 20, 2017

I have been upgrading our test suite to v5.0.0-beta.1 in order to fix the date issue. It’s working great 👍.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understand Custom & Standard Objects - Trailhead - Salesforce
You can customize standard objects by adding custom fields, and you can add custom fields to your custom objects. Every field has a...
Read more >
Custom Objects Events - Zendesk Developer Docs
Custom objects events track the user actions executed on object records and object types. The following actions trigger an event:.
Read more >
Understanding Salesforce Standard and Custom Objects
Salesforce supports several different types of objects. There are standard objects, custom objects, external objects, platform events, ...
Read more >
Custom object field types - Oracle Help Center
Custom object field types. Custom objects have one or more fields that store data associated with a custom object record. There is a...
Read more >
Creating Salesforce Objects and Tabs - Standard and Custom
These objects can be standard objects or custom objects. You can define different types of relationships by creating custom relationship fields ...
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