factory_girl 2.0.0.beta1 → 2.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. data/Appraisals +12 -0
  2. data/{CONTRIBUTION_GUIDELINES.rdoc → CONTRIBUTION_GUIDELINES.md} +3 -3
  3. data/GETTING_STARTED.md +246 -0
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +62 -0
  6. data/README.md +64 -0
  7. data/Rakefile +15 -30
  8. data/features/factory_girl_steps.feature +26 -0
  9. data/features/step_definitions/database_steps.rb +2 -0
  10. data/features/support/env.rb +0 -9
  11. data/features/support/factories.rb +15 -0
  12. data/features/support/test.db +0 -0
  13. data/lib/factory_girl.rb +2 -0
  14. data/lib/factory_girl/definition_proxy.rb +10 -43
  15. data/lib/factory_girl/factory.rb +48 -17
  16. data/lib/factory_girl/proxy.rb +3 -3
  17. data/lib/factory_girl/proxy/attributes_for.rb +1 -1
  18. data/lib/factory_girl/proxy/build.rb +3 -3
  19. data/lib/factory_girl/proxy/create.rb +6 -2
  20. data/lib/factory_girl/proxy/stub.rb +3 -3
  21. data/lib/factory_girl/registry.rb +59 -0
  22. data/lib/factory_girl/sequence.rb +23 -9
  23. data/lib/factory_girl/step_definitions.rb +16 -9
  24. data/lib/factory_girl/syntax/blueprint.rb +2 -2
  25. data/lib/factory_girl/syntax/default.rb +5 -3
  26. data/lib/factory_girl/syntax/generate.rb +6 -6
  27. data/lib/factory_girl/syntax/make.rb +2 -2
  28. data/lib/factory_girl/syntax/methods.rb +75 -0
  29. data/lib/factory_girl/syntax/sham.rb +2 -2
  30. data/lib/factory_girl/syntax/vintage.rb +16 -79
  31. data/lib/factory_girl/version.rb +1 -1
  32. data/spec/acceptance/acceptance_helper.rb +7 -12
  33. data/spec/acceptance/attribute_aliases_spec.rb +26 -0
  34. data/spec/acceptance/attributes_for_spec.rb +48 -0
  35. data/spec/acceptance/build_spec.rb +35 -0
  36. data/spec/acceptance/build_stubbed_spec.rb +79 -0
  37. data/spec/acceptance/callbacks_spec.rb +42 -0
  38. data/spec/acceptance/create_spec.rb +67 -0
  39. data/spec/acceptance/default_strategy_spec.rb +27 -0
  40. data/spec/acceptance/definition_spec.rb +28 -0
  41. data/spec/acceptance/parent_spec.rb +39 -0
  42. data/spec/acceptance/sequence_spec.rb +32 -0
  43. data/spec/acceptance/syntax/blueprint_spec.rb +7 -5
  44. data/spec/acceptance/syntax/generate_spec.rb +10 -4
  45. data/spec/acceptance/syntax/make_spec.rb +7 -4
  46. data/spec/acceptance/syntax/sham_spec.rb +15 -8
  47. data/spec/acceptance/syntax/vintage_spec.rb +57 -17
  48. data/spec/factory_girl/attribute/dynamic_spec.rb +1 -1
  49. data/spec/factory_girl/definition_proxy_spec.rb +13 -11
  50. data/spec/factory_girl/factory_spec.rb +29 -52
  51. data/spec/factory_girl/find_definitions_spec.rb +34 -23
  52. data/spec/factory_girl/proxy/attributes_for_spec.rb +6 -6
  53. data/spec/factory_girl/proxy/build_spec.rb +4 -4
  54. data/spec/factory_girl/proxy/create_spec.rb +11 -3
  55. data/spec/factory_girl/proxy/stub_spec.rb +6 -6
  56. data/spec/factory_girl/proxy_spec.rb +1 -1
  57. data/spec/factory_girl/registry_spec.rb +92 -0
  58. data/spec/factory_girl/sequence_spec.rb +65 -8
  59. data/spec/spec_helper.rb +75 -29
  60. metadata +66 -43
  61. data/README.rdoc +0 -282
  62. data/spec/acceptance/acceptance_spec.rb +0 -288
  63. data/spec/acceptance/models.rb +0 -48
@@ -1,4 +1,4 @@
1
1
  module FactoryGirl
2
- VERSION = "2.0.0.beta1"
2
+ VERSION = "2.0.0.beta2"
3
3
  end
4
4
 
@@ -1,16 +1,11 @@
1
- case ENV['RAILS_VERSION']
2
- when '2.1' then
3
- gem 'activerecord', '~>2.1.0'
4
- when '3.0' then
5
- gem 'activerecord', '~>3.0.0'
6
- else
7
- gem 'activerecord', '~>2.3.0'
8
- end
9
-
10
1
  require 'active_record'
11
- require 'active_record/version'
12
2
 
13
- puts "Running specs using Rails #{ActiveRecord::VERSION::STRING}"
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => 'sqlite3',
5
+ :database => File.join(File.dirname(__FILE__), 'test.db')
6
+ )
14
7
 
15
- require 'acceptance/models'
8
+ RSpec.configure do |config|
9
+ config.include DefinesConstants
10
+ end
16
11
 
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "attribute aliases" do
5
+ before do
6
+ define_model('User')
7
+
8
+ define_model('Post', :user_id => :integer) do
9
+ belongs_to :user
10
+ end
11
+
12
+ FactoryGirl.define do
13
+ factory :user do
14
+ end
15
+
16
+ factory :post do
17
+ user
18
+ end
19
+ end
20
+ end
21
+
22
+ it "doesn't assign both an association and its foreign key" do
23
+ FactoryGirl.build(:post, :user_id => 1).user_id.should == 1
24
+ end
25
+ end
26
+
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "a generated attributes hash" do
5
+ include FactoryGirl::Syntax::Methods
6
+
7
+ before do
8
+ define_model('User')
9
+
10
+ define_model('Post', :title => :string,
11
+ :body => :string,
12
+ :summary => :string,
13
+ :user_id => :integer) do
14
+ belongs_to :user
15
+ end
16
+
17
+ FactoryGirl.define do
18
+ factory :user do
19
+ end
20
+
21
+ factory :post do
22
+ title { "default title" }
23
+ body { "default body" }
24
+ summary { title }
25
+ user
26
+ end
27
+ end
28
+ end
29
+
30
+ subject { attributes_for(:post, :title => 'overridden title') }
31
+
32
+ it "assigns an overridden value" do
33
+ subject[:title].should == "overridden title"
34
+ end
35
+
36
+ it "assigns a default value" do
37
+ subject[:body].should == "default body"
38
+ end
39
+
40
+ it "assigns a lazy, dependent attribute" do
41
+ subject[:summary].should == "overridden title"
42
+ end
43
+
44
+ it "doesn't assign associations" do
45
+ subject[:user_id].should be_nil
46
+ end
47
+ end
48
+
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "a built instance" do
5
+ include FactoryGirl::Syntax::Methods
6
+
7
+ before do
8
+ define_model('User')
9
+
10
+ define_model('Post', :user_id => :integer) do
11
+ belongs_to :user
12
+ end
13
+
14
+ FactoryGirl.define do
15
+ factory :user do
16
+ end
17
+
18
+ factory :post do
19
+ user
20
+ end
21
+ end
22
+ end
23
+
24
+ subject { build(:post) }
25
+
26
+ it "isn't saved" do
27
+ should be_new_record
28
+ end
29
+
30
+ it "assigns and saves associations" do
31
+ subject.user.should be_kind_of(User)
32
+ subject.user.should_not be_new_record
33
+ end
34
+ end
35
+
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "a generated stub instance" do
5
+ include FactoryGirl::Syntax::Methods
6
+
7
+ before do
8
+ define_model('User')
9
+
10
+ define_model('Post', :title => :string,
11
+ :body => :string,
12
+ :user_id => :integer) do
13
+ belongs_to :user
14
+ end
15
+
16
+ FactoryGirl.define do
17
+ factory :user do
18
+ end
19
+
20
+ factory :post do
21
+ title { "default title" }
22
+ body { "default body" }
23
+ user
24
+ end
25
+ end
26
+ end
27
+
28
+ subject { build_stubbed(:post, :title => 'overridden title') }
29
+
30
+ it "assigns a default attribute" do
31
+ subject.body.should == 'default body'
32
+ end
33
+
34
+ it "assigns an overridden attribute" do
35
+ subject.title.should == 'overridden title'
36
+ end
37
+
38
+ it "assigns associations" do
39
+ subject.user.should_not be_nil
40
+ end
41
+
42
+ it "has an id" do
43
+ subject.id.should > 0
44
+ end
45
+
46
+ it "generates unique ids" do
47
+ other_stub = build_stubbed(:post)
48
+ subject.id.should_not == other_stub.id
49
+ end
50
+
51
+ it "isn't a new record" do
52
+ should_not be_new_record
53
+ end
54
+
55
+ it "disables connection" do
56
+ lambda { subject.connection }.should raise_error(RuntimeError)
57
+ end
58
+
59
+ it "disables update_attribute" do
60
+ lambda { subject.update_attribute(:title, "value") }.should raise_error(RuntimeError)
61
+ end
62
+
63
+ it "disables reload" do
64
+ lambda { subject.reload }.should raise_error(RuntimeError)
65
+ end
66
+
67
+ it "disables destroy" do
68
+ lambda { subject.destroy }.should raise_error(RuntimeError)
69
+ end
70
+
71
+ it "disables save" do
72
+ lambda { subject.save }.should raise_error(RuntimeError)
73
+ end
74
+
75
+ it "disables increment" do
76
+ lambda { subject.increment!(:age) }.should raise_error(RuntimeError)
77
+ end
78
+ end
79
+
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "callbacks" do
5
+ before do
6
+ define_model("User", :first_name => :string, :last_name => :string)
7
+
8
+ FactoryGirl.define do
9
+ factory :user_with_callbacks, :class => :user do
10
+ after_stub { |user| user.first_name = 'Stubby' }
11
+ after_build { |user| user.first_name = 'Buildy' }
12
+ after_create { |user| user.last_name = 'Createy' }
13
+ end
14
+
15
+ factory :user_with_inherited_callbacks, :parent => :user_with_callbacks do
16
+ after_stub { |user| user.last_name = 'Double-Stubby' }
17
+ end
18
+ end
19
+ end
20
+
21
+ it "runs the after_stub callback when stubbing" do
22
+ user = FactoryGirl.build_stubbed(:user_with_callbacks)
23
+ user.first_name.should == 'Stubby'
24
+ end
25
+
26
+ it "runs the after_build callback when building" do
27
+ user = FactoryGirl.build(:user_with_callbacks)
28
+ user.first_name.should == 'Buildy'
29
+ end
30
+
31
+ it "runs both the after_build and after_create callbacks when creating" do
32
+ user = FactoryGirl.create(:user_with_callbacks)
33
+ user.first_name.should == 'Buildy'
34
+ user.last_name.should == 'Createy'
35
+ end
36
+
37
+ it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
38
+ user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
39
+ user.first_name.should == 'Stubby'
40
+ user.last_name.should == 'Double-Stubby'
41
+ end
42
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "a created instance" do
5
+ include FactoryGirl::Syntax::Methods
6
+
7
+ before do
8
+ define_model('User')
9
+
10
+ define_model('Post', :user_id => :integer) do
11
+ belongs_to :user
12
+ end
13
+
14
+ FactoryGirl.define do
15
+ factory :user do
16
+ end
17
+
18
+ factory :post do
19
+ user
20
+ end
21
+ end
22
+ end
23
+
24
+ subject { create('post') }
25
+
26
+ it "saves" do
27
+ should_not be_new_record
28
+ end
29
+
30
+ it "assigns and saves associations" do
31
+ subject.user.should be_kind_of(User)
32
+ subject.user.should_not be_new_record
33
+ end
34
+ end
35
+
36
+ describe "a custom create" do
37
+ include FactoryGirl::Syntax::Methods
38
+
39
+ before do
40
+ define_class('User') do
41
+ def initialize
42
+ @persisted = false
43
+ end
44
+
45
+ def persist
46
+ @persisted = true
47
+ end
48
+
49
+ def persisted?
50
+ @persisted
51
+ end
52
+ end
53
+
54
+ FactoryGirl.define do
55
+ factory :user do
56
+ to_create do |user|
57
+ user.persist
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ it "uses the custom create block instead of save" do
64
+ FactoryGirl.create(:user).should be_persisted
65
+ end
66
+ end
67
+
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "default strategy" do
5
+ it "uses create when not specified" do
6
+ define_model('User')
7
+
8
+ FactoryGirl.define do
9
+ factory :user do
10
+ end
11
+ end
12
+
13
+ Factory(:user).should_not be_new_record
14
+ end
15
+
16
+ it "can be overridden" do
17
+ define_model('User')
18
+
19
+ FactoryGirl.define do
20
+ factory :user, :default_strategy => :build do
21
+ end
22
+ end
23
+
24
+ Factory(:user).should be_new_record
25
+ end
26
+ end
27
+
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "an instance generated by a factory with a custom class name" do
5
+ before do
6
+ define_model("User", :admin => :boolean)
7
+
8
+ FactoryGirl.define do
9
+ factory :user do
10
+ end
11
+
12
+ factory :admin, :class => User do
13
+ admin { true }
14
+ end
15
+ end
16
+ end
17
+
18
+ subject { FactoryGirl.create(:admin) }
19
+
20
+ it "uses the correct class name" do
21
+ should be_kind_of(User)
22
+ end
23
+
24
+ it "uses the correct factory definition" do
25
+ should be_admin
26
+ end
27
+ end
28
+
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "an instance generated by a factory that inherits from another factory" do
5
+ before do
6
+ define_model("User", :name => :string, :admin => :boolean, :email => :string)
7
+
8
+ FactoryGirl.define do
9
+ factory :user do
10
+ name { "John" }
11
+ email { "john@example.com" }
12
+ end
13
+
14
+ factory :admin, :parent => :user do
15
+ admin { true }
16
+ email { "admin@example.com" }
17
+ end
18
+ end
19
+ end
20
+
21
+ subject { FactoryGirl.create(:admin) }
22
+
23
+ it "uses the parent build class" do
24
+ subject.should be_kind_of(User)
25
+ end
26
+
27
+ it "inherits attributes" do
28
+ subject.name.should == 'John'
29
+ end
30
+
31
+ it "has its own attributes" do
32
+ subject.should be_admin
33
+ end
34
+
35
+ it "overrides attributes" do
36
+ subject.email.should == 'admin@example.com'
37
+ end
38
+ end
39
+
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "sequences" do
5
+ it "generates several values in the correct format" do
6
+ FactoryGirl.define do
7
+ sequence :email do |n|
8
+ "somebody#{n}@example.com"
9
+ end
10
+ end
11
+
12
+ first_value = FactoryGirl.create(:email)
13
+ another_value = FactoryGirl.create(:email)
14
+
15
+ first_value.should =~ /^somebody\d+@example\.com$/
16
+ another_value.should =~ /^somebody\d+@example\.com$/
17
+ first_value.should_not == another_value
18
+ end
19
+
20
+ it "generates sequential numbers if no block is given" do
21
+ FactoryGirl.define do
22
+ sequence :order
23
+ end
24
+
25
+ first_value = FactoryGirl.create(:order)
26
+ another_value = FactoryGirl.create(:order)
27
+
28
+ first_value.should == 1
29
+ another_value.should == 2
30
+ first_value.should_not == another_value
31
+ end
32
+ end