Skip to content

Commit fedaf39

Browse files
michaelgwelchratson
authored andcommitted
docs: add a few notes about model objects
1 parent 8d8e886 commit fedaf39

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ factory.build('user').then(user => {
3131
});
3232
```
3333

34+
## Defining Models
35+
36+
Define models that have a constructor that takes an object with the attributes needed to
37+
instantiate an instance of the model.
38+
39+
For example:
40+
41+
```javascript
42+
class User {
43+
constructor(attrs = {}) {
44+
this.attrs = Object.assign({
45+
username: attrs.username || 'George',
46+
score: attrs.score || 27,
47+
}, attrs);
48+
}
49+
}
50+
```
51+
52+
The factory methods will invoke this constructor during the construction of model objects.
53+
3454
## Defining Factories
3555

3656
Define factories using the `factory.define()` method.

docs/tutorial.md

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ This tutorial introduces the capabilities of `factory-girl`. We'll start with a
44
factory for a hypothetical `User` model and gradually add to it. This tutorial may not
55
cover all aspects of `factory-girl`, but should serve as a good starting point.
66

7+
### The `User` Model
8+
9+
For these examples we'll assume we have a model like the following with a constructor
10+
that takes one object with the attributes necessary to initialize the model class.
11+
12+
```javascript
13+
class User {
14+
constructor(attrs = {}) {
15+
this.attrs = Object.assign({
16+
email: attrs.email || '[email protected]',
17+
password: attrs.password || 'secure-password',
18+
}, attrs);
19+
}
20+
}
21+
```
22+
23+
The factory methods will invoke this constructor during the construction of model objects.
24+
725
### The `User` Factory
826

927
Let's start with a simple `User` factory, as we go on, we'll keep on modifying

0 commit comments

Comments
 (0)