vitalish-factory_girl 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +67 -121
- data/Rakefile +103 -29
- data/VERSION +1 -0
- data/lib/factory_girl/aliases.rb +32 -3
- data/lib/factory_girl/attribute/association.rb +1 -1
- data/lib/factory_girl/attribute/callback.rb +1 -1
- data/lib/factory_girl/attribute/dynamic.rb +3 -3
- data/lib/factory_girl/attribute/static.rb +1 -1
- data/lib/factory_girl/attribute.rb +3 -3
- data/lib/factory_girl/factory.rb +354 -164
- data/lib/factory_girl/proxy/attributes_for.rb +1 -1
- data/lib/factory_girl/proxy/build.rb +5 -7
- data/lib/factory_girl/proxy/create.rb +3 -2
- data/lib/factory_girl/proxy/stub.rb +5 -19
- data/lib/factory_girl/proxy.rb +23 -7
- data/lib/factory_girl/sequence.rb +40 -5
- data/lib/factory_girl/step_definitions.rb +13 -19
- data/lib/factory_girl/syntax/blueprint.rb +5 -5
- data/lib/factory_girl/syntax/generate.rb +8 -13
- data/lib/factory_girl/syntax/make.rb +7 -9
- data/lib/factory_girl/syntax/sham.rb +8 -11
- data/lib/factory_girl/syntax.rb +7 -7
- data/lib/factory_girl.rb +19 -8
- data/spec/factory_girl/aliases_spec.rb +5 -9
- data/spec/factory_girl/attribute/association_spec.rb +4 -4
- data/spec/factory_girl/attribute/callback_spec.rb +4 -4
- data/spec/factory_girl/attribute/dynamic_spec.rb +10 -21
- data/spec/factory_girl/attribute/static_spec.rb +6 -6
- data/spec/factory_girl/attribute_spec.rb +6 -6
- data/spec/factory_girl/factory_spec.rb +465 -305
- data/spec/factory_girl/proxy/attributes_for_spec.rb +3 -3
- data/spec/factory_girl/proxy/build_spec.rb +13 -18
- data/spec/factory_girl/proxy/create_spec.rb +13 -18
- data/spec/factory_girl/proxy/stub_spec.rb +6 -7
- data/spec/factory_girl/proxy_spec.rb +3 -3
- data/spec/factory_girl/sequence_spec.rb +39 -16
- data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
- data/spec/factory_girl/syntax/generate_spec.rb +57 -0
- data/spec/factory_girl/syntax/make_spec.rb +35 -0
- data/spec/factory_girl/syntax/sham_spec.rb +35 -0
- data/spec/integration_spec.rb +304 -0
- data/spec/models.rb +43 -0
- data/spec/spec_helper.rb +10 -85
- metadata +15 -3
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Factory::Proxy::AttributesFor do
|
4
4
|
before do
|
5
|
-
@proxy =
|
5
|
+
@proxy = Factory::Proxy::AttributesFor.new(@class)
|
6
6
|
end
|
7
7
|
|
8
8
|
describe "when asked to associate with another factory" do
|
@@ -1,16 +1,18 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Factory::Proxy::Build do
|
4
4
|
before do
|
5
5
|
@class = Class.new
|
6
6
|
@instance = "built-instance"
|
7
|
+
@association = "associated-instance"
|
7
8
|
|
8
9
|
stub(@class).new { @instance }
|
9
10
|
stub(@instance).attribute { 'value' }
|
11
|
+
stub(Factory).create { @association }
|
10
12
|
stub(@instance, :attribute=)
|
11
13
|
stub(@instance, :owner=)
|
12
14
|
|
13
|
-
@proxy =
|
15
|
+
@proxy = Factory::Proxy::Build.new(@class)
|
14
16
|
end
|
15
17
|
|
16
18
|
it "should instantiate the class" do
|
@@ -19,16 +21,11 @@ describe FactoryGirl::Proxy::Build do
|
|
19
21
|
|
20
22
|
describe "when asked to associate with another factory" do
|
21
23
|
before do
|
22
|
-
@
|
23
|
-
@associated_factory = "associated-factory"
|
24
|
-
stub(FactoryGirl).factory_by_name { @associated_factory }
|
25
|
-
stub(@associated_factory).run { @association }
|
26
|
-
@overrides = { 'attr' => 'value' }
|
27
|
-
@proxy.associate(:owner, :user, @overrides)
|
24
|
+
@proxy.associate(:owner, :user, {})
|
28
25
|
end
|
29
26
|
|
30
27
|
it "should create the associated instance" do
|
31
|
-
|
28
|
+
Factory.should have_received.create(:user, {})
|
32
29
|
end
|
33
30
|
|
34
31
|
it "should set the associated instance" do
|
@@ -36,14 +33,12 @@ describe FactoryGirl::Proxy::Build do
|
|
36
33
|
end
|
37
34
|
end
|
38
35
|
|
39
|
-
it "should
|
40
|
-
association =
|
41
|
-
|
42
|
-
stub(
|
43
|
-
|
44
|
-
|
45
|
-
@proxy.association(:user, overrides).should == association
|
46
|
-
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
36
|
+
it "should call Factory.create when building an association" do
|
37
|
+
association = 'association'
|
38
|
+
attribs = { :first_name => 'Billy' }
|
39
|
+
stub(Factory).create { association }
|
40
|
+
@proxy.association(:user, attribs).should == association
|
41
|
+
Factory.should have_received.create(:user, attribs)
|
47
42
|
end
|
48
43
|
|
49
44
|
it "should return the built instance when asked for the result" do
|
@@ -1,17 +1,19 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Factory::Proxy::Create do
|
4
4
|
before do
|
5
5
|
@class = Class.new
|
6
6
|
@instance = "built-instance"
|
7
|
+
@association = "associated-instance"
|
7
8
|
|
8
9
|
stub(@class).new { @instance }
|
10
|
+
stub(Factory).create { @association }
|
9
11
|
stub(@instance).attribute { 'value' }
|
10
12
|
stub(@instance, :attribute=)
|
11
13
|
stub(@instance, :owner=)
|
12
14
|
stub(@instance).save!
|
13
15
|
|
14
|
-
@proxy =
|
16
|
+
@proxy = Factory::Proxy::Create.new(@class)
|
15
17
|
end
|
16
18
|
|
17
19
|
it "should instantiate the class" do
|
@@ -20,16 +22,11 @@ describe FactoryGirl::Proxy::Create do
|
|
20
22
|
|
21
23
|
describe "when asked to associate with another factory" do
|
22
24
|
before do
|
23
|
-
@
|
24
|
-
@associated_factory = "associated-factory"
|
25
|
-
stub(FactoryGirl).factory_by_name { @associated_factory }
|
26
|
-
stub(@associated_factory).run { @association }
|
27
|
-
@overrides = { 'attr' => 'value' }
|
28
|
-
@proxy.associate(:owner, :user, @overrides)
|
25
|
+
@proxy.associate(:owner, :user, {})
|
29
26
|
end
|
30
27
|
|
31
28
|
it "should create the associated instance" do
|
32
|
-
|
29
|
+
Factory.should have_received.create(:user, {})
|
33
30
|
end
|
34
31
|
|
35
32
|
it "should set the associated instance" do
|
@@ -37,14 +34,12 @@ describe FactoryGirl::Proxy::Create do
|
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
40
|
-
it "should
|
41
|
-
association =
|
42
|
-
|
43
|
-
stub(
|
44
|
-
|
45
|
-
|
46
|
-
@proxy.association(:user, overrides).should == association
|
47
|
-
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
37
|
+
it "should call Factory.create when building an association" do
|
38
|
+
association = 'association'
|
39
|
+
attribs = { :first_name => 'Billy' }
|
40
|
+
stub(Factory).create { association }
|
41
|
+
@proxy.association(:user, attribs).should == association
|
42
|
+
Factory.should have_received.create(:user, attribs)
|
48
43
|
end
|
49
44
|
|
50
45
|
describe "when asked for the result" do
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Factory::Proxy::Stub do
|
4
4
|
before do
|
5
5
|
@class = "class"
|
6
6
|
@instance = "instance"
|
@@ -9,7 +9,7 @@ describe FactoryGirl::Proxy::Stub do
|
|
9
9
|
stub(@instance).id { 42 }
|
10
10
|
stub(@instance).reload { @instance.connection.reload }
|
11
11
|
|
12
|
-
@stub =
|
12
|
+
@stub = Factory::Proxy::Stub.new(@class)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should not be a new record" do
|
@@ -23,14 +23,13 @@ describe FactoryGirl::Proxy::Stub do
|
|
23
23
|
describe "when a user factory exists" do
|
24
24
|
before do
|
25
25
|
@user = "user"
|
26
|
-
stub(
|
27
|
-
@associated_factory = 'associate-factory'
|
26
|
+
stub(Factory).stub(:user, {}) { @user }
|
28
27
|
end
|
29
28
|
|
30
29
|
describe "when asked to associate with another factory" do
|
31
30
|
before do
|
32
31
|
stub(@instance).owner { @user }
|
33
|
-
mock(
|
32
|
+
mock(Factory).stub(:user, {}) { @user }
|
34
33
|
mock(@stub).set(:owner, @user)
|
35
34
|
|
36
35
|
@stub.associate(:owner, :user, {})
|
@@ -42,7 +41,7 @@ describe FactoryGirl::Proxy::Stub do
|
|
42
41
|
end
|
43
42
|
|
44
43
|
it "should return the association when building one" do
|
45
|
-
mock(
|
44
|
+
mock(Factory).create.never
|
46
45
|
@stub.association(:user).should == @user
|
47
46
|
end
|
48
47
|
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Factory::Proxy do
|
4
4
|
before do
|
5
|
-
@proxy =
|
5
|
+
@proxy = Factory::Proxy.new(Class.new)
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should do nothing when asked to set an attribute to a value" do
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe
|
4
|
-
describe "a
|
3
|
+
describe Factory::Sequence do
|
4
|
+
describe "a sequence" do
|
5
5
|
before do
|
6
|
-
@sequence =
|
6
|
+
@sequence = Factory::Sequence.new {|n| "=#{n}" }
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should start with a value of 1" do
|
@@ -20,24 +20,47 @@ describe FactoryGirl::Sequence do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
24
|
-
describe "a
|
23
|
+
|
24
|
+
describe "defining a sequence" do
|
25
25
|
before do
|
26
|
-
@sequence =
|
26
|
+
@sequence = "sequence"
|
27
|
+
@name = :count
|
28
|
+
stub(Factory::Sequence).new { @sequence }
|
27
29
|
end
|
28
30
|
|
29
|
-
it "should
|
30
|
-
@sequence
|
31
|
+
it "should create a new sequence" do
|
32
|
+
mock(Factory::Sequence).new() { @sequence }
|
33
|
+
Factory.sequence(@name)
|
31
34
|
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
it "should use the supplied block as the sequence generator" do
|
37
|
+
stub(Factory::Sequence).new.yields(1)
|
38
|
+
yielded = false
|
39
|
+
Factory.sequence(@name) {|n| yielded = true }
|
40
|
+
(yielded).should be
|
41
|
+
end
|
42
|
+
end
|
37
43
|
|
38
|
-
|
39
|
-
|
40
|
-
|
44
|
+
describe "after defining a sequence" do
|
45
|
+
before do
|
46
|
+
@sequence = "sequence"
|
47
|
+
@name = :test
|
48
|
+
@value = '1 2 5'
|
49
|
+
|
50
|
+
stub(@sequence).next { @value }
|
51
|
+
stub(Factory::Sequence).new { @sequence }
|
52
|
+
|
53
|
+
Factory.sequence(@name) {}
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should call next on the sequence when sent next" do
|
57
|
+
mock(@sequence).next
|
58
|
+
|
59
|
+
Factory.next(@name)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return the value from the sequence" do
|
63
|
+
Factory.next(@name).should == @value
|
41
64
|
end
|
42
65
|
end
|
43
66
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/blueprint'
|
4
|
+
|
5
|
+
describe "a blueprint" do
|
6
|
+
before do
|
7
|
+
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
|
8
|
+
User.blueprint do
|
9
|
+
first_name { 'Bill' }
|
10
|
+
last_name { 'Nye' }
|
11
|
+
email { Factory.next(:email) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Factory.factories.clear
|
17
|
+
Factory.sequences.clear
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "after making an instance" do
|
21
|
+
before do
|
22
|
+
@instance = Factory(:user, :last_name => 'Rye')
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should use attributes from the blueprint" do
|
26
|
+
@instance.first_name.should == 'Bill'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should evaluate attribute blocks for each instance" do
|
30
|
+
@instance.email.should =~ /somebody\d+@example.com/
|
31
|
+
Factory(:user).email.should_not == @instance.email
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/generate'
|
4
|
+
|
5
|
+
describe "a factory using generate syntax" do
|
6
|
+
before do
|
7
|
+
Factory.define :user do |factory|
|
8
|
+
factory.first_name 'Bill'
|
9
|
+
factory.last_name 'Nye'
|
10
|
+
factory.email 'science@guys.net'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
Factory.factories.clear
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not raise an error when generating an invalid instance" do
|
19
|
+
lambda { User.generate(:first_name => nil) }.should_not raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise an error when forcefully generating an invalid instance" do
|
23
|
+
lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
|
24
|
+
end
|
25
|
+
|
26
|
+
%w(generate generate! spawn).each do |method|
|
27
|
+
it "should yield a generated instance when using #{method} with a block" do
|
28
|
+
saved_instance = nil
|
29
|
+
User.send(method) {|instance| saved_instance = instance }
|
30
|
+
saved_instance.should be_kind_of(User)
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "after generating an instance using #{method}" do
|
34
|
+
before do
|
35
|
+
@instance = User.send(method, :last_name => 'Rye')
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should use attributes from the factory" do
|
39
|
+
@instance.first_name.should == 'Bill'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should use attributes passed to generate" do
|
43
|
+
@instance.last_name.should == 'Rye'
|
44
|
+
end
|
45
|
+
|
46
|
+
if method == 'spawn'
|
47
|
+
it "should not save the record" do
|
48
|
+
@instance.should be_new_record
|
49
|
+
end
|
50
|
+
else
|
51
|
+
it "should save the record" do
|
52
|
+
@instance.should_not be_new_record
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/make'
|
4
|
+
|
5
|
+
describe "a factory using make syntax" do
|
6
|
+
before do
|
7
|
+
Factory.define :user do |factory|
|
8
|
+
factory.first_name 'Bill'
|
9
|
+
factory.last_name 'Nye'
|
10
|
+
factory.email 'science@guys.net'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
Factory.factories.clear
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "after making an instance" do
|
19
|
+
before do
|
20
|
+
@instance = User.make(:last_name => 'Rye')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should use attributes from the factory" do
|
24
|
+
@instance.first_name.should == 'Bill'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should use attributes passed to make" do
|
28
|
+
@instance.last_name.should == 'Rye'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should save the record" do
|
32
|
+
@instance.should_not be_new_record
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/sham'
|
4
|
+
|
5
|
+
describe "a factory using sham syntax" do
|
6
|
+
before do
|
7
|
+
Sham.name { "Name" }
|
8
|
+
Sham.email { "somebody#{rand(5)}@example.com" }
|
9
|
+
|
10
|
+
Factory.define :user do |factory|
|
11
|
+
factory.first_name { Sham.name }
|
12
|
+
factory.last_name { Sham.name }
|
13
|
+
factory.email { Sham.email }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
Factory.factories.clear
|
19
|
+
Factory.sequences.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "after making an instance" do
|
23
|
+
before do
|
24
|
+
@instance = Factory(:user, :last_name => 'Rye')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should support a sham called 'name'" do
|
28
|
+
@instance.first_name.should == 'Name'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should use the sham for the email" do
|
32
|
+
@instance.email.should =~ /somebody\d@example.com/
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,304 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe "integration" do
|
4
|
+
before do
|
5
|
+
Factory.define :user, :class => 'user' do |f|
|
6
|
+
f.first_name 'Jimi'
|
7
|
+
f.last_name 'Hendrix'
|
8
|
+
f.admin false
|
9
|
+
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
10
|
+
end
|
11
|
+
|
12
|
+
Factory.define Post, :default_strategy => :attributes_for do |f|
|
13
|
+
f.name 'Test Post'
|
14
|
+
f.association :author, :factory => :user
|
15
|
+
end
|
16
|
+
|
17
|
+
Factory.define :admin, :class => User do |f|
|
18
|
+
f.first_name 'Ben'
|
19
|
+
f.last_name 'Stein'
|
20
|
+
f.admin true
|
21
|
+
f.sequence(:username) { |n| "username#{n}" }
|
22
|
+
f.email { Factory.next(:email) }
|
23
|
+
end
|
24
|
+
|
25
|
+
Factory.define :sequence_abuser, :class => User do |f|
|
26
|
+
f.first_name { Factory.sequence(:email) }
|
27
|
+
end
|
28
|
+
|
29
|
+
Factory.define :guest, :parent => :user do |f|
|
30
|
+
f.last_name 'Anonymous'
|
31
|
+
f.username 'GuestUser'
|
32
|
+
end
|
33
|
+
|
34
|
+
Factory.define :user_with_callbacks, :parent => :user do |f|
|
35
|
+
f.after_stub {|u| u.first_name = 'Stubby' }
|
36
|
+
f.after_build {|u| u.first_name = 'Buildy' }
|
37
|
+
f.after_create {|u| u.last_name = 'Createy' }
|
38
|
+
end
|
39
|
+
|
40
|
+
Factory.define :user_with_inherited_callbacks, :parent => :user_with_callbacks do |f|
|
41
|
+
f.callback(:after_stub) {|u| u.last_name = 'Double-Stubby' }
|
42
|
+
end
|
43
|
+
|
44
|
+
Factory.define :business do |f|
|
45
|
+
f.name 'Supplier of Awesome'
|
46
|
+
f.association :owner, :factory => :user
|
47
|
+
end
|
48
|
+
|
49
|
+
Factory.sequence :email do |n|
|
50
|
+
"somebody#{n}@example.com"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
after do
|
55
|
+
Factory.factories.clear
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "a generated attributes hash" do
|
59
|
+
|
60
|
+
before do
|
61
|
+
@attrs = Factory.attributes_for(:user, :first_name => 'Bill')
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should assign all attributes" do
|
65
|
+
expected_attrs = [:admin, :email, :first_name, :last_name]
|
66
|
+
actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
|
67
|
+
actual_attrs.should == expected_attrs
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should correctly assign lazy, dependent attributes" do
|
71
|
+
@attrs[:email].should == "bill.hendrix@example.com"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should override attrbutes" do
|
75
|
+
@attrs[:first_name].should == 'Bill'
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not assign associations" do
|
79
|
+
Factory.attributes_for(:post)[:author].should be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "a built instance" do
|
85
|
+
|
86
|
+
before do
|
87
|
+
@instance = Factory.build(:post)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should not be saved" do
|
91
|
+
@instance.should be_new_record
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should assign associations" do
|
95
|
+
@instance.author.should be_kind_of(User)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should save associations" do
|
99
|
+
@instance.author.should_not be_new_record
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should not assign both an association and its foreign key" do
|
103
|
+
Factory.build(:post, :author_id => 1).author_id.should == 1
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "a created instance" do
|
109
|
+
|
110
|
+
before do
|
111
|
+
@instance = Factory.create('post')
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should be saved" do
|
115
|
+
@instance.should_not be_new_record
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should assign associations" do
|
119
|
+
@instance.author.should be_kind_of(User)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should save associations" do
|
123
|
+
@instance.author.should_not be_new_record
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "a generated stub instance" do
|
129
|
+
|
130
|
+
before do
|
131
|
+
@stub = Factory.stub(:user, :first_name => 'Bill')
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should assign all attributes" do
|
135
|
+
[:admin, :email, :first_name, :last_name].each do |attr|
|
136
|
+
@stub.send(attr).should_not be_nil
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should correctly assign attributes" do
|
141
|
+
@stub.email.should == "bill.hendrix@example.com"
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should override attributes" do
|
145
|
+
@stub.first_name.should == 'Bill'
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should assign associations" do
|
149
|
+
Factory.stub(:post).author.should_not be_nil
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should have an id" do
|
153
|
+
@stub.id.should > 0
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have unique IDs" do
|
157
|
+
@other_stub = Factory.stub(:user)
|
158
|
+
@stub.id.should_not == @other_stub.id
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should not be considered a new record" do
|
162
|
+
@stub.should_not be_new_record
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should raise exception if connection to the database is attempted" do
|
166
|
+
lambda { @stub.connection }.should raise_error(RuntimeError)
|
167
|
+
lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
|
168
|
+
lambda { @stub.reload }.should raise_error(RuntimeError)
|
169
|
+
lambda { @stub.destroy }.should raise_error(RuntimeError)
|
170
|
+
lambda { @stub.save }.should raise_error(RuntimeError)
|
171
|
+
lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe "an instance generated by a factory with a custom class name" do
|
176
|
+
|
177
|
+
before do
|
178
|
+
@instance = Factory.create(:admin)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should use the correct class name" do
|
182
|
+
@instance.should be_kind_of(User)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should use the correct factory definition" do
|
186
|
+
@instance.should be_admin
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "an instance generated by a factory that inherits from another factory" do
|
192
|
+
before do
|
193
|
+
@instance = Factory.create(:guest)
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should use the class name of the parent factory" do
|
197
|
+
@instance.should be_kind_of(User)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should have attributes of the parent" do
|
201
|
+
@instance.first_name.should == 'Jimi'
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should have attributes defined in the factory itself" do
|
205
|
+
@instance.username.should == 'GuestUser'
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should have attributes that have been overriden" do
|
209
|
+
@instance.last_name.should == 'Anonymous'
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe "an attribute generated by a sequence" do
|
214
|
+
|
215
|
+
before do
|
216
|
+
@email = Factory.attributes_for(:admin)[:email]
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should match the correct format" do
|
220
|
+
@email.should =~ /^somebody\d+@example\.com$/
|
221
|
+
end
|
222
|
+
|
223
|
+
describe "after the attribute has already been generated once" do
|
224
|
+
|
225
|
+
before do
|
226
|
+
@another_email = Factory.attributes_for(:admin)[:email]
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should match the correct format" do
|
230
|
+
@email.should =~ /^somebody\d+@example\.com$/
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should not be the same as the first generated value" do
|
234
|
+
@another_email.should_not == @email
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
describe "an attribute generated by an in-line sequence" do
|
242
|
+
|
243
|
+
before do
|
244
|
+
@username = Factory.attributes_for(:admin)[:username]
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should match the correct format" do
|
248
|
+
@username.should =~ /^username\d+$/
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "after the attribute has already been generated once" do
|
252
|
+
|
253
|
+
before do
|
254
|
+
@another_username = Factory.attributes_for(:admin)[:username]
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should match the correct format" do
|
258
|
+
@username.should =~ /^username\d+$/
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should not be the same as the first generated value" do
|
262
|
+
@another_username.should_not == @username
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
describe "a factory with a default strategy specified" do
|
270
|
+
it "should generate instances according to the strategy" do
|
271
|
+
Factory(:post).should be_kind_of(Hash)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should raise Factory::SequenceAbuseError" do
|
276
|
+
lambda {
|
277
|
+
Factory(:sequence_abuser)
|
278
|
+
}.should raise_error(Factory::SequenceAbuseError)
|
279
|
+
end
|
280
|
+
|
281
|
+
describe "an instance with callbacks" do
|
282
|
+
it "should run the after_stub callback when stubbing" do
|
283
|
+
@user = Factory.stub(:user_with_callbacks)
|
284
|
+
@user.first_name.should == 'Stubby'
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should run the after_build callback when building" do
|
288
|
+
@user = Factory.build(:user_with_callbacks)
|
289
|
+
@user.first_name.should == 'Buildy'
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should run both the after_build and after_create callbacks when creating" do
|
293
|
+
@user = Factory(:user_with_callbacks)
|
294
|
+
@user.first_name.should == 'Buildy'
|
295
|
+
@user.last_name.should == 'Createy'
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should run both the after_stub callback on the factory and the inherited after_stub callback" do
|
299
|
+
@user = Factory.stub(:user_with_inherited_callbacks)
|
300
|
+
@user.first_name.should == 'Stubby'
|
301
|
+
@user.last_name.should == 'Double-Stubby'
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|