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
@@ -0,0 +1,32 @@
|
|
1
|
+
require "factory_girl/strategy/build"
|
2
|
+
require "factory_girl/strategy/create"
|
3
|
+
require "factory_girl/strategy/attributes_for"
|
4
|
+
require "factory_girl/strategy/stub"
|
5
|
+
require "observer"
|
6
|
+
|
7
|
+
module FactoryGirl
|
8
|
+
class Strategy #:nodoc:
|
9
|
+
include Observable
|
10
|
+
|
11
|
+
def association(runner)
|
12
|
+
raise NotImplementedError, "Strategies must return an association"
|
13
|
+
end
|
14
|
+
|
15
|
+
def result(attribute_assigner, to_create)
|
16
|
+
raise NotImplementedError, "Strategies must return a result"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.ensure_strategy_exists!(strategy)
|
20
|
+
unless Strategy.const_defined? strategy.to_s.camelize
|
21
|
+
raise ArgumentError, "Unknown strategy: #{strategy}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def run_callbacks(name, result_instance)
|
28
|
+
changed
|
29
|
+
notify_observers(name, result_instance)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Strategy #:nodoc:
|
3
|
+
class Build < Strategy #:nodoc:
|
4
|
+
def association(runner)
|
5
|
+
runner.run
|
6
|
+
end
|
7
|
+
|
8
|
+
def result(attribute_assigner, to_create)
|
9
|
+
attribute_assigner.object.tap do |result_instance|
|
10
|
+
run_callbacks(:after_build, result_instance)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Strategy #:nodoc:
|
3
|
+
class Create < Strategy #:nodoc:
|
4
|
+
def association(runner)
|
5
|
+
runner.run
|
6
|
+
end
|
7
|
+
|
8
|
+
def result(attribute_assigner, to_create)
|
9
|
+
attribute_assigner.object.tap do |result_instance|
|
10
|
+
run_callbacks(:after_build, result_instance)
|
11
|
+
to_create[result_instance]
|
12
|
+
run_callbacks(:after_create, result_instance)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Strategy
|
3
|
+
class Stub < Strategy #:nodoc:
|
4
|
+
@@next_id = 1000
|
5
|
+
|
6
|
+
def association(runner)
|
7
|
+
runner.run(Strategy::Stub)
|
8
|
+
end
|
9
|
+
|
10
|
+
def result(attribute_assigner, to_create)
|
11
|
+
attribute_assigner.object.tap do |result_instance|
|
12
|
+
stub_database_interaction_on_result(result_instance)
|
13
|
+
run_callbacks(:after_stub, result_instance)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def next_id
|
20
|
+
@@next_id += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def stub_database_interaction_on_result(result_instance)
|
24
|
+
result_instance.id = next_id
|
25
|
+
result_instance.instance_eval do
|
26
|
+
def persisted?
|
27
|
+
!new_record?
|
28
|
+
end
|
29
|
+
|
30
|
+
def created_at
|
31
|
+
@created_at ||= Time.now
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_record?
|
35
|
+
id.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def save(*args)
|
39
|
+
raise "stubbed models are not allowed to access the database"
|
40
|
+
end
|
41
|
+
|
42
|
+
def destroy(*args)
|
43
|
+
raise "stubbed models are not allowed to access the database"
|
44
|
+
end
|
45
|
+
|
46
|
+
def connection
|
47
|
+
raise "stubbed models are not allowed to access the database"
|
48
|
+
end
|
49
|
+
|
50
|
+
def reload
|
51
|
+
raise "stubbed models are not allowed to access the database"
|
52
|
+
end
|
53
|
+
|
54
|
+
def update_attribute(*args)
|
55
|
+
raise "stubbed models are not allowed to access the database"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class StrategyCalculator
|
3
|
+
def initialize(name_or_object)
|
4
|
+
@name_or_object = name_or_object
|
5
|
+
end
|
6
|
+
|
7
|
+
def strategy
|
8
|
+
if strategy_is_object?
|
9
|
+
@name_or_object
|
10
|
+
else
|
11
|
+
strategy_name_to_object
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def strategy_is_object?
|
18
|
+
@name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
|
19
|
+
end
|
20
|
+
|
21
|
+
def strategy_name_to_object
|
22
|
+
case @name_or_object
|
23
|
+
when :build then Strategy::Build
|
24
|
+
when :create then Strategy::Create
|
25
|
+
else raise "unrecognized method #{@name_or_object}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "factory_girl/syntax/methods"
|
2
|
+
require "factory_girl/syntax/default"
|
3
|
+
require "factory_girl/syntax/vintage"
|
4
|
+
|
5
|
+
module FactoryGirl
|
6
|
+
# Provides alternate syntaxes for factory_girl. If you don't like the default
|
7
|
+
# syntax for defining or using factories, look at one of the
|
8
|
+
# FactoryGirl::Syntax modules:
|
9
|
+
#
|
10
|
+
# * FactoryGirl::Syntax::Blueprint: definition syntax similar to Machinist
|
11
|
+
# * FactoryGirl::Syntax::Generate: usage syntax similar to Object Daddy
|
12
|
+
# * FactoryGirl::Syntax::Make: usage syntax similar to Machinist
|
13
|
+
# * FactoryGirl::Syntax::Sham: sequence syntax similar to Machinist
|
14
|
+
module Syntax
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide a make class method, which is an
|
5
|
+
# alternate syntax for defining factories.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/blueprint'
|
10
|
+
#
|
11
|
+
# User.blueprint do
|
12
|
+
# name { 'Billy Bob' }
|
13
|
+
# email { 'billy@bob.example.com' }
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# FactoryGirl.create(:user, name: 'Johnny')
|
17
|
+
#
|
18
|
+
# This syntax was derived from Pete Yandell's machinist.
|
19
|
+
module Blueprint
|
20
|
+
module ActiveRecord #:nodoc:
|
21
|
+
|
22
|
+
def self.included(base) # :nodoc:
|
23
|
+
base.extend ClassMethods
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods #:nodoc:
|
27
|
+
|
28
|
+
def blueprint(&block)
|
29
|
+
instance = Factory.new(name.underscore, class: self)
|
30
|
+
proxy = FactoryGirl::DefinitionProxy.new(instance)
|
31
|
+
proxy.instance_eval(&block)
|
32
|
+
FactoryGirl.register_factory(instance)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Blueprint::ActiveRecord)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
module Default
|
4
|
+
include Methods
|
5
|
+
|
6
|
+
def define(&block)
|
7
|
+
DSL.run(block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def modify(&block)
|
11
|
+
ModifyDSL.run(block)
|
12
|
+
end
|
13
|
+
|
14
|
+
class DSL
|
15
|
+
def self.run(block)
|
16
|
+
new.instance_eval(&block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def factory(name, options = {}, &block)
|
20
|
+
factory = Factory.new(name, options)
|
21
|
+
proxy = FactoryGirl::DefinitionProxy.new(factory.definition)
|
22
|
+
proxy.instance_eval(&block) if block_given?
|
23
|
+
|
24
|
+
FactoryGirl.register_factory(factory)
|
25
|
+
|
26
|
+
proxy.child_factories.each do |(child_name, child_options, child_block)|
|
27
|
+
parent_factory = child_options.delete(:parent) || name
|
28
|
+
factory(child_name, child_options.merge(parent: parent_factory), &child_block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def sequence(name, start_value = 1, &block)
|
33
|
+
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
34
|
+
end
|
35
|
+
|
36
|
+
def trait(name, &block)
|
37
|
+
FactoryGirl.register_trait(Trait.new(name, &block))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class ModifyDSL
|
42
|
+
def self.run(block)
|
43
|
+
new.instance_eval(&block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def factory(name, options = {}, &block)
|
47
|
+
factory = FactoryGirl.factory_by_name(name)
|
48
|
+
proxy = FactoryGirl::DefinitionProxy.new(factory.definition.overridable)
|
49
|
+
proxy.instance_eval(&block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
extend Syntax::Default
|
56
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide generation methods for factories.
|
5
|
+
#
|
6
|
+
# Usage:
|
7
|
+
#
|
8
|
+
# require 'factory_girl/syntax/generate'
|
9
|
+
#
|
10
|
+
# FactoryGirl.define do
|
11
|
+
# factory :user do
|
12
|
+
# name 'Billy Bob'
|
13
|
+
# email 'billy@bob.example.com'
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # Creates a saved instance without raising (same as saving the result
|
18
|
+
# # of FactoryGirl.build)
|
19
|
+
# User.generate(name: 'Johnny')
|
20
|
+
#
|
21
|
+
# # Creates a saved instance and raises when invalid (same as
|
22
|
+
# # FactoryGirl.create)
|
23
|
+
# User.generate!
|
24
|
+
#
|
25
|
+
# # Creates an unsaved instance (same as FactoryGirl.build)
|
26
|
+
# User.spawn
|
27
|
+
#
|
28
|
+
# # Creates an instance and yields it to the passed block
|
29
|
+
# User.generate do |user|
|
30
|
+
# # ...do something with user...
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# This syntax was derived from Rick Bradley and Yossef Mendelssohn's
|
34
|
+
# object_daddy.
|
35
|
+
module Generate
|
36
|
+
module ActiveRecord #:nodoc:
|
37
|
+
|
38
|
+
def self.included(base) # :nodoc:
|
39
|
+
base.extend ClassMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods #:nodoc:
|
43
|
+
|
44
|
+
def generate(overrides = {}, &block)
|
45
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
46
|
+
instance.save
|
47
|
+
yield(instance) if block_given?
|
48
|
+
instance
|
49
|
+
end
|
50
|
+
|
51
|
+
def generate!(overrides = {}, &block)
|
52
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
|
53
|
+
yield(instance) if block_given?
|
54
|
+
instance
|
55
|
+
end
|
56
|
+
|
57
|
+
def spawn(overrides = {}, &block)
|
58
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
59
|
+
yield(instance) if block_given?
|
60
|
+
instance
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Generate::ActiveRecord)
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide a make class method, which is a
|
5
|
+
# shortcut for FactoryGirl.create.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/make'
|
10
|
+
#
|
11
|
+
# FactoryGirl.define do
|
12
|
+
# factory :user do
|
13
|
+
# name 'Billy Bob'
|
14
|
+
# email 'billy@bob.example.com'
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# User.make(name: 'Johnny')
|
19
|
+
#
|
20
|
+
# This syntax was derived from Pete Yandell's machinist.
|
21
|
+
module Make
|
22
|
+
module ActiveRecord #:nodoc:
|
23
|
+
|
24
|
+
def self.included(base) # :nodoc:
|
25
|
+
base.extend ClassMethods
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods #:nodoc:
|
29
|
+
|
30
|
+
def make(overrides = {})
|
31
|
+
FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
32
|
+
end
|
33
|
+
|
34
|
+
def make!(overrides = {})
|
35
|
+
FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Make::ActiveRecord)
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
module Methods
|
4
|
+
# Generates and returns a Hash of attributes from this factory. Attributes
|
5
|
+
# can be individually overridden by passing in a Hash of attribute => value
|
6
|
+
# pairs.
|
7
|
+
#
|
8
|
+
# Arguments:
|
9
|
+
# * name: +Symbol+ or +String+
|
10
|
+
# The name of the factory that should be used.
|
11
|
+
# * traits_and_overrides: +Array+
|
12
|
+
# [+*Array+] Traits to be applied
|
13
|
+
# [+Hash+] Attributes to overwrite for this set.
|
14
|
+
# * block:
|
15
|
+
# Yields the hash of attributes.
|
16
|
+
#
|
17
|
+
# Returns: +Hash+
|
18
|
+
# A set of attributes that can be used to build an instance of the class
|
19
|
+
# this factory generates.
|
20
|
+
def attributes_for(name, *traits_and_overrides, &block)
|
21
|
+
FactoryRunner.new(name, Strategy::AttributesFor, traits_and_overrides).run(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Generates and returns an instance from this factory. Attributes can be
|
25
|
+
# individually overridden by passing in a Hash of attribute => value pairs.
|
26
|
+
#
|
27
|
+
# Arguments:
|
28
|
+
# * name: +Symbol+ or +String+
|
29
|
+
# The name of the factory that should be used.
|
30
|
+
# * traits_and_overrides: +Array+
|
31
|
+
# [+*Array+] Traits to be applied
|
32
|
+
# [+Hash+] Attributes to overwrite for this instance.
|
33
|
+
# * block:
|
34
|
+
# Yields the built instance.
|
35
|
+
#
|
36
|
+
# Returns: +Object+
|
37
|
+
# An instance of the class this factory generates, with generated attributes
|
38
|
+
# assigned.
|
39
|
+
def build(name, *traits_and_overrides, &block)
|
40
|
+
FactoryRunner.new(name, Strategy::Build, traits_and_overrides).run(&block)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Generates, saves, and returns an instance from this factory. Attributes can
|
44
|
+
# be individually overridden by passing in a Hash of attribute => value
|
45
|
+
# pairs.
|
46
|
+
#
|
47
|
+
# Instances are saved using the +save!+ method, so ActiveRecord models will
|
48
|
+
# raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
|
49
|
+
#
|
50
|
+
# Arguments:
|
51
|
+
# * name: +Symbol+ or +String+
|
52
|
+
# The name of the factory that should be used.
|
53
|
+
# * traits_and_overrides: +Array+
|
54
|
+
# [+*Array+] Traits to be applied
|
55
|
+
# [+Hash+] Attributes to overwrite for this instance.
|
56
|
+
# * block:
|
57
|
+
# Yields the created instance.
|
58
|
+
#
|
59
|
+
# Returns: +Object+
|
60
|
+
# A saved instance of the class this factory generates, with generated
|
61
|
+
# attributes assigned.
|
62
|
+
def create(name, *traits_and_overrides, &block)
|
63
|
+
FactoryRunner.new(name, Strategy::Create, traits_and_overrides).run(&block)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generates and returns an object with all attributes from this factory
|
67
|
+
# stubbed out. Attributes can be individually overridden by passing in a Hash
|
68
|
+
# of attribute => value pairs.
|
69
|
+
#
|
70
|
+
# Arguments:
|
71
|
+
# * name: +Symbol+ or +String+
|
72
|
+
# The name of the factory that should be used.
|
73
|
+
# * traits_and_overrides: +Array+
|
74
|
+
# [+*Array+] Traits to be applied
|
75
|
+
# [+Hash+] Attributes to overwrite for this instance.
|
76
|
+
# * block
|
77
|
+
# Yields the stubbed object.
|
78
|
+
#
|
79
|
+
# Returns: +Object+
|
80
|
+
# An object with generated attributes stubbed out.
|
81
|
+
def build_stubbed(name, *traits_and_overrides, &block)
|
82
|
+
FactoryRunner.new(name, Strategy::Stub, traits_and_overrides).run(&block)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Builds and returns multiple instances from this factory as an array. Attributes can be
|
86
|
+
# individually overridden by passing in a Hash of attribute => value pairs.
|
87
|
+
#
|
88
|
+
# Arguments:
|
89
|
+
# * name: +Symbol+ or +String+
|
90
|
+
# The name of the factory to be used.
|
91
|
+
# * amount: +Integer+
|
92
|
+
# number of instances to be built.
|
93
|
+
# * traits_and_overrides: +Array+
|
94
|
+
# [+*Array+] Traits to be applied
|
95
|
+
# [+Hash+] Attributes to overwrite for this instance.
|
96
|
+
#
|
97
|
+
# Returns: +Array+
|
98
|
+
# An array of instances of the class this factory generates, with generated attributes
|
99
|
+
# assigned.
|
100
|
+
def build_list(name, amount, *traits_and_overrides)
|
101
|
+
amount.times.map { build(name, *traits_and_overrides) }
|
102
|
+
end
|
103
|
+
|
104
|
+
# Creates and returns multiple instances from this factory as an array. Attributes can be
|
105
|
+
# individually overridden by passing in a Hash of attribute => value pairs.
|
106
|
+
#
|
107
|
+
# Arguments:
|
108
|
+
# * name: +Symbol+ or +String+
|
109
|
+
# The name of the factory to be used.
|
110
|
+
# * amount: +Integer+
|
111
|
+
# number of instances to be created.
|
112
|
+
# * traits_and_overrides: +Array+
|
113
|
+
# [+*Array+] Traits to be applied
|
114
|
+
# [+Hash+] Attributes to overwrite for this instance.
|
115
|
+
#
|
116
|
+
# Returns: +Array+
|
117
|
+
# An array of instances of the class this factory generates, with generated attributes
|
118
|
+
# assigned.
|
119
|
+
def create_list(name, amount, *traits_and_overrides)
|
120
|
+
amount.times.map { create(name, *traits_and_overrides) }
|
121
|
+
end
|
122
|
+
|
123
|
+
# Generates and returns the next value in a sequence.
|
124
|
+
#
|
125
|
+
# Arguments:
|
126
|
+
# name: (Symbol)
|
127
|
+
# The name of the sequence that a value should be generated for.
|
128
|
+
#
|
129
|
+
# Returns:
|
130
|
+
# The next value in the sequence. (Object)
|
131
|
+
def generate(name)
|
132
|
+
FactoryGirl.sequence_by_name(name).next
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|