Getting Started
Update Your NPM
If you're using Node, you'll want to install using npm:
npm install steam-factory --save-dev
You will need to instanciate the factory before using it.
var Factory = ;
Using Without NPM
If you're not using npm, be sure to load Steam Factory into your enviroment.
Once Required, assuming you have a directory structure of spec/factories
or test/factories
, all you'll need load your factories definitions
var Factory = ; Factory; Factory;
Defining factories
Each factory has a name and a set of attributes, functions and other factories that will define the state of the returned object
Factory; Factory;
It is highly recommended that you have one factory for each entity that provides the simplest set of attributes necessary to create an instance of that entity. I.e: If you're creating model objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
Attempting to define multiple factories with the same name will raise an error.
Factories can be defined anywhere
Using factories
SteamFactory supports several different build strategies: build, create, and attributesFor.
// Returns a User instance that's not saveduser = SteamFactory // Returns a hash of attributes that can be used to build a User instanceattrs = SteamFactory // Passing a function to any of the methods above will yield the return objectSteamFactory
it's possible to override the defined attributes by passing an object, or an array of objects, functions and factories:
// Build a User instance and override the firstName propertyuser = SteamFactoryuserFirstName// => "John"
;
Lazy Attributes
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These "lazy" attributes can be added by passing a block instead of a parameter:
User{ /* Activation code*/ }; Factory; ;; Factory;
Aliases
Aliases allow you to use named associations more easily.
Factory; Factory; Factory; ;
Dependent Attributes
Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
Associations
It's possible to set up associations within factories.
Factory;
Generating data for a has_many
relationship is a bit more involved,
depending on the amount of flexibility desired, but here's a surefire example
of generating associated data.
// post factory with a `belongs_to` association for the user Factory;Factory; Factory
This allows us to do:
SteamFactorypostslength; // 0SteamFactorypostslength; // 5
Extending Factories
You can easily create multiple factories for the same class without repeating common attributes by nesting factories:
Factory;Factory; approvedPost = SteamFactory;ApprovedPosttitle; // => "A title"approvedPostapproved; // => true
As mentioned above, it's good practice to define a basic factory for each class with only the attributes required to create it. Then, create more specific factories that extend from this basic one. Factory definitions are still code, so keep them DRY.
Sequences
Unique values in a specific format (for example, e-mail addresses) can be generated using sequences. Sequences are defined by calling sequence in a definition block, and values in a sequence are generated by calling SteamFactory.generate:
// Defines a new sequenceSteamFactory.define("email", function(n){ return "person" + n + "@example.com"}); SteamFactory.Generate("email");// => "person1@example.com" SteamFactory.generate("email");// => "person2@example.com"
Sequences can be used for attributes:
Factory;
You can also override the initial value:
SteamFactory; SteamFactory;// => "person100@example.com"
Composition
Composition allow you to group attributes together and then apply them to any factory.
Factory;Factory;Factory;Factory; Factory; Factory; // Define factories by reusing other factoriesFactory;Factory; Factory;Factory;
Composition that define the same attributes won't raise AttributeDefinitionErrors;