vitalish-factory_girl 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
- data/Changelog +29 -0
- data/LICENSE +19 -0
- data/README.rdoc +282 -0
- data/Rakefile +66 -0
- data/lib/factory_girl.rb +24 -0
- data/lib/factory_girl/aliases.rb +21 -0
- data/lib/factory_girl/attribute.rb +29 -0
- data/lib/factory_girl/attribute/association.rb +20 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/attribute/dynamic.rb +20 -0
- data/lib/factory_girl/attribute/static.rb +17 -0
- data/lib/factory_girl/factory.rb +215 -0
- data/lib/factory_girl/proxy.rb +77 -0
- data/lib/factory_girl/proxy/attributes_for.rb +21 -0
- data/lib/factory_girl/proxy/build.rb +32 -0
- data/lib/factory_girl/proxy/create.rb +12 -0
- data/lib/factory_girl/proxy/stub.rb +64 -0
- data/lib/factory_girl/sequence.rb +28 -0
- data/lib/factory_girl/step_definitions.rb +60 -0
- data/lib/factory_girl/syntax.rb +12 -0
- data/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/lib/factory_girl/syntax/generate.rb +73 -0
- data/lib/factory_girl/syntax/make.rb +41 -0
- data/lib/factory_girl/syntax/sham.rb +45 -0
- data/spec/factory_girl/aliases_spec.rb +33 -0
- data/spec/factory_girl/attribute/association_spec.rb +29 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/attribute/dynamic_spec.rb +60 -0
- data/spec/factory_girl/attribute/static_spec.rb +29 -0
- data/spec/factory_girl/attribute_spec.rb +30 -0
- data/spec/factory_girl/factory_spec.rb +411 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +86 -0
- data/spec/factory_girl/proxy/create_spec.rb +99 -0
- data/spec/factory_girl/proxy/stub_spec.rb +80 -0
- data/spec/factory_girl/proxy_spec.rb +84 -0
- data/spec/factory_girl/sequence_spec.rb +43 -0
- data/spec/spec_helper.rb +93 -0
- metadata +119 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Proxy
|
3
|
+
class Stub < Proxy #:nodoc:
|
4
|
+
@@next_id = 1000
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@instance = klass.new
|
8
|
+
@instance.id = next_id
|
9
|
+
@instance.instance_eval do
|
10
|
+
def new_record?
|
11
|
+
id.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def save(*args)
|
15
|
+
raise "stubbed models are not allowed to access the database"
|
16
|
+
end
|
17
|
+
|
18
|
+
def destroy(*args)
|
19
|
+
raise "stubbed models are not allowed to access the database"
|
20
|
+
end
|
21
|
+
|
22
|
+
def connection
|
23
|
+
raise "stubbed models are not allowed to access the database"
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload
|
27
|
+
raise "stubbed models are not allowed to access the database"
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_attribute(*args)
|
31
|
+
raise "stubbed models are not allowed to access the database"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def next_id
|
37
|
+
@@next_id += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def get(attribute)
|
41
|
+
@instance.send(attribute)
|
42
|
+
end
|
43
|
+
|
44
|
+
def set(attribute, value)
|
45
|
+
@instance.send(:"#{attribute}=", value)
|
46
|
+
end
|
47
|
+
|
48
|
+
def associate(name, factory_name, overrides)
|
49
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
50
|
+
set(name, factory.run(Proxy::Stub, overrides))
|
51
|
+
end
|
52
|
+
|
53
|
+
def association(factory_name, overrides = {})
|
54
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
55
|
+
factory.run(Proxy::Stub, overrides)
|
56
|
+
end
|
57
|
+
|
58
|
+
def result
|
59
|
+
run_callbacks(:after_stub)
|
60
|
+
@instance
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
|
3
|
+
# Raised when calling Factory.sequence from a dynamic attribute block
|
4
|
+
class SequenceAbuseError < StandardError; end
|
5
|
+
|
6
|
+
# Sequences are defined using Factory.sequence. Sequence values are generated
|
7
|
+
# using next.
|
8
|
+
class Sequence
|
9
|
+
|
10
|
+
def initialize(value = 1, &proc) #:nodoc:
|
11
|
+
@proc = proc
|
12
|
+
@value = value || 1
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns the next value for this sequence
|
16
|
+
def next
|
17
|
+
@proc.call(@value)
|
18
|
+
ensure
|
19
|
+
@value = @value.next
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_accessor :sequences #:nodoc:
|
26
|
+
end
|
27
|
+
self.sequences = {}
|
28
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module FactoryGirlStepHelpers
|
2
|
+
|
3
|
+
def convert_association_string_to_instance(factory_name, assignment)
|
4
|
+
attribute, value = assignment.split(':', 2)
|
5
|
+
return if value.blank?
|
6
|
+
factory = FactoryGirl.factory_by_name(factory_name)
|
7
|
+
attributes = convert_human_hash_to_attribute_hash({attribute => value.strip}, factory.associations)
|
8
|
+
attributes_find = {}
|
9
|
+
attributes.each do |k, v|
|
10
|
+
k = "#{k}_id" if v.is_a? ActiveRecord::Base
|
11
|
+
attributes_find[k] = v
|
12
|
+
end
|
13
|
+
model_class = factory.build_class
|
14
|
+
model_class.find(:first, :conditions => attributes_find) or
|
15
|
+
Factory(factory_name, attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def convert_human_hash_to_attribute_hash(human_hash, associations = [])
|
19
|
+
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
20
|
+
key = human_key.downcase.gsub(' ', '_').to_sym
|
21
|
+
if association = associations.detect {|association| association.name == key }
|
22
|
+
value = convert_association_string_to_instance(association.factory, value)
|
23
|
+
end
|
24
|
+
attribute_hash.merge(key => value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
World(FactoryGirlStepHelpers)
|
30
|
+
|
31
|
+
FactoryGirl.factories.values.each do |factory|
|
32
|
+
Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/ do |table|
|
33
|
+
table.hashes.each do |human_hash|
|
34
|
+
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
35
|
+
factory.run(FactoryGirl::Proxy::Create, attributes)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Given /^an? #{factory.human_name} exists$/ do
|
40
|
+
Factory(factory.name)
|
41
|
+
end
|
42
|
+
|
43
|
+
Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count|
|
44
|
+
count.to_i.times { Factory(factory.name) }
|
45
|
+
end
|
46
|
+
|
47
|
+
if factory.build_class.respond_to?(:columns)
|
48
|
+
factory.build_class.columns.each do |column|
|
49
|
+
human_column_name = column.name.downcase.gsub('_', ' ')
|
50
|
+
Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
|
51
|
+
Factory(factory.name, column.name => value)
|
52
|
+
end
|
53
|
+
|
54
|
+
Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
55
|
+
count.to_i.times { Factory(factory.name, column.name => value) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
# Provides alternate syntaxes for factory_girl. If you don't like the default
|
3
|
+
# syntax for defining or using factories, look at one of the
|
4
|
+
# FactoryGirl::Syntax modules:
|
5
|
+
#
|
6
|
+
# * FactoryGirl::Syntax::Blueprint: definition syntax similar to Machinist
|
7
|
+
# * FactoryGirl::Syntax::Generate: usage syntax similar to Object Daddy
|
8
|
+
# * FactoryGirl::Syntax::Make: usage syntax similar to Machinist
|
9
|
+
# * FactoryGirl::Syntax::Sham: sequence syntax similar to Machinist
|
10
|
+
module Syntax
|
11
|
+
end
|
12
|
+
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
|
+
# Factory(: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,73 @@
|
|
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 Factory.build)
|
19
|
+
# User.generate(:name => 'Johnny')
|
20
|
+
#
|
21
|
+
# # Creates a saved instance and raises when invalid (same as
|
22
|
+
# # Factory.create)
|
23
|
+
# User.generate!
|
24
|
+
#
|
25
|
+
# # Creates an unsaved instance (same as Factory.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
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
46
|
+
instance = factory.run(Proxy::Build, overrides)
|
47
|
+
instance.save
|
48
|
+
yield(instance) if block_given?
|
49
|
+
instance
|
50
|
+
end
|
51
|
+
|
52
|
+
def generate!(overrides = {}, &block)
|
53
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
54
|
+
instance = factory.run(Proxy::Create, overrides)
|
55
|
+
yield(instance) if block_given?
|
56
|
+
instance
|
57
|
+
end
|
58
|
+
|
59
|
+
def spawn(overrides = {}, &block)
|
60
|
+
factory = FactoryGirl.factory_by_name(name.underscore)
|
61
|
+
instance = factory.run(Proxy::Build, overrides)
|
62
|
+
yield(instance) if block_given?
|
63
|
+
instance
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Generate::ActiveRecord)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Extends ActiveRecord::Base to provide a make class method, which is a
|
5
|
+
# shortcut for Factory.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
|
+
FactoryGirl.factory_by_name(name.underscore).run(Proxy::Create, overrides)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Make::ActiveRecord)
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
# Adds a Sham module, which provides an alternate interface to
|
5
|
+
# FactoryGirl::Sequence.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'factory_girl/syntax/sham'
|
10
|
+
#
|
11
|
+
# Sham.email {|n| "somebody#{n}@example.com" }
|
12
|
+
#
|
13
|
+
# FactoryGirl.define do
|
14
|
+
# factory :user do
|
15
|
+
# email
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Note that you can also use Faker, but it is recommended that you simply
|
20
|
+
# use a sequence as in the above example, as factory_girl does not provide
|
21
|
+
# protection against duplication in randomized sequences, and a randomized
|
22
|
+
# value does not provide any tangible benefits over an ascending sequence.
|
23
|
+
#
|
24
|
+
# This syntax was derived from Pete Yandell's machinist.
|
25
|
+
module Sham
|
26
|
+
module Sham #:nodoc:
|
27
|
+
def self.method_missing(name, *args, &block)
|
28
|
+
if block_given?
|
29
|
+
start_value = args.first
|
30
|
+
FactoryGirl.sequences[name] = Sequence.new(start_value || 1, &block)
|
31
|
+
else
|
32
|
+
FactoryGirl.sequences[name].next
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# overrides name on Module
|
37
|
+
def self.name(&block)
|
38
|
+
method_missing('name', &block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
include FactoryGirl::Syntax::Sham
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Factory, "aliases" do
|
4
|
+
|
5
|
+
it "should include an attribute as an alias for itself by default" do
|
6
|
+
FactoryGirl.aliases_for(:test).should include(:test)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should include the root of a foreign key as an alias by default" do
|
10
|
+
FactoryGirl.aliases_for(:test_id).should include(:test)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should include an attribute's foreign key as an alias by default" do
|
14
|
+
FactoryGirl.aliases_for(:test).should include(:test_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should NOT include an attribute as an alias when it starts with underscore" do
|
18
|
+
FactoryGirl.aliases_for(:_id).should_not include(:id)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "after adding an alias" do
|
22
|
+
|
23
|
+
before do
|
24
|
+
Factory.alias(/(.*)_suffix/, '\1')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return the alias in the aliases list" do
|
28
|
+
FactoryGirl.aliases_for(:test_suffix).should include(:test)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FactoryGirl::Attribute::Association do
|
4
|
+
before do
|
5
|
+
@name = :author
|
6
|
+
@factory = :user
|
7
|
+
@overrides = { :first_name => 'John' }
|
8
|
+
@attr = FactoryGirl::Attribute::Association.new(@name, @factory, @overrides)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have a name" do
|
12
|
+
@attr.name.should == @name
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have a factory" do
|
16
|
+
@attr.factory.should == @factory
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should tell the proxy to associate when being added to a proxy" do
|
20
|
+
proxy = "proxy"
|
21
|
+
stub(proxy).associate
|
22
|
+
@attr.add_to(proxy)
|
23
|
+
proxy.should have_received.associate(@name, @factory, @overrides)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should convert names to symbols" do
|
27
|
+
FactoryGirl::Attribute::Association.new('name', :user, {}).name.should == :name
|
28
|
+
end
|
29
|
+
end
|