freegenie-factory_girl 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +236 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl/aliases.rb +50 -0
  7. data/lib/factory_girl/attribute/association.rb +20 -0
  8. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  9. data/lib/factory_girl/attribute/static.rb +17 -0
  10. data/lib/factory_girl/attribute.rb +29 -0
  11. data/lib/factory_girl/factory.rb +412 -0
  12. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  13. data/lib/factory_girl/proxy/build.rb +29 -0
  14. data/lib/factory_girl/proxy/create.rb +10 -0
  15. data/lib/factory_girl/proxy/stub.rb +49 -0
  16. data/lib/factory_girl/proxy.rb +62 -0
  17. data/lib/factory_girl/sequence.rb +63 -0
  18. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  19. data/lib/factory_girl/syntax/generate.rb +68 -0
  20. data/lib/factory_girl/syntax/make.rb +39 -0
  21. data/lib/factory_girl/syntax/sham.rb +42 -0
  22. data/lib/factory_girl/syntax.rb +12 -0
  23. data/lib/factory_girl.rb +39 -0
  24. data/spec/factory_girl/aliases_spec.rb +29 -0
  25. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  26. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  27. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  28. data/spec/factory_girl/attribute_spec.rb +30 -0
  29. data/spec/factory_girl/factory_spec.rb +525 -0
  30. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  31. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  32. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  33. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  34. data/spec/factory_girl/proxy_spec.rb +28 -0
  35. data/spec/factory_girl/sequence_spec.rb +66 -0
  36. data/spec/factory_girl/syntax/blueprint_spec.rb +35 -0
  37. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  38. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  39. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  40. data/spec/integration_spec.rb +265 -0
  41. data/spec/models.rb +43 -0
  42. data/spec/spec_helper.rb +18 -0
  43. data/test/factory_girl_test.rb +28 -0
  44. metadata +118 -0
@@ -0,0 +1,68 @@
1
+ class Factory
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
+ # Factory.define :user do |factory|
11
+ # factory.name 'Billy Bob'
12
+ # factory.email 'billy@bob.example.com'
13
+ # end
14
+ #
15
+ # # Creates a saved instance without raising (same as saving the result
16
+ # # of Factory.build)
17
+ # User.generate(:name => 'Johnny')
18
+ #
19
+ # # Creates a saved instance and raises when invalid (same as
20
+ # # Factory.create)
21
+ # User.generate!
22
+ #
23
+ # # Creates an unsaved instance (same as Factory.build)
24
+ # User.spawn
25
+ #
26
+ # # Creates an instance and yields it to the passed block
27
+ # User.generate do |user|
28
+ # # ...do something with user...
29
+ # end
30
+ #
31
+ # This syntax was derived from Rick Bradley and Yossef Mendelssohn's
32
+ # object_daddy.
33
+ module Generate
34
+ module ActiveRecord #:nodoc:
35
+
36
+ def self.included(base) # :nodoc:
37
+ base.extend ClassMethods
38
+ end
39
+
40
+ module ClassMethods #:nodoc:
41
+
42
+ def generate(overrides = {}, &block)
43
+ instance = Factory.build(name.underscore, overrides)
44
+ instance.save
45
+ yield(instance) if block_given?
46
+ instance
47
+ end
48
+
49
+ def generate!(overrides = {}, &block)
50
+ instance = Factory.create(name.underscore, overrides)
51
+ yield(instance) if block_given?
52
+ instance
53
+ end
54
+
55
+ def spawn(overrides = {}, &block)
56
+ instance = Factory.build(name.underscore, overrides)
57
+ yield(instance) if block_given?
58
+ instance
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ ActiveRecord::Base.send(:include, Factory::Syntax::Generate::ActiveRecord)
@@ -0,0 +1,39 @@
1
+ class Factory
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
+ # Factory.define :user do |factory|
12
+ # factory.name 'Billy Bob'
13
+ # factory.email 'billy@bob.example.com'
14
+ # end
15
+ #
16
+ # User.make(:name => 'Johnny')
17
+ #
18
+ # This syntax was derived from Pete Yandell's machinist.
19
+ module Make
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 make(overrides = {})
29
+ Factory.create(name.underscore, overrides)
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveRecord::Base.send(:include, Factory::Syntax::Make::ActiveRecord)
@@ -0,0 +1,42 @@
1
+ class Factory
2
+ module Syntax
3
+
4
+ # Adds a Sham module, which provides an alternate interface to
5
+ # Factory::Sequence.
6
+ #
7
+ # Usage:
8
+ #
9
+ # require 'factory_girl/syntax/sham'
10
+ #
11
+ # Sham.email {|n| "somebody#{n}@example.com" }
12
+ #
13
+ # Factory.define :user do |factory|
14
+ # factory.email { Sham.email }
15
+ # end
16
+ #
17
+ # Note that you can also use Faker, but it is recommended that you simply
18
+ # use a sequence as in the above example, as factory_girl does not provide
19
+ # protection against duplication in randomized sequences, and a randomized
20
+ # value does not provide any tangible benefits over an ascending sequence.
21
+ #
22
+ # This syntax was derived from Pete Yandell's machinist.
23
+ module Sham
24
+ module Sham #:nodoc:
25
+ def self.method_missing(name, &block)
26
+ if block_given?
27
+ Factory.sequence(name, &block)
28
+ else
29
+ Factory.next(name)
30
+ end
31
+ end
32
+
33
+ # overrides name on Module
34
+ def self.name(&block)
35
+ method_missing('name', &block)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ include Factory::Syntax::Sham
@@ -0,0 +1,12 @@
1
+ class Factory
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 Factory::Syntax
4
+ # modules:
5
+ #
6
+ # * Factory::Syntax::Blueprint: definition syntax similar to Machinist
7
+ # * Factory::Syntax::Generate: usage syntax similar to Object Daddy
8
+ # * Factory::Syntax::Make: usage syntax similar to Machinist
9
+ # * Factory::Syntax::Sham: sequence syntax similar to Machinist
10
+ module Syntax
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support'
2
+ require 'factory_girl/proxy'
3
+ require 'factory_girl/proxy/build'
4
+ require 'factory_girl/proxy/create'
5
+ require 'factory_girl/proxy/attributes_for'
6
+ require 'factory_girl/proxy/stub'
7
+ require 'factory_girl/proxy/find_or_create'
8
+ require 'factory_girl/factory'
9
+ require 'factory_girl/attribute'
10
+ require 'factory_girl/attribute/static'
11
+ require 'factory_girl/attribute/dynamic'
12
+ require 'factory_girl/attribute/association'
13
+ require 'factory_girl/sequence'
14
+ require 'factory_girl/aliases'
15
+
16
+
17
+ require 'factory_girl/activerecord'
18
+
19
+
20
+ # Shortcut for Factory.default_strategy.
21
+ #
22
+ # Example:
23
+ # Factory(:user, :name => 'Joe')
24
+ def Factory (name, attrs = {})
25
+ Factory.default_strategy(name, attrs)
26
+ end
27
+
28
+ if defined? Rails.configuration
29
+ Rails.configuration.after_initialize do
30
+ Factory.definition_file_paths = [
31
+ File.join(RAILS_ROOT, 'test', 'factories'),
32
+ File.join(RAILS_ROOT, 'spec', 'factories')
33
+ ]
34
+ Factory.find_definitions
35
+ end
36
+ else
37
+ Factory.find_definitions
38
+ end
39
+
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory, "aliases" do
4
+
5
+ it "should include an attribute as an alias for itself by default" do
6
+ Factory.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
+ Factory.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
+ Factory.aliases_for(:test).should include(:test_id)
15
+ end
16
+
17
+ describe "after adding an alias" do
18
+
19
+ before do
20
+ Factory.alias(/(.*)_suffix/, '\1')
21
+ end
22
+
23
+ it "should return the alias in the aliases list" do
24
+ Factory.aliases_for(:test_suffix).should include(:test)
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Attribute::Association do
4
+ before do
5
+ @name = :author
6
+ @factory = :user
7
+ @overrides = { :first_name => 'John' }
8
+ @attr = Factory::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
+ Factory::Attribute::Association.new('name', :user, {}).name.should == :name
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Attribute::Dynamic do
4
+ before do
5
+ @name = :first_name
6
+ @block = lambda { 'value' }
7
+ @attr = Factory::Attribute::Dynamic.new(@name, @block)
8
+ end
9
+
10
+ it "should have a name" do
11
+ @attr.name.should == @name
12
+ end
13
+
14
+ it "should call the block to set a value" do
15
+ @proxy = "proxy"
16
+ stub(@proxy).set
17
+ @attr.add_to(@proxy)
18
+ @proxy.should have_received.set(@name, 'value')
19
+ end
20
+
21
+ it "should yield the proxy to the block when adding its value to a proxy" do
22
+ @block = lambda {|a| a }
23
+ @attr = Factory::Attribute::Dynamic.new(:user, @block)
24
+ @proxy = "proxy"
25
+ stub(@proxy).set
26
+ @attr.add_to(@proxy)
27
+ @proxy.should have_received.set(:user, @proxy)
28
+ end
29
+
30
+ it "should raise an error when defining an attribute writer" do
31
+ lambda {
32
+ Factory::Attribute::Dynamic.new('test=', nil)
33
+ }.should raise_error(Factory::AttributeDefinitionError)
34
+ end
35
+
36
+ it "should raise an error when returning a sequence" do
37
+ stub(Factory).sequence { Factory::Sequence.new }
38
+ block = lambda { Factory.sequence(:email) }
39
+ attr = Factory::Attribute::Dynamic.new(:email, block)
40
+ proxy = stub!.set.subject
41
+ lambda {
42
+ attr.add_to(proxy)
43
+ }.should raise_error(Factory::SequenceAbuseError)
44
+ end
45
+
46
+ it "should convert names to symbols" do
47
+ Factory::Attribute::Dynamic.new('name', nil).name.should == :name
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Attribute::Static do
4
+ before do
5
+ @name = :first_name
6
+ @value = 'John'
7
+ @attr = Factory::Attribute::Static.new(@name, @value)
8
+ end
9
+
10
+ it "should have a name" do
11
+ @attr.name.should == @name
12
+ end
13
+
14
+ it "should set its static value on a proxy" do
15
+ @proxy = "proxy"
16
+ mock(@proxy).set(@name, @value)
17
+ @attr.add_to(@proxy)
18
+ end
19
+
20
+ it "should raise an error when defining an attribute writer" do
21
+ lambda {
22
+ Factory::Attribute::Static.new('test=', nil)
23
+ }.should raise_error(Factory::AttributeDefinitionError)
24
+ end
25
+
26
+ it "should convert names to symbols" do
27
+ Factory::Attribute::Static.new('name', nil).name.should == :name
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory::Attribute do
4
+ before do
5
+ @name = :user
6
+ @attr = Factory::Attribute.new(@name)
7
+ end
8
+
9
+ it "should have a name" do
10
+ @attr.name.should == @name
11
+ end
12
+
13
+ it "should do nothing when being added to a proxy" do
14
+ @proxy = "proxy"
15
+ stub(@proxy).set
16
+ @attr.add_to(@proxy)
17
+ @proxy.should have_received.set.never
18
+ end
19
+
20
+ it "should raise an error when defining an attribute writer" do
21
+ error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
22
+ lambda {
23
+ Factory::Attribute.new('test=')
24
+ }.should raise_error(Factory::AttributeDefinitionError, error_message)
25
+ end
26
+
27
+ it "should convert names to symbols" do
28
+ Factory::Attribute.new('name').name.should == :name
29
+ end
30
+ end