fabrication 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +14 -0
- data/README.markdown +7 -1
- data/Rakefile +19 -0
- data/lib/fabrication.rb +11 -0
- data/lib/fabrication/config.rb +1 -1
- data/lib/fabrication/{cucumber.rb → cucumber/step_fabricator.rb} +2 -7
- data/lib/fabrication/fabricator.rb +6 -1
- data/lib/fabrication/support.rb +1 -1
- data/lib/fabrication/syntax/make.rb +26 -0
- data/lib/fabrication/transform.rb +26 -0
- data/lib/fabrication/version.rb +1 -1
- data/lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb +0 -2
- data/lib/rails/generators/fabrication/turnip_steps/templates/fabrication_steps.rb +57 -0
- data/lib/rails/generators/fabrication/turnip_steps/turnip_steps_generator.rb +21 -0
- metadata +156 -135
- data/spec/fabrication/attribute_spec.rb +0 -53
- data/spec/fabrication/config_spec.rb +0 -32
- data/spec/fabrication/cucumber_spec.rb +0 -116
- data/spec/fabrication/fabricator_spec.rb +0 -67
- data/spec/fabrication/generator/active_record_spec.rb +0 -83
- data/spec/fabrication/generator/base_spec.rb +0 -108
- data/spec/fabrication/schematic_spec.rb +0 -199
- data/spec/fabrication/sequencer_spec.rb +0 -98
- data/spec/fabrication/support_spec.rb +0 -54
- data/spec/fabrication_spec.rb +0 -329
- data/spec/fabricators/active_record_fabricator.rb +0 -21
- data/spec/fabricators/cool_object_fabricator.rb +0 -1
- data/spec/fabricators/mongoid_fabricator.rb +0 -36
- data/spec/fabricators/plain_old_ruby_object_fabricator.rb +0 -51
- data/spec/fabricators/sequel_fabricator.rb +0 -15
- data/spec/fabricators/sequencer_fabricator.rb +0 -9
- data/spec/spec_helper.rb +0 -12
- data/spec/support/active_record.rb +0 -65
- data/spec/support/mongoid.rb +0 -74
- data/spec/support/plain_old_ruby_objects.rb +0 -68
- data/spec/support/sequel.rb +0 -22
- data/spec/support/sequel_migrations/001_create_tables.rb +0 -23
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Fabrication::Attribute do
|
4
|
-
|
5
|
-
describe ".new" do
|
6
|
-
|
7
|
-
context "with name, params, and a static value" do
|
8
|
-
|
9
|
-
subject do
|
10
|
-
Fabrication::Attribute.new("a", {:b => 1}, "c")
|
11
|
-
end
|
12
|
-
|
13
|
-
its(:name) { should == "a" }
|
14
|
-
its(:params) { should == {:b => 1} }
|
15
|
-
its(:value) { should == "c" }
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
context "with a block value" do
|
20
|
-
|
21
|
-
subject do
|
22
|
-
Fabrication::Attribute.new("a", nil, Proc.new { "c" })
|
23
|
-
end
|
24
|
-
|
25
|
-
it "has a proc for a value" do
|
26
|
-
Proc.should === subject.value
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
context "with a nil value" do
|
32
|
-
|
33
|
-
subject do
|
34
|
-
Fabrication::Attribute.new("a", nil, nil)
|
35
|
-
end
|
36
|
-
|
37
|
-
its(:value) { should be_nil }
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
context "with nil params" do
|
42
|
-
|
43
|
-
subject do
|
44
|
-
Fabrication::Attribute.new("a", nil, nil)
|
45
|
-
end
|
46
|
-
|
47
|
-
its(:params) { should be_nil }
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Fabrication::Config do
|
4
|
-
subject { Fabrication::Config }
|
5
|
-
after { Fabrication::Config.reset_defaults }
|
6
|
-
|
7
|
-
context "default configs" do
|
8
|
-
its(:fabricator_dir) { should == ['test', 'spec'] }
|
9
|
-
end
|
10
|
-
|
11
|
-
describe ".fabricator_dir" do
|
12
|
-
context "with a single folder" do
|
13
|
-
before do
|
14
|
-
Fabrication.configure do |config|
|
15
|
-
config.fabricator_dir = 'lib'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
its(:fabricator_dir) { should == ['lib'] }
|
20
|
-
end
|
21
|
-
|
22
|
-
context "with multiple folders" do
|
23
|
-
before do
|
24
|
-
Fabrication.configure do |config|
|
25
|
-
config.fabricator_dir = %w(lib support)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
its(:fabricator_dir) { should == ['lib', 'support'] }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,116 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'fabrication/cucumber'
|
3
|
-
|
4
|
-
describe Fabrication::Cucumber do
|
5
|
-
include described_class
|
6
|
-
|
7
|
-
let(:name) { 'dogs' }
|
8
|
-
|
9
|
-
describe '#klass' do
|
10
|
-
context 'with a schematic for class "Boom"' do
|
11
|
-
subject { StepFabricator.new(name).klass }
|
12
|
-
let(:fabricator_name) { :dog }
|
13
|
-
|
14
|
-
before do
|
15
|
-
Fabrication::Fabricator.schematics.stub(:[])
|
16
|
-
.with(fabricator_name).and_return(stub(:klass => "Boom"))
|
17
|
-
end
|
18
|
-
|
19
|
-
it { should == "Boom" }
|
20
|
-
|
21
|
-
context "given a human name" do
|
22
|
-
let(:name) { "weiner dogs" }
|
23
|
-
let(:fabricator_name) { :weiner_dog }
|
24
|
-
it { should == "Boom" }
|
25
|
-
end
|
26
|
-
|
27
|
-
context "given a titlecase human name" do
|
28
|
-
let(:name) { "Weiner Dog" }
|
29
|
-
let(:fabricator_name) { :weiner_dog }
|
30
|
-
it { should == "Boom" }
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "#n" do
|
36
|
-
let(:n) { 3 }
|
37
|
-
let(:fabricator) { StepFabricator.new(name) }
|
38
|
-
|
39
|
-
it "fabricates n times" do
|
40
|
-
Fabrication::Fabricator.should_receive(:generate).with(:dog, anything, {}).exactly(n).times
|
41
|
-
fabricator.n n
|
42
|
-
end
|
43
|
-
|
44
|
-
it "fabricates with attrs" do
|
45
|
-
Fabrication::Fabricator.should_receive(:generate)
|
46
|
-
.with(anything, anything, :collar => 'red').at_least(1)
|
47
|
-
fabricator.n n, :collar => 'red'
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with a plural subject' do
|
51
|
-
let(:name) { 'dogs' }
|
52
|
-
it 'remembers' do
|
53
|
-
Fabrication::Fabricator.stub(:generate).and_return("dog1", "dog2", "dog3")
|
54
|
-
fabricator.n n
|
55
|
-
Fabrications[name].should == ["dog1", "dog2", "dog3"]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'with a singular subject' do
|
60
|
-
let(:name) { 'dog' }
|
61
|
-
it 'remembers' do
|
62
|
-
Fabrication::Fabricator.stub(:generate).and_return("dog1")
|
63
|
-
fabricator.n 1
|
64
|
-
Fabrications[name].should == 'dog1'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '#from_table' do
|
71
|
-
it 'maps column names to attribute names' do
|
72
|
-
table = stub(:hashes => [{ 'Favorite Color' => 'pink' }])
|
73
|
-
Fabrication::Fabricator.should_receive(:generate)
|
74
|
-
.with(anything, anything, :favorite_color => 'pink')
|
75
|
-
StepFabricator.new('bears').from_table(table)
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'with a plural subject' do
|
79
|
-
let(:table) { double("ASTable", :hashes => hashes) }
|
80
|
-
let(:hashes) do
|
81
|
-
[{'some' => 'thing'},
|
82
|
-
{'some' => 'panother'}]
|
83
|
-
end
|
84
|
-
it 'fabricates with each rows attributes' do
|
85
|
-
Fabrication::Fabricator.should_receive(:generate)
|
86
|
-
.with(:dog, anything, {:some => 'thing'})
|
87
|
-
Fabrication::Fabricator.should_receive(:generate)
|
88
|
-
.with(:dog, anything, {:some => 'panother'})
|
89
|
-
StepFabricator.new(name).from_table(table)
|
90
|
-
end
|
91
|
-
it 'remembers' do
|
92
|
-
Fabrication::Fabricator.stub(:generate).and_return('dog1', 'dog2')
|
93
|
-
StepFabricator.new(name).from_table(table)
|
94
|
-
Fabrications[name].should == ["dog1", "dog2"]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context 'singular' do
|
99
|
-
let(:name) { 'dog' }
|
100
|
-
let(:table) { double("ASTable", :rows_hash => rows_hash) }
|
101
|
-
let(:rows_hash) do
|
102
|
-
{'some' => 'thing'}
|
103
|
-
end
|
104
|
-
it 'fabricates with each row as an attribute' do
|
105
|
-
Fabrication::Fabricator.should_receive(:generate).with(:dog, anything, {:some => 'thing'})
|
106
|
-
StepFabricator.new(name).from_table(table)
|
107
|
-
end
|
108
|
-
it 'remembers' do
|
109
|
-
Fabrication::Fabricator.stub(:generate).and_return('dog1')
|
110
|
-
StepFabricator.new(name).from_table(table)
|
111
|
-
Fabrications[name].should == "dog1"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Fabrication::Fabricator do
|
4
|
-
|
5
|
-
subject { Fabrication::Fabricator }
|
6
|
-
|
7
|
-
describe ".define" do
|
8
|
-
|
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
|
21
|
-
|
22
|
-
it "creates a schematic" do
|
23
|
-
subject.schematics[:open_struct].should be
|
24
|
-
end
|
25
|
-
|
26
|
-
it "has the correct class" do
|
27
|
-
subject.schematics[:open_struct].klass.should == OpenStruct
|
28
|
-
end
|
29
|
-
|
30
|
-
it "has the attributes" do
|
31
|
-
subject.schematics[:open_struct].attributes.size.should == 2
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
describe ".generate" do
|
37
|
-
|
38
|
-
context 'without definitions' do
|
39
|
-
|
40
|
-
before { subject.schematics.clear }
|
41
|
-
|
42
|
-
it "finds definitions if none exist" do
|
43
|
-
Fabrication::Support.should_receive(:find_definitions)
|
44
|
-
lambda { subject.generate(:object) }.should raise_error
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'with definitions' do
|
50
|
-
|
51
|
-
it "raises an error if the class cannot be located" do
|
52
|
-
lambda { subject.define(:somenonexistantclass) }.should raise_error(Fabrication::UnfabricatableError)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "raises an error if the fabricator cannot be located" do
|
56
|
-
lambda { subject.generate(:object) }.should raise_error(Fabrication::UnknownFabricatorError)
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'generates a new object every time' do
|
60
|
-
subject.generate(:person).should_not == subject.generate(:person)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Fabrication::Generator::ActiveRecord do
|
4
|
-
|
5
|
-
describe ".supports?" do
|
6
|
-
subject { Fabrication::Generator::ActiveRecord }
|
7
|
-
it "returns true for active record objects" do
|
8
|
-
subject.supports?(Company).should be_true
|
9
|
-
end
|
10
|
-
it "returns false for non-active record objects" do
|
11
|
-
subject.supports?(Person).should be_false
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "#after_generation" do
|
16
|
-
let(:instance) { mock(:instance) }
|
17
|
-
let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
|
18
|
-
|
19
|
-
before { generator.send(:instance=, instance) }
|
20
|
-
|
21
|
-
it "saves with a true save flag" do
|
22
|
-
instance.should_receive(:save!)
|
23
|
-
generator.send(:after_generation, {:save => true})
|
24
|
-
end
|
25
|
-
|
26
|
-
it "does not save without a true save flag" do
|
27
|
-
instance.should_not_receive(:save)
|
28
|
-
generator.send(:after_generation, {})
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#generate" do
|
33
|
-
|
34
|
-
let(:attributes) do
|
35
|
-
Fabrication::Schematic.new(Company) do
|
36
|
-
name 'Company Name'
|
37
|
-
city { |c| c.name.downcase }
|
38
|
-
divisions(:count => 2) { |c, i| Division.create(:company => c, :name => "Division #{i}") }
|
39
|
-
end.attributes
|
40
|
-
end
|
41
|
-
|
42
|
-
let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
|
43
|
-
let(:company) { generator.generate({:save => true}, attributes) }
|
44
|
-
before { company }
|
45
|
-
|
46
|
-
it 'does not persist the divisions immediately' do
|
47
|
-
Division.count.should == 0
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'passes the object to blocks' do
|
51
|
-
company.city.should == 'company name'
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'passes the object and count to blocks' do
|
55
|
-
company.divisions.map(&:name).should == ["Division 1", "Division 2"]
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'persists the company upon creation' do
|
59
|
-
Company.find_by_name('Company Name').should be
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'upon accessing the divisions association' do
|
63
|
-
|
64
|
-
let(:divisions) { company.divisions }
|
65
|
-
|
66
|
-
it 'generates the divisions' do
|
67
|
-
divisions.length.should == 2
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'persists the divisions' do
|
71
|
-
divisions
|
72
|
-
Division.find_all_by_company_id(company.id).count.should == 2
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'can load the divisions from the database' do
|
76
|
-
company.reload.divisions.length.should == 2
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
@@ -1,108 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Fabrication::Generator::Base do
|
4
|
-
|
5
|
-
describe ".supports?" do
|
6
|
-
subject { Fabrication::Generator::Base }
|
7
|
-
it "supports any object" do
|
8
|
-
subject.supports?(Object).should be_true
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#generate" do
|
13
|
-
|
14
|
-
let(:generator) { Fabrication::Generator::Base.new(Person) }
|
15
|
-
|
16
|
-
let(:attributes) do
|
17
|
-
Fabrication::Schematic.new(Person) do
|
18
|
-
first_name 'Guy'
|
19
|
-
shoes(:count => 4) do |person, index|
|
20
|
-
"#{person.first_name}'s shoe #{index}"
|
21
|
-
end
|
22
|
-
end.attributes
|
23
|
-
end
|
24
|
-
|
25
|
-
let(:person) { generator.generate({}, attributes) }
|
26
|
-
|
27
|
-
it 'generates an instance' do
|
28
|
-
person.instance_of?(Person).should be_true
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'passes the object and count to blocks' do
|
32
|
-
person.shoes.should == (1..4).map { |i| "Guy's shoe #{i}" }
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'sets the static value' do
|
36
|
-
person.instance_variable_get(:@first_name).should == 'Guy'
|
37
|
-
end
|
38
|
-
|
39
|
-
context "with on_init block" do
|
40
|
-
subject { schematic.generate }
|
41
|
-
|
42
|
-
let(:klass) { Struct.new :arg1, :arg2 }
|
43
|
-
|
44
|
-
context "using init_with" do
|
45
|
-
let(:schematic) do
|
46
|
-
Fabrication::Schematic.new(klass) do
|
47
|
-
on_init { init_with(:a, :b) }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it "sends the return value of the block to the klass' initialize method" do
|
52
|
-
subject.arg1.should == :a
|
53
|
-
subject.arg2.should == :b
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context "not using init_with" do
|
58
|
-
let(:schematic) do
|
59
|
-
Fabrication::Schematic.new(klass) do
|
60
|
-
on_init { [ :a, :b ] }
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
it "sends the return value of the block to the klass' initialize method" do
|
65
|
-
subject.arg1.should == :a
|
66
|
-
subject.arg2.should == :b
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
context "using an after_create hook" do
|
73
|
-
let(:schematic) do
|
74
|
-
Fabrication::Schematic.new(Person) do
|
75
|
-
first_name "Guy"
|
76
|
-
after_create { |k| k.first_name.upcase! }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it "calls after_create when generated with saving" do
|
81
|
-
schematic.generate(:save => true).first_name.should == "GUY"
|
82
|
-
end
|
83
|
-
|
84
|
-
it "does not call after_create when generated without saving" do
|
85
|
-
schematic.generate(:save => false).first_name.should == "Guy"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "#after_generation" do
|
92
|
-
let(:instance) { mock(:instance) }
|
93
|
-
let(:generator) { Fabrication::Generator::Base.new(Object) }
|
94
|
-
|
95
|
-
before { generator.send(:instance=, instance) }
|
96
|
-
|
97
|
-
it "saves with a true save flag" do
|
98
|
-
instance.should_receive(:save!)
|
99
|
-
generator.send(:after_generation, {:save => true})
|
100
|
-
end
|
101
|
-
|
102
|
-
it "does not save without a true save flag" do
|
103
|
-
instance.should_not_receive(:save)
|
104
|
-
generator.send(:after_generation, {})
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|