blueprints_boy 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.travis.yml +20 -0
- data/Appraisals +37 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +26 -0
- data/Rakefile +76 -0
- data/blueprints_boy.gemspec +27 -0
- data/gemfiles/ar3.1.gemfile +8 -0
- data/gemfiles/ar3.2.gemfile +8 -0
- data/gemfiles/ar4.0.gemfile +8 -0
- data/gemfiles/ar4.1.gemfile +8 -0
- data/gemfiles/mongoid2.gemfile +8 -0
- data/gemfiles/mongoid3.gemfile +8 -0
- data/gemfiles/mongoid4.gemfile +8 -0
- data/gemfiles/noorm.gemfile +5 -0
- data/integration/active_record/active_record_spec.rb +38 -0
- data/integration/active_record/active_record_truncation_spec.rb +26 -0
- data/integration/active_record/blueprints.rb +1 -0
- data/integration/active_record/setup.rb +8 -0
- data/integration/cucumber/blueprints.feature +20 -0
- data/integration/cucumber/step_definitions/blueprints_steps.rb +17 -0
- data/integration/cucumber/support/env.rb +11 -0
- data/integration/minitest/blueprints.rb +11 -0
- data/integration/minitest/test_minispec.rb +41 -0
- data/integration/minitest/test_minitest.rb +28 -0
- data/integration/mongoid/blueprints.rb +1 -0
- data/integration/mongoid/mongoid_spec.rb +50 -0
- data/integration/rspec/blueprints.rb +13 -0
- data/integration/rspec/rspec_spec.rb +113 -0
- data/integration/shared.rb +9 -0
- data/lib/blueprints_boy/blueprint.rb +59 -0
- data/lib/blueprints_boy/configuration.rb +48 -0
- data/lib/blueprints_boy/context.rb +53 -0
- data/lib/blueprints_boy/dependency.rb +23 -0
- data/lib/blueprints_boy/errors.rb +10 -0
- data/lib/blueprints_boy/factories.rb +18 -0
- data/lib/blueprints_boy/helper.rb +47 -0
- data/lib/blueprints_boy/integration/active_record.rb +11 -0
- data/lib/blueprints_boy/integration/cucumber.rb +9 -0
- data/lib/blueprints_boy/integration/minitest.rb +23 -0
- data/lib/blueprints_boy/integration/mongoid.rb +11 -0
- data/lib/blueprints_boy/integration/rspec.rb +20 -0
- data/lib/blueprints_boy/manager.rb +87 -0
- data/lib/blueprints_boy/railtie.rb +24 -0
- data/lib/blueprints_boy/registry.rb +28 -0
- data/lib/blueprints_boy/version.rb +3 -0
- data/lib/blueprints_boy.rb +62 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/empty_file.rb +0 -0
- data/spec/support/fixtures.rb +41 -0
- data/spec/support/manager_fixture.rb +3 -0
- data/spec/unit/blueprint_spec.rb +102 -0
- data/spec/unit/blueprints_boy_spec.rb +15 -0
- data/spec/unit/configuration_spec.rb +41 -0
- data/spec/unit/context_spec.rb +145 -0
- data/spec/unit/dependency_spec.rb +56 -0
- data/spec/unit/factories_spec.rb +27 -0
- data/spec/unit/manager_spec.rb +122 -0
- data/spec/unit/registry_spec.rb +47 -0
- metadata +189 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'blueprints_boy'
|
3
|
+
|
4
|
+
ROOT = Pathname.new('../../').expand_path(__FILE__)
|
5
|
+
|
6
|
+
require_relative 'support/fixtures'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.include Fixtures
|
10
|
+
|
11
|
+
config.after do
|
12
|
+
BlueprintsBoy.instance_eval do
|
13
|
+
@manager = nil
|
14
|
+
@config = nil
|
15
|
+
@factories = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
config.expect_with :rspec do |c|
|
20
|
+
c.syntax = :should, :expect
|
21
|
+
end
|
22
|
+
config.mock_with :rspec do |c|
|
23
|
+
c.syntax = :should, :expect
|
24
|
+
end
|
25
|
+
end
|
File without changes
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Fixtures
|
2
|
+
def self.fixture(name, &block)
|
3
|
+
iv_name = :"@#{name}"
|
4
|
+
define_method(name) do |*args|
|
5
|
+
instance_variable_set(iv_name, instance_exec(*args, &block)) unless instance_variable_defined?(iv_name)
|
6
|
+
instance_variable_get(iv_name)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_env(klass)
|
11
|
+
klass.new.tap do |object|
|
12
|
+
object.extend(BlueprintsBoy::Helper)
|
13
|
+
object.instance_variable_set(:@_blueprint_data, {})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_blueprint(name, &block)
|
18
|
+
BlueprintsBoy::Blueprint.new(empty_context, name, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
fixture :env do
|
22
|
+
create_env(Object)
|
23
|
+
end
|
24
|
+
|
25
|
+
[1, 2, 3].each do |n|
|
26
|
+
fixture("mock#{n}") { "mock#{n}" }
|
27
|
+
|
28
|
+
fixture "blueprint#{n}" do |result|
|
29
|
+
result ||= send("mock#{n}")
|
30
|
+
create_blueprint("blueprint#{n}") { result }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
fixture :manager do
|
35
|
+
BlueprintsBoy::Manager.new
|
36
|
+
end
|
37
|
+
|
38
|
+
fixture :empty_context do
|
39
|
+
BlueprintsBoy::Context.new(ROOT.join('spec/support/empty_file.rb').to_s)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Blueprint do
|
4
|
+
describe "name" do
|
5
|
+
it "should return name" do
|
6
|
+
blueprint1.name.should == :blueprint1
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "build" do
|
11
|
+
it "should build blueprint" do
|
12
|
+
blueprint1.build(env, :create)
|
13
|
+
env.blueprint1.should == mock1
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not replace existing method" do
|
17
|
+
env.set :blueprint1, :correct
|
18
|
+
blueprint1.build(env, :create)
|
19
|
+
env.blueprint1.should == :correct
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "depends_on" do
|
24
|
+
it "should return blueprint" do
|
25
|
+
blueprint1.depends_on(:blueprint2).should equal(blueprint1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should allow setting dependencies" do
|
29
|
+
blueprint1.depends_on(:blueprint2)
|
30
|
+
blueprint1.context.dependencies.should == [:blueprint2]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "attributes & options" do
|
35
|
+
it "should allow creating blueprint with attributes" do
|
36
|
+
blueprint = described_class.new(empty_context, :blueprint, attr: 'value')
|
37
|
+
blueprint.context.attrs.should == {attr: 'value'}
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should adding attributes to blueprint later" do
|
41
|
+
blueprint = described_class.new(empty_context, :blueprint).attributes(attr: 'value')
|
42
|
+
blueprint.context.attrs.should == {attr: 'value'}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should allow passing options" do
|
46
|
+
blueprint = described_class.new(empty_context, :blueprint) { |data| data.options }
|
47
|
+
blueprint.build(env, :create, attr: 'value')
|
48
|
+
env.blueprint.should == {attr: 'value'}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should allow using attributes with merged options in blueprint" do
|
52
|
+
blueprint = described_class.new(empty_context, :blueprint, attr1: 'value1', attr2: 'value2') { |data| data.attributes }
|
53
|
+
blueprint.build(env, :create, attr2: 'v2', attr3: 'v3')
|
54
|
+
env.blueprint.should == {attr1: 'value1', attr2: 'v2', attr3: 'v3'}
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should use normalized attributes when building" do
|
58
|
+
BlueprintsBoy.manager.add blueprint1
|
59
|
+
BlueprintsBoy.manager.setup(env)
|
60
|
+
blueprint = described_class.new(empty_context, :blueprint, attr: empty_context.blueprint1) { |data| data.attributes }
|
61
|
+
blueprint.build(env, :create)
|
62
|
+
env.blueprint.should == {attr: mock1}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "name" do
|
67
|
+
it "should put name in data" do
|
68
|
+
blueprint = create_blueprint(:blueprint1) { |data| data.name }
|
69
|
+
blueprint.build(env, :create)
|
70
|
+
env.blueprint1.should == :blueprint1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "strategies" do
|
75
|
+
it "should allow having multiple strategies" do
|
76
|
+
blueprint1.blueprint(:options) { |data| data.options }
|
77
|
+
blueprint1.build(env, :options, option: 'val')
|
78
|
+
env.blueprint1.should == {option: 'val'}
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should have attributes strategy" do
|
82
|
+
blueprint1.attributes(attr: 'val')
|
83
|
+
blueprint1.build(env, :attributes)
|
84
|
+
env.blueprint1.should == {attr: 'val'}
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should raise error if blueprint doesn't define strategy" do
|
88
|
+
expect {
|
89
|
+
blueprint1.build(env, :not_existing_strategy)
|
90
|
+
}.to raise_error(BlueprintsBoy::StrategyNotFound, 'Blueprint :blueprint1 does not define strategy :not_existing_strategy')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "factory" do
|
95
|
+
it "should use factory when building blueprint" do
|
96
|
+
BlueprintsBoy.factories.add(Array, :create) { |data| data.factory.new(data.attributes[:size]) }
|
97
|
+
blueprint = create_blueprint('blueprint1').factory(Array)
|
98
|
+
blueprint.build(env, :create, size: 3)
|
99
|
+
env.blueprint1.should == Array.new(3)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy do
|
4
|
+
it "should return manager" do
|
5
|
+
BlueprintsBoy.manager.should be_instance_of(BlueprintsBoy::Manager)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return configuration" do
|
9
|
+
BlueprintsBoy.config.should be_instance_of(BlueprintsBoy::Configuration)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return factories" do
|
13
|
+
BlueprintsBoy.factories.should be_instance_of(BlueprintsBoy::Factories)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Configuration do
|
4
|
+
it "should have filename with default value" do
|
5
|
+
subject.filenames.should == %w{blueprints.rb blueprints/*.rb spec/blueprints.rb spec/blueprints/*.rb test/blueprints.rb test/blueprints/*.rb}.collect do |f|
|
6
|
+
Pathname.new(f)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have correct attribute values" do
|
11
|
+
subject.prebuild.should == []
|
12
|
+
subject.transactions.should be_truthy
|
13
|
+
subject.root.should == Pathname.pwd
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should use Rails root for root if it's defined" do
|
17
|
+
module Rails
|
18
|
+
def self.root
|
19
|
+
Pathname.new('rails/root')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
subject.root.should == Pathname.new('rails/root')
|
23
|
+
Object.send(:remove_const, :Rails)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set root to pathname" do
|
27
|
+
subject.root = "root"
|
28
|
+
subject.root.should == Pathname.new("root")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should automatically set filename to array of path names" do
|
32
|
+
subject.filenames = "my_file.rb"
|
33
|
+
subject.filenames.should == [Pathname.new("my_file.rb")]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should set global to array of global blueprints" do
|
37
|
+
subject.global.should == []
|
38
|
+
subject.global = :cherry
|
39
|
+
subject.global.should == [:cherry]
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Context do
|
4
|
+
let :manager do
|
5
|
+
BlueprintsBoy::Manager.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "initialize" do
|
9
|
+
it "should read file and callback for each blueprint" do
|
10
|
+
blueprints = []
|
11
|
+
described_class.new(ROOT.join('spec/support/manager_fixture.rb').to_s) { |blueprint| blueprints << blueprint }
|
12
|
+
blueprints.size.should == 1
|
13
|
+
blueprints.first.name.should == :test
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "blueprint" do
|
18
|
+
it "should add new blueprint" do
|
19
|
+
blueprint = empty_context.blueprint(:blueprint1) {}
|
20
|
+
blueprint.should be_instance_of(BlueprintsBoy::Blueprint)
|
21
|
+
blueprint.name.should == :blueprint1
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should set context" do
|
25
|
+
blueprint = empty_context.depends_on(:blueprint2).blueprint(:blueprint1) {}
|
26
|
+
blueprint.context.should be_instance_of(BlueprintsBoy::Context)
|
27
|
+
blueprint.context.dependencies.should == [:blueprint2]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "depends_on" do
|
32
|
+
it "should set dependencies to empty array" do
|
33
|
+
empty_context.dependencies.should == []
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should create new context with dependencies set" do
|
37
|
+
context = empty_context.depends_on(:blueprint, :blueprint2)
|
38
|
+
context.should be_instance_of(BlueprintsBoy::Context)
|
39
|
+
context.should_not equal(empty_context)
|
40
|
+
context.dependencies.should == [:blueprint, :blueprint2]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should allow chaining dependencies" do
|
44
|
+
context = empty_context.depends_on(:blueprint).depends_on(:blueprint2)
|
45
|
+
context.dependencies.should == [:blueprint, :blueprint2]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should allow using block form to chain dependencies" do
|
49
|
+
context = nil
|
50
|
+
empty_context.depends_on(:blueprint) do
|
51
|
+
context = depends_on(:blueprint2)
|
52
|
+
end
|
53
|
+
context.dependencies.should == [:blueprint, :blueprint2]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should only keep uniq dependencies" do
|
57
|
+
context = empty_context.depends_on(:blueprint).depends_on(:blueprint)
|
58
|
+
context.dependencies.should == [:blueprint]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not modify original dependencies" do
|
62
|
+
empty_context.depends_on(:blueprint)
|
63
|
+
empty_context.dependencies.should == []
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "attributes" do
|
68
|
+
it "should set attributes to empty hash" do
|
69
|
+
empty_context.attrs.should == {}
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should create new context with attributes set" do
|
73
|
+
context = empty_context.attributes(attr: 'val')
|
74
|
+
context.should be_instance_of(BlueprintsBoy::Context)
|
75
|
+
context.should_not equal(empty_context)
|
76
|
+
context.attrs.should == {attr: 'val'}
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow chaining attributes using block form" do
|
80
|
+
context = nil
|
81
|
+
empty_context.attributes(attr1: 'val1', attr2: 'val2') do
|
82
|
+
context = attributes(attr2: 'v2', attr3: 'v3')
|
83
|
+
end
|
84
|
+
context.attrs.should == {attr1: 'val1', attr2: 'v2', attr3: 'v3'}
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should not modify original attributes" do
|
88
|
+
empty_context.attributes(attr: 'val')
|
89
|
+
empty_context.attrs.should == {}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "factories" do
|
94
|
+
it "should set block to nil" do
|
95
|
+
empty_context.factory_class.should be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should create new context with block" do
|
99
|
+
context = empty_context.factory(Fixnum)
|
100
|
+
context.factory_class.should == Fixnum
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should allow chaining after factory" do
|
104
|
+
context = empty_context.factory(Fixnum).attributes(attr: 'value')
|
105
|
+
context.factory_class.should == Fixnum
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "creating dependencies" do
|
110
|
+
it "should allow dependencies in attributes" do
|
111
|
+
dependency = empty_context.blueprint1
|
112
|
+
dependency.should be_a(BlueprintsBoy::Dependency)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should pass blueprint_name and options" do
|
116
|
+
dependency = empty_context.blueprint1(:blueprint_name, option: 'value')
|
117
|
+
dependency.instance_eval do
|
118
|
+
@name.should == :blueprint1
|
119
|
+
@blueprint_name.should == :blueprint_name
|
120
|
+
@options.should == {option: 'value'}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "group" do
|
126
|
+
before do
|
127
|
+
BlueprintsBoy.manager.add blueprint1
|
128
|
+
BlueprintsBoy.manager.add blueprint2
|
129
|
+
BlueprintsBoy.manager.setup env
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should allow grouping blueprints" do
|
133
|
+
group, = empty_context.group(:blueprints => [:blueprint1, :blueprint2])
|
134
|
+
group.build(env, :create)
|
135
|
+
env.blueprints.should == [mock1, mock2]
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should allow multiple groups" do
|
139
|
+
BlueprintsBoy.manager.add blueprint3
|
140
|
+
_, group2 = empty_context.group(:group1 => [:blueprint1, :blueprint2], :group2 => [:blueprint2, :blueprint3])
|
141
|
+
group2.build(env, :create)
|
142
|
+
env.group2.should == [mock2, mock3]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Dependency do
|
4
|
+
before do
|
5
|
+
BlueprintsBoy.manager.add blueprint1
|
6
|
+
BlueprintsBoy.manager.setup env
|
7
|
+
end
|
8
|
+
|
9
|
+
def value(dep)
|
10
|
+
env.instance_eval(&dep)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should allow getting instance variable value" do
|
14
|
+
value(described_class.new(:blueprint1)).should == mock1
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should allow getting another instance variable" do
|
18
|
+
preset_mock = double
|
19
|
+
env.set(:preset_key, preset_mock)
|
20
|
+
value(described_class.new(:preset_key, :blueprint1)).should == preset_mock
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should allow passing options for building" do
|
24
|
+
BlueprintsBoy.manager.add(create_blueprint(:options_blueprint) { |data| data.options })
|
25
|
+
value(described_class.new(:options_blueprint, :option => 'value')).should == {:option => 'value'}
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "missing methods" do
|
29
|
+
subject { described_class.new(:blueprint1) }
|
30
|
+
|
31
|
+
it "should record all missing methods" do
|
32
|
+
subject.missing_method
|
33
|
+
mock1.should_receive(:missing_method).and_return(mock2)
|
34
|
+
value(subject).should == mock2
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should allow chaining methods" do
|
38
|
+
subject.missing_method.another_missing_method
|
39
|
+
mock1.should_receive(:missing_method).and_return(mock2)
|
40
|
+
mock2.should_receive(:another_missing_method).and_return(mock3)
|
41
|
+
value(subject).should == mock3
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should remember parameters" do
|
45
|
+
subject.missing_method(1, 2)
|
46
|
+
mock1.should_receive(:missing_method).with(1, 2).and_return(mock2)
|
47
|
+
value(subject).should == mock2
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should remember block" do
|
51
|
+
subject.missing_method { |value| value + 2 }
|
52
|
+
mock1.should_receive(:missing_method).and_yield(1)
|
53
|
+
value(subject).should == 3
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Factories do
|
4
|
+
describe "finding" do
|
5
|
+
before do
|
6
|
+
subject.add(Integer, :create) { 5 }
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should allow defining and finding a factory" do
|
10
|
+
factory = subject[Integer, :create]
|
11
|
+
factory.should be_instance_of(Proc)
|
12
|
+
factory.call.should == 5
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should find factory for child class" do
|
16
|
+
factory = subject[Fixnum, :create]
|
17
|
+
factory.should be_instance_of(Proc)
|
18
|
+
factory.call.should == 5
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should raise error if factory can't be found" do
|
22
|
+
expect {
|
23
|
+
subject[Float, :create]
|
24
|
+
}.to raise_error(BlueprintsBoy::FactoryNotFound, "Factory for Float can't be located")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Manager do
|
4
|
+
describe "add" do
|
5
|
+
it "should add blueprint" do
|
6
|
+
subject.add(blueprint1)
|
7
|
+
subject.blueprints.should == {:blueprint1 => blueprint1}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "find" do
|
12
|
+
it "should find added blueprint" do
|
13
|
+
subject.add(blueprint1)
|
14
|
+
subject.find(:blueprint1).should equal(blueprint1)
|
15
|
+
subject[:blueprint1].should equal(blueprint1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should raise error if blueprint is not found" do
|
19
|
+
expect {
|
20
|
+
subject.find(:blueprint_not_existing)
|
21
|
+
}.to raise_error(BlueprintsBoy::BlueprintNotFound, 'Blueprint :blueprint_not_existing cannot be found')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "build" do
|
26
|
+
before do
|
27
|
+
subject.add(blueprint1)
|
28
|
+
subject.push_registry
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should build blueprint" do
|
32
|
+
subject.build(env, [:blueprint1])
|
33
|
+
env.blueprint1.should == mock1
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should add blueprint to built blueprints" do
|
37
|
+
subject.build(env, [:blueprint1])
|
38
|
+
subject.registry.built.should include(:blueprint1)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not build same blueprint twice" do
|
42
|
+
env.should_receive(:autoset).once
|
43
|
+
subject.build(env, [:blueprint1, :blueprint1])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should use update strategy if options are present and blueprint is already built" do
|
47
|
+
mock1.should_receive(:update)
|
48
|
+
blueprint1.blueprint(:update) { blueprint1.update }
|
49
|
+
subject.build(env, [:blueprint1, {:blueprint1 => {option: 'value'}}])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should build dependencies of blueprint" do
|
53
|
+
subject.add blueprint2.depends_on(:blueprint1)
|
54
|
+
subject.build env, [:blueprint2]
|
55
|
+
subject.registry.built.to_a.should == [:blueprint2, :blueprint1]
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should allow passing options" do
|
59
|
+
subject.add(create_blueprint(:options_blueprint) { |data| data.options[:name] })
|
60
|
+
subject.build(env, [{:options_blueprint => {name: 'success'}}])
|
61
|
+
env.options_blueprint.should == 'success'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return results" do
|
65
|
+
subject.add(blueprint2)
|
66
|
+
subject.build(env, [:blueprint1, :blueprint2 => {attr: 'val'}]).should == [mock1, mock2]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return 1 result if only one blueprint was built" do
|
70
|
+
subject.build(env, [:blueprint1]).should == mock1
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return results for already built blueprints" do
|
74
|
+
subject.build(env, [:blueprint1])
|
75
|
+
subject.build(env, [:blueprint1]).should == mock1
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should allow passing strategy" do
|
79
|
+
blueprint1.blueprint(:new) { :new_strat }
|
80
|
+
subject.build(env, [:blueprint1], strategy: :new)
|
81
|
+
env.blueprint1.should == :new_strat
|
82
|
+
end
|
83
|
+
|
84
|
+
it "does not overwrite strategy with :update" do
|
85
|
+
counter = 0
|
86
|
+
blueprint1.blueprint(:counter) { counter += 1 }
|
87
|
+
2.times { subject.build(env, [:blueprint1], strategy: :counter) }
|
88
|
+
counter.should == 2
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "setup" do
|
93
|
+
it "should set @_blueprint_data to {}" do
|
94
|
+
subject.setup(env)
|
95
|
+
env.instance_variable_get(:@_blueprint_data).should == {}
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should restore blueprints from registry to @_blueprint_data" do
|
99
|
+
blueprint1
|
100
|
+
blueprint2
|
101
|
+
subject.add(blueprint1)
|
102
|
+
subject.add(blueprint2)
|
103
|
+
|
104
|
+
subject.push_registry([:blueprint1])
|
105
|
+
subject.push_registry([:blueprint2])
|
106
|
+
|
107
|
+
subject.setup(env)
|
108
|
+
env.instance_variable_get(:@_blueprint_data).should == {blueprint1: mock1, blueprint2: mock2}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "teardown" do
|
113
|
+
it "should pop registry" do
|
114
|
+
subject.add(blueprint1)
|
115
|
+
subject.push_registry
|
116
|
+
subject.build(env, [:blueprint1])
|
117
|
+
|
118
|
+
subject.teardown
|
119
|
+
subject.registry.should be_nil
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BlueprintsBoy::Registry do
|
4
|
+
let :names do
|
5
|
+
[:mock1, :mock2]
|
6
|
+
end
|
7
|
+
|
8
|
+
subject do
|
9
|
+
described_class.new(names, nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "built" do
|
13
|
+
it "should initialize built with empty set" do
|
14
|
+
subject.built.should == Set.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should inherit built from parent" do
|
18
|
+
subject.built.add :blueprint1
|
19
|
+
child = described_class.new(names, subject)
|
20
|
+
child.built.should == Set.new([:blueprint1])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "store/restore" do
|
25
|
+
it "should return {} by default" do
|
26
|
+
subject.restore.should == {}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should restore results of blueprints" do
|
30
|
+
subject.store mock1: mock1, mock2: mock2
|
31
|
+
subject.restore.should == {mock1: mock1, mock2: mock2}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should revert changes to restored objects" do
|
35
|
+
subject.store mock1: mock1, mock2: mock2
|
36
|
+
mock1 << ' has failed us'
|
37
|
+
subject.restore[:mock1].should == 'mock1'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should restore parent objects too" do
|
41
|
+
child = described_class.new([], subject)
|
42
|
+
subject.store mock1: mock1
|
43
|
+
child.store mock2: mock2
|
44
|
+
child.restore.should == {mock1: mock1, mock2: mock2}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|