fabrication 0.6.0 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,52 +2,61 @@ require 'spec_helper'
2
2
 
3
3
  describe Fabrication::Fabricator do
4
4
 
5
- it 'generates a new object every time' do
6
- f = Fabrication::Fabricator.new(:person) { first_name '1' }
7
- f.fabricate.should_not == f.fabricate
8
- end
5
+ subject { Fabrication::Fabricator }
6
+
7
+ describe ".define" do
9
8
 
10
- context 'with a plain old ruby object' do
9
+ before(:all) do
10
+ subject.define(:open_struct) do
11
+ first_name "Joe"
12
+ last_name { "Schmoe" }
13
+ end
14
+ end
15
+
16
+ it "returns the schematic" do
17
+ subject.define(:something, :class_name => :open_struct) do
18
+ name "Paul"
19
+ end.class.should == Fabrication::Schematic
20
+ end
11
21
 
12
- let(:fabricator) { Fabrication::Fabricator.new(:person) { first_name '1' } }
22
+ it "creates a schematic" do
23
+ subject.schematics[:open_struct].should be
24
+ end
13
25
 
14
- it 'fabricates a Person instance' do
15
- fabricator.fabricate.instance_of?(Person).should be_true
26
+ it "has the correct class" do
27
+ subject.schematics[:open_struct].klass.should == OpenStruct
16
28
  end
17
29
 
18
- it 'uses the base generator' do
19
- fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::Base).should be_true
30
+ it "has the attributes" do
31
+ subject.schematics[:open_struct].attributes.size.should == 2
20
32
  end
21
33
 
22
34
  end
23
35
 
24
- context 'with an activerecord object' do
36
+ describe ".generate" do
25
37
 
26
- before(:all) { TestMigration.up }
27
- after(:all) { TestMigration.down }
38
+ context 'without definitions' do
28
39
 
29
- let(:fabricator) { Fabrication::Fabricator.new(:company) { name '1' } }
40
+ before { subject.schematics.clear }
30
41
 
31
- it 'fabricates a Company instance' do
32
- fabricator.fabricate.instance_of?(Company).should be_true
33
- end
42
+ it "finds definitions if none exist" do
43
+ Fabrication::Support.should_receive(:find_definitions)
44
+ subject.generate(:object)
45
+ end
34
46
 
35
- it 'uses the activerecord generator' do
36
- fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::ActiveRecord).should be_true
37
47
  end
38
48
 
39
- end
49
+ context 'with definitions' do
40
50
 
41
- context 'with a mongoid document' do
51
+ it "raises an error if the object can't be fabricated" do
52
+ lambda { subject.generate(:somenonexistantclass) }.should raise_error(Fabrication::UnfabricatableError)
53
+ end
42
54
 
43
- let(:fabricator) { Fabrication::Fabricator.new(:author) { name "Seth Godin" } }
44
-
45
- it 'fabricates a Author instance' do
46
- fabricator.fabricate.instance_of?(Author).should be_true
47
- end
55
+ it 'generates a new object every time' do
56
+ subject.define(:person) { first_name '1' }
57
+ subject.generate(:person).should_not == subject.generate(:person)
58
+ end
48
59
 
49
- it 'uses the activerecord generator' do
50
- fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::Mongoid).should be_true
51
60
  end
52
61
 
53
62
  end
@@ -5,30 +5,45 @@ describe Fabrication::Generator::ActiveRecord do
5
5
  before(:all) { TestMigration.up }
6
6
  after(:all) { TestMigration.down }
7
7
 
8
- let(:schematic) do
9
- Fabrication::Schematic.new do
10
- name 'Company Name'
11
- city { |c| c.name.reverse.downcase.titleize }
12
- divisions(:count => 2) { |c, i| Fabricate(:division, :company => c, :name => "Division #{i}") }
8
+ describe ".supports?" do
9
+ subject { Fabrication::Generator::ActiveRecord }
10
+ it "returns true for active record objects" do
11
+ subject.supports?(Company).should be_true
12
+ end
13
+ it "returns false for non-active record objects" do
14
+ subject.supports?(Person).should be_false
13
15
  end
14
16
  end
15
17
 
16
- let(:generator) do
17
- Fabrication::Generator::ActiveRecord.new(Company, schematic)
18
- end
18
+ describe "#after_generation" do
19
+ let(:instance) { mock(:instance) }
20
+ let(:generator) { Fabrication::Generator::ActiveRecord.new(Object) }
19
21
 
20
- context 'active record object' do
22
+ before { generator.send(:instance=, instance) }
21
23
 
22
- let(:company) do
23
- generator.generate
24
+ it "saves with a true save flag" do
25
+ instance.should_receive(:save)
26
+ generator.send(:after_generation, {:save => true})
24
27
  end
25
28
 
26
- before(:all) do
27
- Fabricator(:division) do
28
- name "Division Name"
29
- end
29
+ it "does not save without a true save flag" do
30
+ instance.should_not_receive(:save)
31
+ generator.send(:after_generation, {})
32
+ end
33
+ end
34
+
35
+ describe "#generate" do
36
+
37
+ let(:attributes) do
38
+ Fabrication::Schematic.new(Company) do
39
+ name 'Company Name'
40
+ city { |c| c.name.downcase }
41
+ divisions(:count => 2) { |c, i| Division.create(:company => c, :name => "Division #{i}") }
42
+ end.attributes
30
43
  end
31
44
 
45
+ let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
46
+ let(:company) { generator.generate({:save => true}, attributes) }
32
47
  before { company }
33
48
 
34
49
  it 'does not persist the divisions immediately' do
@@ -36,7 +51,7 @@ describe Fabrication::Generator::ActiveRecord do
36
51
  end
37
52
 
38
53
  it 'passes the object to blocks' do
39
- company.city.should == 'Eman Ynapmoc'
54
+ company.city.should == 'company name'
40
55
  end
41
56
 
42
57
  it 'passes the object and count to blocks' do
@@ -68,18 +83,4 @@ describe Fabrication::Generator::ActiveRecord do
68
83
 
69
84
  end
70
85
 
71
- context 'with the build option' do
72
-
73
- let(:company) { Fabricate.build(:company, :name => "Epitaph") }
74
-
75
- it 'creates the record' do
76
- company.name.should == 'Epitaph'
77
- end
78
-
79
- it 'does not save it to the database' do
80
- Company.count.should == 0
81
- end
82
-
83
- end
84
-
85
86
  end
@@ -2,43 +2,48 @@ require 'spec_helper'
2
2
 
3
3
  describe Fabrication::Generator::Base do
4
4
 
5
- let(:schematic) do
6
- Fabrication::Schematic.new do
7
- first_name 'Some'
8
- last_name { |person| person.first_name.reverse.capitalize }
9
- age 40
10
- shoes(:count => 4) { |person, index| "shoe #{index}" }
5
+ describe ".supports?" do
6
+ subject { Fabrication::Generator::Base }
7
+ it "supports any object" do
8
+ subject.supports?(Object).should be_true
11
9
  end
12
10
  end
13
11
 
14
- let(:generator) { Fabrication::Generator::Base.new(Person, schematic) }
15
-
16
- let(:person) do
17
- generator.generate({:save => true}, {:first_name => 'Body'})
12
+ describe "#after_create" do
13
+ subject { Fabrication::Generator::Base.new(Object) }
14
+ before { subject.after_create { "something" } }
15
+ it "stores the after create block" do
16
+ Proc.should === subject.send(:after_create_block)
17
+ end
18
18
  end
19
19
 
20
- it 'passes the object to blocks' do
21
- person.last_name.should == 'Ydob'
22
- end
20
+ describe "#generate" do
23
21
 
24
- it 'passes the object and count to blocks' do
25
- person.shoes.should == (1..4).map { |i| "shoe #{i}" }
26
- end
22
+ let(:generator) { Fabrication::Generator::Base.new(Person) }
27
23
 
28
- it 'generates an instance' do
29
- person.instance_of?(Person).should be_true
30
- end
24
+ let(:attributes) do
25
+ Fabrication::Schematic.new(Person) do
26
+ first_name 'Guy'
27
+ shoes(:count => 4) do |person, index|
28
+ "#{person.first_name}'s shoe #{index}"
29
+ end
30
+ end.attributes
31
+ end
31
32
 
32
- it 'generates the first name immediately' do
33
- person.instance_variable_get(:@first_name).should == 'Body'
34
- end
33
+ let(:person) { generator.generate({}, attributes) }
35
34
 
36
- it 'generates the last name immediately' do
37
- person.instance_variable_get(:@last_name).should == 'Ydob'
38
- end
35
+ it 'generates an instance' do
36
+ person.instance_of?(Person).should be_true
37
+ end
38
+
39
+ it 'passes the object and count to blocks' do
40
+ person.shoes.should == (1..4).map { |i| "Guy's shoe #{i}" }
41
+ end
42
+
43
+ it 'sets the static value' do
44
+ person.instance_variable_get(:@first_name).should == 'Guy'
45
+ end
39
46
 
40
- it 'generates the age immediately' do
41
- person.instance_variable_get(:@age).should == 40
42
47
  end
43
48
 
44
49
  end
@@ -2,36 +2,31 @@ require 'spec_helper'
2
2
 
3
3
  describe Fabrication::Generator::Mongoid do
4
4
 
5
- before(:all) do
6
- Fabricator(:book) { title "book title" }
7
- end
8
-
9
- let(:schematic) do
10
- Fabrication::Schematic.new do
11
- name 'Name'
12
- handle { |author| author.name.downcase }
13
- books(:count => 3) { |author, index| Fabricate(:book, :title => "#{author.name} #{index}", :author => author) }
5
+ describe ".supports?" do
6
+ subject { Fabrication::Generator::Mongoid }
7
+ it "returns true for mongoid objects" do
8
+ subject.supports?(Author).should be_true
9
+ end
10
+ it "returns false for non-mongoid objects" do
11
+ subject.supports?(Person).should be_false
14
12
  end
15
13
  end
16
14
 
17
- let(:generator) { Fabrication::Generator::Mongoid.new(Author, schematic) }
18
-
19
- context 'mongoid object' do
15
+ describe "#after_generation" do
16
+ let(:instance) { mock(:instance) }
17
+ let(:generator) { Fabrication::Generator::Mongoid.new(Object) }
20
18
 
21
- before { generator.generate({:save => true}, :name => 'Something') }
19
+ before { generator.send(:instance=, instance) }
22
20
 
23
- it 'passes the object to blocks' do
24
- generator.generate.handle.should == "name"
21
+ it "saves with a true save flag" do
22
+ instance.should_receive(:save)
23
+ generator.send(:after_generation, {:save => true})
25
24
  end
26
25
 
27
- it 'passes the object and count to blocks' do
28
- generator.generate.books.map(&:title).should == ["Name 1","Name 2","Name 3"]
26
+ it "does not save without a true save flag" do
27
+ instance.should_not_receive(:save)
28
+ generator.send(:after_generation, {})
29
29
  end
30
-
31
- it 'persists the author upon creation' do
32
- Author.where(:name => 'Something').first.should be
33
- end
34
-
35
30
  end
36
31
 
37
32
  end
@@ -3,109 +3,111 @@ require 'spec_helper'
3
3
  describe Fabrication::Schematic do
4
4
 
5
5
  let(:schematic) do
6
- Fabrication::Schematic.new do
6
+ Fabrication::Schematic.new(OpenStruct) do
7
7
  name "Orgasmo"
8
8
  something(:param => 2) { "hi!" }
9
9
  another_thing { 25 }
10
10
  end
11
11
  end
12
12
 
13
- context "without inheritance" do
14
-
15
- subject { schematic }
16
-
17
- it "stored 'name' correctly" do
18
- attribute = subject.attribute(:name)
19
- attribute.name.should == :name
20
- attribute.params.should be_nil
21
- attribute.value.should == "Orgasmo"
13
+ describe ".new" do
14
+ it "stores the klass" do
15
+ schematic.klass.should == OpenStruct
22
16
  end
23
-
24
- it "stored 'something' correctly" do
25
- attribute = subject.attribute(:something)
26
- attribute.name.should == :something
27
- attribute.params.should == { :param => 2 }
28
- Proc.should === attribute.value
29
- attribute.value.call.should == "hi!"
17
+ it "stores the generator" do
18
+ schematic.generator.should == Fabrication::Generator::Base
30
19
  end
31
-
32
- it "stored 'another_thing' correctly" do
33
- attribute = subject.attribute(:another_thing)
34
- attribute.name.should == :another_thing
35
- attribute.params.should be_nil
36
- Proc.should === attribute.value
37
- attribute.value.call.should == 25
20
+ it "stores the attributes" do
21
+ schematic.attributes.size.should == 3
38
22
  end
39
-
40
23
  end
41
24
 
42
- context "with inheritance" do
43
-
44
- subject do
45
- schematic.merge! do
46
- name { "Willis" }
47
- something "Else!"
48
- another_thing(:thats_what => 'she_said') { "Boo-ya!" }
49
- end
25
+ describe "#attribute" do
26
+ it "returns the requested attribute if it exists" do
27
+ schematic.attribute(:name).name.should == :name
50
28
  end
51
-
52
- it "stored 'name' correctly" do
53
- attribute = subject.attribute(:name)
54
- attribute.name.should == :name
55
- attribute.params.should be_nil
56
- Proc.should === attribute.value
57
- attribute.value.call.should == "Willis"
29
+ it "returns nil if it does not exist" do
30
+ schematic.attribute(:not_there).should be_nil
58
31
  end
32
+ end
59
33
 
60
- it "stored 'something' correctly" do
61
- attribute = subject.attribute(:something)
62
- attribute.name.should == :something
63
- attribute.params.should be_nil
64
- attribute.value.should == "Else!"
34
+ describe "#attributes" do
35
+ it "always returns an empty array" do
36
+ schematic.attributes = nil
37
+ schematic.attributes.should == []
65
38
  end
39
+ end
40
+
41
+ describe "#generate" do
66
42
 
67
- it "stored 'another_thing' correctly" do
68
- attribute = subject.attribute(:another_thing)
69
- attribute.name.should == :another_thing
70
- attribute.params.should == { :thats_what => 'she_said' }
71
- Proc.should === attribute.value
72
- attribute.value.call.should == "Boo-ya!"
43
+ it "generates a new instance" do
44
+ schematic.generate.should be_kind_of(OpenStruct)
73
45
  end
74
46
 
75
47
  end
76
48
 
77
- it 'is deep clonable' do
78
- schematic2 = schematic.clone
79
- schematic.merge! do
80
- name "Henry"
81
- end
82
- schematic.attribute(:name).value.should == 'Henry'
83
- schematic2.attribute(:name).value.should == 'Orgasmo'
84
- end
49
+ describe "#merge" do
85
50
 
86
- it 'allows temporary parameter overrides' do
87
- schematic2 = schematic.merge(:name => 'Henry')
88
- schematic.attribute(:name).value.should == 'Orgasmo'
89
- schematic2.attribute(:name).value.should == 'Henry'
90
- end
51
+ context "without inheritance" do
52
+
53
+ subject { schematic }
54
+
55
+ it "stored 'name' correctly" do
56
+ attribute = subject.attribute(:name)
57
+ attribute.name.should == :name
58
+ attribute.params.should be_nil
59
+ attribute.value.should == "Orgasmo"
60
+ end
61
+
62
+ it "stored 'something' correctly" do
63
+ attribute = subject.attribute(:something)
64
+ attribute.name.should == :something
65
+ attribute.params.should == { :param => 2 }
66
+ Proc.should === attribute.value
67
+ attribute.value.call.should == "hi!"
68
+ end
69
+
70
+ it "stored 'another_thing' correctly" do
71
+ attribute = subject.attribute(:another_thing)
72
+ attribute.name.should == :another_thing
73
+ attribute.params.should be_nil
74
+ Proc.should === attribute.value
75
+ attribute.value.call.should == 25
76
+ end
91
77
 
92
- describe ".merge" do
78
+ end
93
79
 
94
- context "accepts options and a block parameter" do
80
+ context "with inheritance" do
95
81
 
96
- let(:merged) do
97
- schematic.merge(:name => "Paul") do
98
- name "Barack"
99
- something "else"
82
+ subject do
83
+ schematic.merge do
84
+ name { "Willis" }
85
+ something "Else!"
86
+ another_thing(:thats_what => 'she_said') { "Boo-ya!" }
100
87
  end
101
88
  end
102
89
 
103
- it "sets name to 'Paul'" do
104
- merged.attribute(:name).value.should == 'Paul'
90
+ it "stored 'name' correctly" do
91
+ attribute = subject.attribute(:name)
92
+ attribute.name.should == :name
93
+ attribute.params.should be_nil
94
+ Proc.should === attribute.value
95
+ attribute.value.call.should == "Willis"
96
+ end
97
+
98
+ it "stored 'something' correctly" do
99
+ attribute = subject.attribute(:something)
100
+ attribute.name.should == :something
101
+ attribute.params.should be_nil
102
+ attribute.value.should == "Else!"
105
103
  end
106
104
 
107
- it "sets something to 'else'" do
108
- merged.attribute(:something).value.should == 'else'
105
+ it "stored 'another_thing' correctly" do
106
+ attribute = subject.attribute(:another_thing)
107
+ attribute.name.should == :another_thing
108
+ attribute.params.should == { :thats_what => 'she_said' }
109
+ Proc.should === attribute.value
110
+ attribute.value.call.should == "Boo-ya!"
109
111
  end
110
112
 
111
113
  end