fabrication 0.6.0 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +0 -15
- data/lib/fabrication.rb +11 -74
- data/lib/fabrication/errors.rb +1 -1
- data/lib/fabrication/fabricator.rb +41 -21
- data/lib/fabrication/generator/base.rb +9 -11
- data/lib/fabrication/generator/mongoid.rb +4 -4
- data/lib/fabrication/schematic.rb +22 -13
- data/lib/fabrication/sequencer.rb +19 -0
- data/lib/fabrication/support.rb +13 -1
- data/lib/fabrication/version.rb +1 -1
- data/spec/fabrication_spec.rb +38 -105
- data/spec/fabricator_spec.rb +37 -28
- data/spec/generator/active_record_spec.rb +31 -30
- data/spec/generator/base_spec.rb +32 -27
- data/spec/generator/mongoid_spec.rb +17 -22
- data/spec/schematic_spec.rb +76 -74
- data/spec/sequence_spec.rb +5 -5
- data/spec/spec_helper.rb +0 -6
- data/spec/support/active_record.rb +4 -0
- data/spec/support/helper_objects.rb +15 -0
- data/spec/support/mongoid.rb +15 -0
- data/spec/support_spec.rb +61 -0
- metadata +6 -5
- data/spec/benchmark_spec.rb +0 -11
data/spec/fabricator_spec.rb
CHANGED
@@ -2,52 +2,61 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Fabrication::Fabricator do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
5
|
+
subject { Fabrication::Fabricator }
|
6
|
+
|
7
|
+
describe ".define" do
|
9
8
|
|
10
|
-
|
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
|
-
|
22
|
+
it "creates a schematic" do
|
23
|
+
subject.schematics[:open_struct].should be
|
24
|
+
end
|
13
25
|
|
14
|
-
it
|
15
|
-
|
26
|
+
it "has the correct class" do
|
27
|
+
subject.schematics[:open_struct].klass.should == OpenStruct
|
16
28
|
end
|
17
29
|
|
18
|
-
it
|
19
|
-
|
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
|
-
|
36
|
+
describe ".generate" do
|
25
37
|
|
26
|
-
|
27
|
-
after(:all) { TestMigration.down }
|
38
|
+
context 'without definitions' do
|
28
39
|
|
29
|
-
|
40
|
+
before { subject.schematics.clear }
|
30
41
|
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
49
|
+
context 'with definitions' do
|
40
50
|
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
9
|
-
Fabrication::
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
18
|
+
describe "#after_generation" do
|
19
|
+
let(:instance) { mock(:instance) }
|
20
|
+
let(:generator) { Fabrication::Generator::ActiveRecord.new(Object) }
|
19
21
|
|
20
|
-
|
22
|
+
before { generator.send(:instance=, instance) }
|
21
23
|
|
22
|
-
|
23
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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 == '
|
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
|
data/spec/generator/base_spec.rb
CHANGED
@@ -2,43 +2,48 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Fabrication::Generator::Base do
|
4
4
|
|
5
|
-
|
6
|
-
Fabrication::
|
7
|
-
|
8
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
21
|
-
person.last_name.should == 'Ydob'
|
22
|
-
end
|
20
|
+
describe "#generate" do
|
23
21
|
|
24
|
-
|
25
|
-
person.shoes.should == (1..4).map { |i| "shoe #{i}" }
|
26
|
-
end
|
22
|
+
let(:generator) { Fabrication::Generator::Base.new(Person) }
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
person.instance_variable_get(:@first_name).should == 'Body'
|
34
|
-
end
|
33
|
+
let(:person) { generator.generate({}, attributes) }
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
15
|
+
describe "#after_generation" do
|
16
|
+
let(:instance) { mock(:instance) }
|
17
|
+
let(:generator) { Fabrication::Generator::Mongoid.new(Object) }
|
20
18
|
|
21
|
-
before { generator.
|
19
|
+
before { generator.send(:instance=, instance) }
|
22
20
|
|
23
|
-
it
|
24
|
-
|
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
|
28
|
-
|
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
|
data/spec/schematic_spec.rb
CHANGED
@@ -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
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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 "
|
68
|
-
|
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
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
78
|
+
end
|
93
79
|
|
94
|
-
context "
|
80
|
+
context "with inheritance" do
|
95
81
|
|
96
|
-
|
97
|
-
schematic.merge
|
98
|
-
name "
|
99
|
-
something "
|
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 "
|
104
|
-
|
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 "
|
108
|
-
|
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
|