"builder.apply is not a function" error when user makes subtle mistake
kylezeeuwen opened this issue · comments
Using rosie v1.3.0 with node v6.1.0.
I was just writing up practice docs for a project that uses rosie and I found a scenario where user error could result in a subtle bug.
If I define a factory:
- with a sequence and erroneously pass a default value to it (i.e., .sequence('myid', 1)
- with an attribute that relies on that sequence ID
Then when I invoke factory.build with all defaults it will cause a builder.apply is not a function
error.
I think it could be a bug in the handling of calls to sequence, but in any case I thought I would write it up for posterity:
var Factory = require('rosie').Factory;
var dump = function(o) { console.log(JSON.stringify(o, {}, 2)) }
// //Case 1 - Fail
Factory.define('failingSequenceExample')
.sequence('propertyId', 1) // HERE!!! Look here where I pass 1 to the sequence
.attr('reference', ['propertyId'], function(propertyId) { return `REFPROP${propertyId}` })
.attr('bedroomNr', 1)
try {
dump(Factory.build('failingSequenceExample'));
}
catch (err) {
console.log("This is the apply error, caused because you must explicitly set anything you use as a dependency")
console.log(err.message)
}
// OUTPUT:
// This is the apply error, caused because you must explicitly set anything you use as a dependency
// "builder.apply is not a function"
// //Case 2 - Pass
Factory.define('passingSequenceExample')
.sequence('propertyId') // HERE! There is no default provided, and now it works
.attr('reference', ['propertyId'], function(propertyId) { return `REFPROP${propertyId}` })
.attr('bedroomNr', 1)
dump(Factory.build('passingSequenceExample'));
dump(Factory.build('passingSequenceExample'));
// OUTPUT:
//{
// "propertyId": 1,
// "reference": "REFPROP1",
// "bedroomNr": 1
//}
//{
// "propertyId": 2,
// "reference": "REFPROP2",
// "bedroomNr": 1
//}
really comes down to the lack of docs in the README.md. Looking at the code you have clearly defined the function signature and I wasn't using sequence()
correctly. Might submit a PR if I can find time. Would you accept some doc updates if I submitted them ? I am using rosie on two projects, would be willing to help out if it was accepted.
Would love to have better docs! Please feel free to submit a PR.
Please see PR to add said docs : #49
I will try to make a new PR that makes some of the PEBKAC errors a little more graceful, but that will probably not be for a month or so.
Thanks for the code !
I believe this is pretty much addressed by #49. Thanks!