challah 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +9 -2
- data/README.md +18 -18
- data/lib/challah/test.rb +22 -5
- data/lib/challah/version.rb +1 -1
- data/test/audit_test.rb +32 -32
- data/test/controller_test.rb +9 -9
- data/test/cookie_store_test.rb +25 -25
- data/test/factories.rb +7 -7
- data/test/helper.rb +14 -12
- data/test/permission_test.rb +14 -14
- data/test/restrictions_controller_test.rb +29 -30
- data/test/role_test.rb +26 -26
- data/test/session_test.rb +65 -65
- data/test/sessions_controller_test.rb +13 -13
- data/test/simple_cookie_store_test.rb +25 -25
- data/test/user_test.rb +79 -79
- data/vendor/bundle/cache/factory_girl-3.0.0.gem +0 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/Appraisals +11 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/CONTRIBUTION_GUIDELINES.md +10 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/Changelog +168 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/GETTING_STARTED.md +735 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile +5 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile.lock +95 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/LICENSE +19 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/README.md +70 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/Rakefile +38 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/cucumber.yml +1 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/factory_girl.gemspec +38 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/factory_girl_steps.feature +237 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/find_definitions.feature +75 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/database_steps.rb +42 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/factory_girl_steps.rb +43 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/support/env.rb +11 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/features/support/factories.rb +119 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/2.3.gemfile +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile.lock +86 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile.lock +95 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile.lock +93 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl.rb +69 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/aliases.rb +19 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute.rb +40 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/association.rb +23 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/dynamic.rb +20 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/sequence.rb +17 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/static.rb +15 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_assigner.rb +73 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb +54 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/callback.rb +33 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration.rb +22 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/association.rb +25 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/dynamic.rb +25 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/implicit.rb +32 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/static.rb +25 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration_list.rb +48 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition.rb +76 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition_proxy.rb +159 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/errors.rb +19 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator.rb +76 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator_class_definer.rb +34 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory.rb +145 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory_runner.rb +24 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/find_definitions.rb +25 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_factory.rb +15 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_object.rb +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/registry.rb +46 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/reload.rb +8 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/sequence.rb +24 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/step_definitions.rb +130 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy.rb +32 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/attributes_for.rb +12 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/build.rb +15 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/create.rb +17 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/stub.rb +61 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy_calculator.rb +29 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax.rb +16 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/default.rb +56 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/generate.rb +70 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/make.rb +45 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/methods.rb +136 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/sham.rb +45 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/vintage.rb +130 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/trait.rb +29 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/version.rb +4 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/aliases_spec.rb +19 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_aliases_spec.rb +45 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_existing_on_object_spec.rb +68 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_for_spec.rb +89 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_from_instance_spec.rb +53 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_ordered_spec.rb +51 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_list_spec.rb +41 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_spec.rb +89 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_stubbed_spec.rb +104 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/callbacks_spec.rb +47 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_list_spec.rb +82 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_spec.rb +117 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/define_child_before_parent_spec.rb +21 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_spec.rb +26 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_without_block_spec.rb +15 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/initialize_with_spec.rb +147 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_factories_spec.rb +184 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_inherited_spec.rb +52 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/nested_attributes_spec.rb +32 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/overrides_spec.rb +61 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/parent_spec.rb +90 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/sequence_spec.rb +33 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/stub_spec.rb +62 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/blueprint_spec.rb +34 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/generate_spec.rb +59 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/make_spec.rb +52 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/sham_spec.rb +43 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/vintage_spec.rb +217 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/traits_spec.rb +421 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/transient_attributes_spec.rb +124 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/aliases_spec.rb +31 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/association_spec.rb +28 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/dynamic_spec.rb +52 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/sequence_spec.rb +16 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/static_spec.rb +19 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_list_spec.rb +78 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_spec.rb +16 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/callback_spec.rb +41 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration/implicit_spec.rb +25 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration_list_spec.rb +71 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_proxy_spec.rb +197 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_spec.rb +104 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/evaluator_class_definer_spec.rb +54 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/factory_spec.rb +279 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/find_definitions_spec.rb +110 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_factory_spec.rb +13 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_object_spec.rb +8 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/registry_spec.rb +81 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/sequence_spec.rb +47 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/attributes_for_spec.rb +18 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/build_spec.rb +7 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/create_spec.rb +13 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/stub_spec.rb +40 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_calculator_spec.rb +33 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_spec.rb +21 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl_spec.rb +22 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/spec_helper.rb +26 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/macros/define_constant.rb +86 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/callback.rb +9 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/declaration.rb +71 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/delegate.rb +44 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/trait.rb +9 -0
- data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/shared_examples/strategy.rb +104 -0
- data/vendor/bundle/specifications/factory_girl-3.0.0.gemspec +62 -0
- metadata +147 -12
Binary file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Contributing to factory\_girl:
|
2
|
+
|
3
|
+
1. Fork the [official repository](https://github.com/thoughtbot/factory_girl/tree/master).
|
4
|
+
2. Make your changes in a topic branch.
|
5
|
+
3. Send a pull request.
|
6
|
+
|
7
|
+
Notes:
|
8
|
+
|
9
|
+
* Contributions without tests won't be accepted.
|
10
|
+
* Please don't update the Gem version.
|
@@ -0,0 +1,168 @@
|
|
1
|
+
3.0.0 (March 23, 2012)
|
2
|
+
Deprecate the vintage syntax
|
3
|
+
Remove Rails 2.x support
|
4
|
+
Remove Ruby 1.8 support
|
5
|
+
Remove deprecated features, including default_strategy, factory_name,
|
6
|
+
:method for defining default strategy, ignore on individual attributes, and
|
7
|
+
interacting with Factory the way you would FactoryGirl
|
8
|
+
|
9
|
+
2.6.4 (March 16, 2012)
|
10
|
+
Do not ignore names of transient attributes
|
11
|
+
Ensure attributes set on instance are calculated uniquely
|
12
|
+
|
13
|
+
2.6.3 (March 9, 2012)
|
14
|
+
Fix issue with traits not being present the first time a factory is accessed
|
15
|
+
Update available Cucumber step definitions to not require a trialing colon
|
16
|
+
when building a table of attributes to instantiate records with
|
17
|
+
|
18
|
+
2.6.2 (March 9, 2012)
|
19
|
+
Allow factories to use all their ancestors' traits
|
20
|
+
Ignore bin dir generated by bundler
|
21
|
+
Namespace ::Factory as top-level to fix vintage syntax issue with
|
22
|
+
Ruby 1.9.2-p3p18
|
23
|
+
|
24
|
+
2.6.1 (March 2, 2012)
|
25
|
+
Use FactoryGirl.reload in specs
|
26
|
+
Clean up running named factories with a particular strategy with
|
27
|
+
FactoryGirl::FactoryRunner
|
28
|
+
|
29
|
+
2.6.0 (February 17, 2012)
|
30
|
+
Improve documentation of has_many associations in the GETTING_STARTED
|
31
|
+
document
|
32
|
+
Deprecate :method in favor of :strategy when overriding an association's
|
33
|
+
build strategy
|
34
|
+
|
35
|
+
2.5.2 (February 10, 2012)
|
36
|
+
Fix step definitions to use associations defined in parent factories
|
37
|
+
Add inline trait support to (build|create)_list
|
38
|
+
Update ActiveSupport dependency to >= 2.3.9, which introduced
|
39
|
+
class_attribute
|
40
|
+
|
41
|
+
2.5.1 (February 3, 2012)
|
42
|
+
Fix attribute evaluation when the attribute isn't defined in the factory but
|
43
|
+
is a private method on Object
|
44
|
+
Update rubygems on Travis before running tests
|
45
|
+
Fix spec name
|
46
|
+
Update GETTING_STARTED with correct usage of build_stubbed
|
47
|
+
Update README with more info on initialize_with
|
48
|
+
Honor :parent on factory over block nesting
|
49
|
+
|
50
|
+
2.5.0 (January 20, 2012)
|
51
|
+
Revert 'Deprecate build_stubbed and attributes_for'
|
52
|
+
Implement initialize_with to allow overriding object instantiation
|
53
|
+
Ensure FG runs against Rails 3.2.0
|
54
|
+
|
55
|
+
2.4.2 (January 18, 2012)
|
56
|
+
Fix inline traits' interaction with defaults on the factory
|
57
|
+
|
58
|
+
2.4.1 (January 17, 2012)
|
59
|
+
Deprecate build_stubbed and attributes_for
|
60
|
+
Fix inline traits
|
61
|
+
|
62
|
+
2.4.0 (January 13, 2012)
|
63
|
+
Refactor internals of Factory Girl to use anonymous class on which attributes
|
64
|
+
get defined
|
65
|
+
Explicitly require Ruby 1.8.7 or higher in gemspec
|
66
|
+
Fix documentation
|
67
|
+
Add Gemnasium status to documentation
|
68
|
+
Supplying a Class to a factory that overrides to_s no longer results in
|
69
|
+
getting the wrong Class constructed
|
70
|
+
Be more agnostic about ORMs when using columns in Factory Girl step
|
71
|
+
definitions
|
72
|
+
Test against Active Record 3.2.0.rc2
|
73
|
+
Update GETTING_STARTED to use Ruby syntax highlighting
|
74
|
+
|
75
|
+
2.3.2 (November 26, 2011)
|
76
|
+
Move logic of where instance.save! is set to Definition
|
77
|
+
Fix method name from aliases_for? to alias_for?
|
78
|
+
Refactor internal attribute handling to use an anonymous class instead of
|
79
|
+
faking Ruby's variable resolution. This allows for more sane usage of
|
80
|
+
attributes without having to manage sorting priority because attributes
|
81
|
+
can turn themselves into procs, which are used with define_method on a
|
82
|
+
class so attributes work correctly all the time.
|
83
|
+
|
84
|
+
2.3.1 (November 23, 2011)
|
85
|
+
Remove internally-used associate method from all the FactoryGirl::Proxy subclasses
|
86
|
+
Move around requiring of files
|
87
|
+
Consolidate errors into factory_girl.rb
|
88
|
+
Refactor AttributeList to deal with priority only when iterating over
|
89
|
+
attributes
|
90
|
+
Refactor internals of some of the Proxy subclasses
|
91
|
+
Ensure callbacks on traits are executed in the correct order
|
92
|
+
|
93
|
+
2.3.0 (November 18, 2011)
|
94
|
+
Registries are named, resulting in better messages when factories, traits,
|
95
|
+
or sequences cannot be found
|
96
|
+
Fix incorrect tests
|
97
|
+
Internals refactoring introducing FactoryGirl::NullFactory,
|
98
|
+
FactoryGirl::Definition, and FactoryGirl::DeclarationList
|
99
|
+
Use ActiveSupport for Hash#except and its delegation capabilities
|
100
|
+
Fix usage of callbacks when added via implicit traits
|
101
|
+
Use Bundler tasks and clean up dependencies
|
102
|
+
Fix failing spec for big letters in factory name passed as symbol
|
103
|
+
Add ability for traits to be added dynamically when creating an instance via
|
104
|
+
build, create, build_stubbed, or attributes_for
|
105
|
+
|
106
|
+
2.2.0 (October 14, 2011)
|
107
|
+
Clean up RSpec suite to not use 'should'
|
108
|
+
Use create_list in step definitions
|
109
|
+
Syntax methods that deal with ORM interaction (attributes_for, build, build_stubbed,
|
110
|
+
and create) now accept a block that yields the result. This results in a
|
111
|
+
more convenient way to interact with the result than using Object.tap.
|
112
|
+
Standardize deprecation warnings
|
113
|
+
Update transient attribute syntax to use blocks instead of calling ignore on
|
114
|
+
each attribute declaration
|
115
|
+
Parents can be defined after children because factories are evaluated when
|
116
|
+
they're used; this means breaking up factories across multiple files will
|
117
|
+
behave as expected
|
118
|
+
Large internal refactoring, including changing access modifiers for a
|
119
|
+
handful of methods for a more clearly defined API
|
120
|
+
|
121
|
+
2.1.2 (September 23, 2011)
|
122
|
+
Bugfix: Vintage syntax fixed after bug introduced in 2.1.1
|
123
|
+
Introduce dependency on activesupport to remove code from Factory class
|
124
|
+
|
125
|
+
2.1.1 (September 23, 2011) (yanked)
|
126
|
+
Bugfix: Parent object callbacks are run before child object callbacks
|
127
|
+
Declarations: allow overriding/modification of individual traits in child factories
|
128
|
+
Callbacks refactored to not be attributes
|
129
|
+
Updating documentation for formatting and clarity (incl. new specificity for cucumber)
|
130
|
+
|
131
|
+
2.1.0 (September 02, 2011)
|
132
|
+
Bugfix: created_at now defined for stubbed models
|
133
|
+
Gemspec updated for use with Rails 3.1
|
134
|
+
Factories can now be modified post-definition (useful for overriding defaults from gems/plugins)
|
135
|
+
All factories can now be reloaded with Factory.reload
|
136
|
+
Add :method => build to factory associations to prevent saving of associated objects
|
137
|
+
Factories defined in {Rails.root}/factories are now loaded by default
|
138
|
+
Various documentation updates
|
139
|
+
|
140
|
+
1.1.4 (November 28, 2008)
|
141
|
+
Factory.build now uses Factory.create for associations of the built object
|
142
|
+
Factory definitions are now detected in subdirectories, such as
|
143
|
+
factories/person_factory.rb (thanks to Josh Nichols)
|
144
|
+
Factory definitions are now loaded after the environment in a Rails project
|
145
|
+
(fixes some issues with dependencies being loaded too early) (thanks to
|
146
|
+
Josh Nichols)
|
147
|
+
Factory names ending in 's' no longer cause problems (thanks to Alex Sharp
|
148
|
+
and Josh Owens)
|
149
|
+
|
150
|
+
1.1.3 (September 12, 2008)
|
151
|
+
Automatically pull in definitions from factories.rb, test/factories.rb, or
|
152
|
+
spec/factories.rb
|
153
|
+
1.1.2 (July 30, 2008)
|
154
|
+
Improved error handling for invalid and undefined factories/attributes
|
155
|
+
Improved handling of strings vs symbols vs classes
|
156
|
+
Added a prettier syntax for handling associations
|
157
|
+
Updated documentation and fixed compatibility with Rails 2.1
|
158
|
+
|
159
|
+
1.1.1 (June 23, 2008)
|
160
|
+
The attribute "name" no longer requires using #add_attribute
|
161
|
+
|
162
|
+
1.1.0 (June 03, 2008)
|
163
|
+
Added support for dependent attributes
|
164
|
+
Fixed the attributes_for build strategy to not build associations
|
165
|
+
Added support for sequences
|
166
|
+
|
167
|
+
1.0.0 (May 31, 2008)
|
168
|
+
First version
|
@@ -0,0 +1,735 @@
|
|
1
|
+
Getting Started
|
2
|
+
===============
|
3
|
+
|
4
|
+
Update Your Gemfile
|
5
|
+
-------------------
|
6
|
+
|
7
|
+
If you're using Rails, you'll need to change the required version of `factory_girl_rails`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "factory_girl_rails", "~> 2.0"
|
11
|
+
```
|
12
|
+
|
13
|
+
If you're *not* using Rails, you'll just have to change the required version of `factory_girl`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "factory_girl", "~> 3.0"
|
17
|
+
```
|
18
|
+
|
19
|
+
Once your Gemfile is updated, you'll want to update your bundle.
|
20
|
+
|
21
|
+
Defining factories
|
22
|
+
------------------
|
23
|
+
|
24
|
+
Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it's possible to explicitly specify it:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# This will guess the User class
|
28
|
+
FactoryGirl.define do
|
29
|
+
factory :user do
|
30
|
+
first_name 'John'
|
31
|
+
last_name 'Doe'
|
32
|
+
admin false
|
33
|
+
end
|
34
|
+
|
35
|
+
# This will use the User class (Admin would have been guessed)
|
36
|
+
factory :admin, :class => User do
|
37
|
+
first_name 'Admin'
|
38
|
+
last_name 'User'
|
39
|
+
admin true
|
40
|
+
end
|
41
|
+
|
42
|
+
# The same, but using a string instead of class constant
|
43
|
+
factory :admin, :class => 'user' do
|
44
|
+
first_name 'Admin'
|
45
|
+
last_name 'User'
|
46
|
+
admin true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord 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.
|
52
|
+
|
53
|
+
Attempting to define multiple factories with the same name will raise an error.
|
54
|
+
|
55
|
+
Factories can be defined anywhere, but will be automatically loaded if they
|
56
|
+
are defined in files at the following locations:
|
57
|
+
|
58
|
+
test/factories.rb
|
59
|
+
spec/factories.rb
|
60
|
+
test/factories/*.rb
|
61
|
+
spec/factories/*.rb
|
62
|
+
|
63
|
+
Using factories
|
64
|
+
---------------
|
65
|
+
|
66
|
+
factory\_girl supports several different build strategies: build, create, attributes\_for and stub:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# Returns a User instance that's not saved
|
70
|
+
user = FactoryGirl.build(:user)
|
71
|
+
|
72
|
+
# Returns a saved User instance
|
73
|
+
user = FactoryGirl.create(:user)
|
74
|
+
|
75
|
+
# Returns a hash of attributes that can be used to build a User instance
|
76
|
+
attrs = FactoryGirl.attributes_for(:user)
|
77
|
+
|
78
|
+
# Returns an object with all defined attributes stubbed out
|
79
|
+
stub = FactoryGirl.build_stubbed(:user)
|
80
|
+
|
81
|
+
# Passing a block to any of the methods above will yield the return object
|
82
|
+
FactoryGirl.create(:user) do |user|
|
83
|
+
user.posts.create(attributes_for(:post))
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Build a User instance and override the first_name property
|
91
|
+
user = FactoryGirl.build(:user, :first_name => 'Joe')
|
92
|
+
user.first_name
|
93
|
+
# => "Joe"
|
94
|
+
```
|
95
|
+
|
96
|
+
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# rspec
|
100
|
+
RSpec.configure do |config|
|
101
|
+
config.include FactoryGirl::Syntax::Methods
|
102
|
+
end
|
103
|
+
|
104
|
+
# Test::Unit
|
105
|
+
class Test::Unit::TestCase
|
106
|
+
include Factory::Syntax::Methods
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
This would allow you to write:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
describe User, "#full_name" do
|
114
|
+
subject { create(:user, :first_name => "John", :last_name => "Doe") }
|
115
|
+
|
116
|
+
its(:full_name) { should == "John Doe" }
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
Lazy Attributes
|
121
|
+
---------------
|
122
|
+
|
123
|
+
Most factory attributes can be added using static values that are evaluated when
|
124
|
+
the factory is defined, but some attributes (such as associations and other
|
125
|
+
attributes that must be dynamically generated) will need values assigned each
|
126
|
+
time an instance is generated. These "lazy" attributes can be added by passing a
|
127
|
+
block instead of a parameter:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
factory :user do
|
131
|
+
# ...
|
132
|
+
activation_code { User.generate_activation_code }
|
133
|
+
date_of_birth { 21.years.ago }
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
Aliases
|
138
|
+
-------
|
139
|
+
|
140
|
+
Aliases allow you to use named associations more easily.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
factory :user, :aliases => [:author, :commenter] do
|
144
|
+
first_name "John"
|
145
|
+
last_name "Doe"
|
146
|
+
date_of_birth { 18.years.ago }
|
147
|
+
end
|
148
|
+
|
149
|
+
factory :post do
|
150
|
+
author
|
151
|
+
# instead of
|
152
|
+
# association :author, :factory => :user
|
153
|
+
title "How to read a book effectively"
|
154
|
+
body "There are five steps involved."
|
155
|
+
end
|
156
|
+
|
157
|
+
factory :comment do
|
158
|
+
commenter
|
159
|
+
# instead of
|
160
|
+
# association :commenter, :factory => :user
|
161
|
+
body "Great article!"
|
162
|
+
end
|
163
|
+
```
|
164
|
+
|
165
|
+
Dependent Attributes
|
166
|
+
--------------------
|
167
|
+
|
168
|
+
Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
factory :user do
|
172
|
+
first_name 'Joe'
|
173
|
+
last_name 'Blow'
|
174
|
+
email { "#{first_name}.#{last_name}@example.com".downcase }
|
175
|
+
end
|
176
|
+
|
177
|
+
FactoryGirl.create(:user, :last_name => 'Doe').email
|
178
|
+
# => "joe.doe@example.com"
|
179
|
+
```
|
180
|
+
|
181
|
+
Transient Attributes
|
182
|
+
--------------------
|
183
|
+
|
184
|
+
There may be times where your code can be DRYed up by passing in transient attributes to factories.
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
factory :user do
|
188
|
+
ignore do
|
189
|
+
rockstar true
|
190
|
+
upcased { false }
|
191
|
+
end
|
192
|
+
|
193
|
+
name { "John Doe#{" - Rockstar" if rockstar}" }
|
194
|
+
email { "#{name.downcase}@example.com" }
|
195
|
+
|
196
|
+
after_create do |user, evaluator|
|
197
|
+
user.name.upcase! if evaluator.upcased
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
FactoryGirl.create(:user, :upcased => true).name
|
202
|
+
#=> "JOHN DOE - ROCKSTAR"
|
203
|
+
```
|
204
|
+
|
205
|
+
Static and dynamic attributes can be ignored. Ignored attributes will be ignored
|
206
|
+
within attributes\_for and won't be set on the model, even if the attribute
|
207
|
+
exists or you attempt to override it.
|
208
|
+
|
209
|
+
Within Factory Girl's dynamic attributes, you can access ignored attributes as
|
210
|
+
you would expect. If you need to access the evaluator in a Factory Girl callback,
|
211
|
+
you'll need to declare a second block argument (for the evaluator) and access
|
212
|
+
ignored attributes from there.
|
213
|
+
|
214
|
+
Associations
|
215
|
+
------------
|
216
|
+
|
217
|
+
It's possible to set up associations within factories. If the factory name is the same as the association name, the factory name can be left out.
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
factory :post do
|
221
|
+
# ...
|
222
|
+
author
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
You can also specify a different factory or override attributes:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
factory :post do
|
230
|
+
# ...
|
231
|
+
association :author, :factory => :user, :last_name => 'Writely'
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
The behavior of the association method varies depending on the build strategy used for the parent object.
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
# Builds and saves a User and a Post
|
239
|
+
post = FactoryGirl.create(:post)
|
240
|
+
post.new_record? # => false
|
241
|
+
post.author.new_record? # => false
|
242
|
+
|
243
|
+
# Builds and saves a User, and then builds but does not save a Post
|
244
|
+
post = FactoryGirl.build(:post)
|
245
|
+
post.new_record? # => true
|
246
|
+
post.author.new_record? # => false
|
247
|
+
```
|
248
|
+
|
249
|
+
To not save the associated object, specify :strategy => :build in the factory:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
factory :post do
|
253
|
+
# ...
|
254
|
+
association :author, :factory => :user, :strategy => :build
|
255
|
+
end
|
256
|
+
|
257
|
+
# Builds a User, and then builds a Post, but does not save either
|
258
|
+
post = FactoryGirl.build(:post)
|
259
|
+
post.new_record? # => true
|
260
|
+
post.author.new_record? # => true
|
261
|
+
```
|
262
|
+
|
263
|
+
Generating data for a `has_many` relationship is a bit more involved,
|
264
|
+
depending on the amount of flexibility desired, but here's a surefire example
|
265
|
+
of generating associated data.
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
FactoryGirl.define do
|
269
|
+
|
270
|
+
# post factory with a `belongs_to` association for the user
|
271
|
+
factory :post do
|
272
|
+
title "Through the Looking Glass"
|
273
|
+
user
|
274
|
+
end
|
275
|
+
|
276
|
+
# user factory without associated posts
|
277
|
+
factory :user do
|
278
|
+
name "John Doe"
|
279
|
+
|
280
|
+
# user_with_posts will create post data after the user has been created
|
281
|
+
factory :user_with_posts do
|
282
|
+
# posts_count is declared as an ignored attribute and available in
|
283
|
+
# attributes on the factory, as well as the callback via the evaluator
|
284
|
+
ignore do
|
285
|
+
posts_count 5
|
286
|
+
end
|
287
|
+
|
288
|
+
# the after_create yields two values; the user instance itself and the
|
289
|
+
# evaluator, which stores all values from the factory, including ignored
|
290
|
+
# attributes; `create_list`'s second argument is the number of records
|
291
|
+
# to create and we make sure the user is associated properly to the post
|
292
|
+
after_create do |user, evaluator|
|
293
|
+
FactoryGirl.create_list(:post, evaluator.posts_count, :user => user)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
```
|
299
|
+
|
300
|
+
This allows us to do:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
FactoryGirl.create(:user).posts.length # 0
|
304
|
+
FactoryGirl.create(:user_with_posts).posts.length # 5
|
305
|
+
FactoryGirl.create(:user_with_posts, :posts_count => 15).posts.length # 15
|
306
|
+
```
|
307
|
+
|
308
|
+
Inheritance
|
309
|
+
-----------
|
310
|
+
|
311
|
+
You can easily create multiple factories for the same class without repeating common attributes by nesting factories:
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
factory :post do
|
315
|
+
title 'A title'
|
316
|
+
|
317
|
+
factory :approved_post do
|
318
|
+
approved true
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
approved_post = FactoryGirl.create(:approved_post)
|
323
|
+
approved_post.title # => 'A title'
|
324
|
+
approved_post.approved # => true
|
325
|
+
```
|
326
|
+
|
327
|
+
You can also assign the parent explicitly:
|
328
|
+
|
329
|
+
```ruby
|
330
|
+
factory :post do
|
331
|
+
title 'A title'
|
332
|
+
end
|
333
|
+
|
334
|
+
factory :approved_post, :parent => :post do
|
335
|
+
approved true
|
336
|
+
end
|
337
|
+
```
|
338
|
+
|
339
|
+
As mentioned above, it's good practice to define a basic factory for each class
|
340
|
+
with only the attributes required to create it. Then, create more specific
|
341
|
+
factories that inherit from this basic parent. Factory definitions are still
|
342
|
+
code, so keep them DRY.
|
343
|
+
|
344
|
+
Sequences
|
345
|
+
---------
|
346
|
+
|
347
|
+
Unique values in a specific format (for example, e-mail addresses) can be
|
348
|
+
generated using sequences. Sequences are defined by calling sequence in a
|
349
|
+
definition block, and values in a sequence are generated by calling
|
350
|
+
FactoryGirl.generate:
|
351
|
+
|
352
|
+
```ruby
|
353
|
+
# Defines a new sequence
|
354
|
+
FactoryGirl.define do
|
355
|
+
sequence :email do |n|
|
356
|
+
"person#{n}@example.com"
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
FactoryGirl.generate :email
|
361
|
+
# => "person1@example.com"
|
362
|
+
|
363
|
+
FactoryGirl.generate :email
|
364
|
+
# => "person2@example.com"
|
365
|
+
```
|
366
|
+
|
367
|
+
Sequences can be used as attributes:
|
368
|
+
|
369
|
+
```ruby
|
370
|
+
factory :user do
|
371
|
+
email
|
372
|
+
end
|
373
|
+
```
|
374
|
+
|
375
|
+
Or in lazy attributes:
|
376
|
+
|
377
|
+
```ruby
|
378
|
+
factory :invite do
|
379
|
+
invitee { FactoryGirl.generate(:email) }
|
380
|
+
end
|
381
|
+
```
|
382
|
+
|
383
|
+
And it's also possible to define an in-line sequence that is only used in
|
384
|
+
a particular factory:
|
385
|
+
|
386
|
+
```ruby
|
387
|
+
factory :user do
|
388
|
+
sequence(:email) {|n| "person#{n}@example.com" }
|
389
|
+
end
|
390
|
+
```
|
391
|
+
|
392
|
+
You can also override the initial value:
|
393
|
+
|
394
|
+
```ruby
|
395
|
+
factory :user do
|
396
|
+
sequence(:email, 1000) {|n| "person#{n}@example.com" }
|
397
|
+
end
|
398
|
+
```
|
399
|
+
|
400
|
+
Without a block, the value will increment itself, starting at its initial value:
|
401
|
+
|
402
|
+
```ruby
|
403
|
+
factory :post do
|
404
|
+
sequence(:position)
|
405
|
+
end
|
406
|
+
```
|
407
|
+
|
408
|
+
Traits
|
409
|
+
------
|
410
|
+
|
411
|
+
Traits allow you to group attributes together and then apply them
|
412
|
+
to any factory.
|
413
|
+
|
414
|
+
```ruby
|
415
|
+
factory :user, :aliases => [:author]
|
416
|
+
|
417
|
+
factory :story do
|
418
|
+
title "My awesome story"
|
419
|
+
author
|
420
|
+
|
421
|
+
trait :published do
|
422
|
+
published true
|
423
|
+
end
|
424
|
+
|
425
|
+
trait :unpublished do
|
426
|
+
published false
|
427
|
+
end
|
428
|
+
|
429
|
+
trait :week_long_publishing do
|
430
|
+
start_at { 1.week.ago }
|
431
|
+
end_at { Time.now }
|
432
|
+
end
|
433
|
+
|
434
|
+
trait :month_long_publishing do
|
435
|
+
start_at { 1.month.ago }
|
436
|
+
end_at { Time.now }
|
437
|
+
end
|
438
|
+
|
439
|
+
factory :week_long_published_story, :traits => [:published, :week_long_publishing]
|
440
|
+
factory :month_long_published_story, :traits => [:published, :month_long_publishing]
|
441
|
+
factory :week_long_unpublished_story, :traits => [:unpublished, :week_long_publishing]
|
442
|
+
factory :month_long_unpublished_story, :traits => [:unpublished, :month_long_publishing]
|
443
|
+
end
|
444
|
+
```
|
445
|
+
|
446
|
+
Traits can be used as attributes:
|
447
|
+
|
448
|
+
```ruby
|
449
|
+
factory :week_long_published_story_with_title, :parent => :story do
|
450
|
+
published
|
451
|
+
week_long_publishing
|
452
|
+
title { "Publishing that was started at {start_at}" }
|
453
|
+
end
|
454
|
+
```
|
455
|
+
|
456
|
+
Traits that define the same attributes won't raise AttributeDefinitionErrors;
|
457
|
+
the trait that defines the attribute latest gets precedence.
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
factory :user do
|
461
|
+
name "Friendly User"
|
462
|
+
login { name }
|
463
|
+
|
464
|
+
trait :male do
|
465
|
+
name "John Doe"
|
466
|
+
gender "Male"
|
467
|
+
login { "#{name} (M)" }
|
468
|
+
end
|
469
|
+
|
470
|
+
trait :female do
|
471
|
+
name "Jane Doe"
|
472
|
+
gender "Female"
|
473
|
+
login { "#{name} (F)" }
|
474
|
+
end
|
475
|
+
|
476
|
+
trait :admin do
|
477
|
+
admin true
|
478
|
+
login { "admin-#{name}" }
|
479
|
+
end
|
480
|
+
|
481
|
+
factory :male_admin, :traits => [:male, :admin] # login will be "admin-John Doe"
|
482
|
+
factory :female_admin, :traits => [:admin, :female] # login will be "Jane Doe (F)"
|
483
|
+
end
|
484
|
+
```
|
485
|
+
|
486
|
+
You can also override individual attributes granted by a trait in subclasses.
|
487
|
+
|
488
|
+
```ruby
|
489
|
+
factory :user do
|
490
|
+
name "Friendly User"
|
491
|
+
login { name }
|
492
|
+
|
493
|
+
trait :male do
|
494
|
+
name "John Doe"
|
495
|
+
gender "Male"
|
496
|
+
login { "#{name} (M)" }
|
497
|
+
end
|
498
|
+
|
499
|
+
factory :brandon do
|
500
|
+
male
|
501
|
+
name "Brandon"
|
502
|
+
end
|
503
|
+
end
|
504
|
+
```
|
505
|
+
|
506
|
+
Traits can also be passed in as a list of symbols when you construct an instance from FactoryGirl.
|
507
|
+
|
508
|
+
```ruby
|
509
|
+
factory :user do
|
510
|
+
name "Friendly User"
|
511
|
+
|
512
|
+
trait :male do
|
513
|
+
name "John Doe"
|
514
|
+
gender "Male"
|
515
|
+
end
|
516
|
+
|
517
|
+
trait :admin do
|
518
|
+
admin true
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
# creates an admin user with gender "Male" and name "Jon Snow"
|
523
|
+
FactoryGirl.create(:user, :admin, :male, :name => "Jon Snow")
|
524
|
+
```
|
525
|
+
|
526
|
+
This ability works with `build`, `build_stubbed`, `attributes_for`, and `create`.
|
527
|
+
|
528
|
+
`create_list` and `build_list` methods are supported as well. Just remember to pass
|
529
|
+
the number of instances to create/build as second parameter, as documented in the
|
530
|
+
"Building or Creating Multiple Records" section of this file.
|
531
|
+
|
532
|
+
```ruby
|
533
|
+
factory :user do
|
534
|
+
name "Friendly User"
|
535
|
+
|
536
|
+
trait :admin do
|
537
|
+
admin true
|
538
|
+
end
|
539
|
+
end
|
540
|
+
|
541
|
+
# creates 3 admin users with gender "Male" and name "Jon Snow"
|
542
|
+
FactoryGirl.create_list(:user, 3, :admin, :male, :name => "Jon Snow")
|
543
|
+
```
|
544
|
+
|
545
|
+
|
546
|
+
Callbacks
|
547
|
+
---------
|
548
|
+
|
549
|
+
factory\_girl makes available three callbacks for injecting some code:
|
550
|
+
|
551
|
+
* after_build - called after a factory is built (via FactoryGirl.build)
|
552
|
+
* after_create - called after a factory is saved (via FactoryGirl.create)
|
553
|
+
* after_stub - called after a factory is stubbed (via FactoryGirl.build_stubbed)
|
554
|
+
|
555
|
+
Examples:
|
556
|
+
|
557
|
+
```ruby
|
558
|
+
# Define a factory that calls the generate_hashed_password method after it is built
|
559
|
+
factory :user do
|
560
|
+
after_build { |user| generate_hashed_password(user) }
|
561
|
+
end
|
562
|
+
```
|
563
|
+
|
564
|
+
Note that you'll have an instance of the user in the block. This can be useful.
|
565
|
+
|
566
|
+
You can also define multiple types of callbacks on the same factory:
|
567
|
+
|
568
|
+
```ruby
|
569
|
+
factory :user do
|
570
|
+
after_build { |user| do_something_to(user) }
|
571
|
+
after_create { |user| do_something_else_to(user) }
|
572
|
+
end
|
573
|
+
```
|
574
|
+
|
575
|
+
Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
|
576
|
+
|
577
|
+
```ruby
|
578
|
+
factory :user do
|
579
|
+
after_create { this_runs_first }
|
580
|
+
after_create { then_this }
|
581
|
+
end
|
582
|
+
```
|
583
|
+
|
584
|
+
Calling FactoryGirl.create will invoke both after\_build and after\_create callbacks.
|
585
|
+
|
586
|
+
Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
|
587
|
+
|
588
|
+
Modifying factories
|
589
|
+
-------------------
|
590
|
+
|
591
|
+
If you're given a set of factories (say, from a gem developer) but want to change them to fit into your application better, you can
|
592
|
+
modify that factory instead of creating a child factory and adding attributes there.
|
593
|
+
|
594
|
+
If a gem were to give you a User factory:
|
595
|
+
|
596
|
+
```ruby
|
597
|
+
FactoryGirl.define do
|
598
|
+
factory :user do
|
599
|
+
full_name "John Doe"
|
600
|
+
sequence(:username) {|n| "user#{n}" }
|
601
|
+
password "password"
|
602
|
+
end
|
603
|
+
end
|
604
|
+
```
|
605
|
+
|
606
|
+
Instead of creating a child factory that added additional attributes:
|
607
|
+
|
608
|
+
```ruby
|
609
|
+
FactoryGirl.define do
|
610
|
+
factory :application_user, :parent => :user do
|
611
|
+
full_name { Faker::Name.name }
|
612
|
+
date_of_birth { 21.years.ago }
|
613
|
+
gender "Female"
|
614
|
+
health 90
|
615
|
+
end
|
616
|
+
end
|
617
|
+
```
|
618
|
+
|
619
|
+
You could modify that factory instead.
|
620
|
+
|
621
|
+
```ruby
|
622
|
+
FactoryGirl.modify do
|
623
|
+
factory :user do
|
624
|
+
full_name { Faker::Name.name }
|
625
|
+
date_of_birth { 21.years.ago }
|
626
|
+
gender "Female"
|
627
|
+
health 90
|
628
|
+
end
|
629
|
+
end
|
630
|
+
```
|
631
|
+
|
632
|
+
When modifying a factory, you can change any of the attributes you want (aside from callbacks).
|
633
|
+
|
634
|
+
`FactoryGirl.modify` must be called outside of a `FactoryGirl.define` block as it operates on factories differently.
|
635
|
+
|
636
|
+
A caveat: you can only modify factories (not sequences or traits) and callbacks *still compound as they normally would*. So, if
|
637
|
+
the factory you're modifying defines an `after_create` callback, you defining an `after_create` won't override it, it'll just get run after the first callback.
|
638
|
+
|
639
|
+
Building or Creating Multiple Records
|
640
|
+
-------------------------------------
|
641
|
+
|
642
|
+
Sometimes, you'll want to create or build multiple instances of a factory at once.
|
643
|
+
|
644
|
+
```ruby
|
645
|
+
built_users = FactoryGirl.build_list(:user, 25)
|
646
|
+
created_users = FactoryGirl.create_list(:user, 25)
|
647
|
+
```
|
648
|
+
|
649
|
+
These methods will build or create a specific amount of factories and return them as an array.
|
650
|
+
To set the attributes for each of the factories, you can pass in a hash as you normally would.
|
651
|
+
|
652
|
+
```ruby
|
653
|
+
twenty_year_olds = FactoryGirl.build_list(:user, 25, :date_of_birth => 20.years.ago)
|
654
|
+
```
|
655
|
+
|
656
|
+
Custom Construction
|
657
|
+
-------------------
|
658
|
+
|
659
|
+
If you want to use factory_girl to construct an object where some attributes
|
660
|
+
are passed to `initialize` or if you want to do something other than simply
|
661
|
+
calling `new` on your build class, you can override the default behavior by
|
662
|
+
defining `to_initialize` on your factory. Example:
|
663
|
+
|
664
|
+
```ruby
|
665
|
+
# user.rb
|
666
|
+
class User
|
667
|
+
attr_accessor :name, :email
|
668
|
+
|
669
|
+
def initialize(name)
|
670
|
+
@name = name
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
# factories.rb
|
675
|
+
sequence(:name) {|n| "person#{n}@example.com" }
|
676
|
+
|
677
|
+
factory :user do
|
678
|
+
ignore do
|
679
|
+
name { Faker::Name.name }
|
680
|
+
end
|
681
|
+
|
682
|
+
email
|
683
|
+
initialize_with { User.new(name) }
|
684
|
+
end
|
685
|
+
|
686
|
+
FactoryGirl.build(:user).name # Bob Hope
|
687
|
+
```
|
688
|
+
|
689
|
+
Notice that I ignored the `name` attribute. If you don't want attributes
|
690
|
+
reassigned after your object has been instantiated, you'll want to `ignore` them.
|
691
|
+
|
692
|
+
Although factory_girl is written to work with ActiveRecord out of the box, it
|
693
|
+
can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
|
694
|
+
the default initializer builds all instances by calling new on your build class
|
695
|
+
without any arguments. It then calls attribute writer methods to assign all the
|
696
|
+
attribute values. While that works fine for ActiveRecord, it actually doesn't
|
697
|
+
work for almost any other Ruby class.
|
698
|
+
|
699
|
+
You can override the initializer in order to:
|
700
|
+
|
701
|
+
* Build non-ActiveRecord objects that require arguments to `initialize`
|
702
|
+
* Use a method other than `new` to instantiate the instance
|
703
|
+
* Do crazy things like decorate the instance after it's built
|
704
|
+
|
705
|
+
Cucumber Integration
|
706
|
+
--------------------
|
707
|
+
|
708
|
+
factory\_girl ships with step definitions that make calling factories from Cucumber easier. To use them, add the following to features/support/env.rb:
|
709
|
+
|
710
|
+
```ruby
|
711
|
+
require 'factory_girl/step_definitions'
|
712
|
+
```
|
713
|
+
|
714
|
+
Alternate Syntaxes
|
715
|
+
------------------
|
716
|
+
|
717
|
+
Users' tastes for syntax vary dramatically, but most users are looking for a
|
718
|
+
common feature set. Because of this factory\_girl supports "syntax layers" which
|
719
|
+
provide alternate interfaces. See Factory::Syntax for information about the
|
720
|
+
various layers available. For example, the Machinist-style syntax is popular:
|
721
|
+
|
722
|
+
```ruby
|
723
|
+
require 'factory_girl/syntax/blueprint'
|
724
|
+
require 'factory_girl/syntax/make'
|
725
|
+
require 'factory_girl/syntax/sham'
|
726
|
+
|
727
|
+
Sham.email {|n| "#{n}@example.com" }
|
728
|
+
|
729
|
+
User.blueprint do
|
730
|
+
name { 'Billy Bob' }
|
731
|
+
email { Sham.email }
|
732
|
+
end
|
733
|
+
|
734
|
+
User.make(:name => 'Johnny')
|
735
|
+
```
|