factory_girl 4.2.0 → 4.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +29 -7
- data/.yardopts +1 -1
- data/Appraisals +12 -8
- data/CONTRIBUTING.md +60 -0
- data/GETTING_STARTED.md +473 -191
- data/Gemfile +6 -3
- data/Gemfile.lock +93 -65
- data/LICENSE +1 -1
- data/NAME.md +11 -0
- data/NEWS +43 -1
- data/README.md +52 -28
- data/Rakefile +1 -3
- data/factory_girl.gemspec +9 -17
- data/gemfiles/3.2.gemfile +6 -3
- data/gemfiles/3.2.gemfile.lock +85 -62
- data/gemfiles/4.0.gemfile +6 -3
- data/gemfiles/4.0.gemfile.lock +84 -74
- data/gemfiles/4.1.gemfile +10 -0
- data/gemfiles/4.1.gemfile.lock +104 -0
- data/gemfiles/4.2.gemfile +10 -0
- data/gemfiles/4.2.gemfile.lock +104 -0
- data/gemfiles/5.0.gemfile +10 -0
- data/gemfiles/5.0.gemfile.lock +103 -0
- data/lib/factory_girl/attribute/dynamic.rb +4 -1
- data/lib/factory_girl/attribute_assigner.rb +4 -4
- data/lib/factory_girl/attribute_list.rb +1 -1
- data/lib/factory_girl/callback.rb +1 -1
- data/lib/factory_girl/callbacks_observer.rb +1 -1
- data/lib/factory_girl/configuration.rb +7 -2
- data/lib/factory_girl/declaration_list.rb +2 -2
- data/lib/factory_girl/decorator/attribute_hash.rb +1 -2
- data/lib/factory_girl/decorator/class_key_hash.rb +2 -1
- data/lib/factory_girl/decorator.rb +2 -2
- data/lib/factory_girl/definition.rb +25 -15
- data/lib/factory_girl/definition_hierarchy.rb +1 -1
- data/lib/factory_girl/definition_proxy.rb +15 -17
- data/lib/factory_girl/errors.rb +6 -0
- data/lib/factory_girl/evaluator.rb +7 -1
- data/lib/factory_girl/factory.rb +3 -3
- data/lib/factory_girl/factory_runner.rb +7 -1
- data/lib/factory_girl/find_definitions.rb +3 -3
- data/lib/factory_girl/linter.rb +97 -0
- data/lib/factory_girl/strategy/stub.rb +58 -20
- data/lib/factory_girl/strategy_syntax_method_registrar.rb +17 -0
- data/lib/factory_girl/syntax/default.rb +8 -0
- data/lib/factory_girl/syntax/methods.rb +18 -2
- data/lib/factory_girl/version.rb +1 -1
- data/lib/factory_girl.rb +30 -3
- metadata +65 -249
- data/CONTRIBUTION_GUIDELINES.md +0 -10
- data/features/find_definitions.feature +0 -75
- data/features/step_definitions/database_steps.rb +0 -9
- data/features/step_definitions/factory_girl_steps.rb +0 -36
- data/features/support/env.rb +0 -10
- data/features/support/factories.rb +0 -16
- data/gemfiles/3.0.gemfile +0 -7
- data/gemfiles/3.0.gemfile.lock +0 -81
- data/gemfiles/3.1.gemfile +0 -7
- data/gemfiles/3.1.gemfile.lock +0 -82
- data/spec/acceptance/activesupport_instrumentation_spec.rb +0 -62
- data/spec/acceptance/aliases_spec.rb +0 -19
- data/spec/acceptance/attribute_aliases_spec.rb +0 -45
- data/spec/acceptance/attribute_existing_on_object_spec.rb +0 -68
- data/spec/acceptance/attributes_for_spec.rb +0 -97
- data/spec/acceptance/attributes_from_instance_spec.rb +0 -53
- data/spec/acceptance/attributes_ordered_spec.rb +0 -51
- data/spec/acceptance/build_list_spec.rb +0 -56
- data/spec/acceptance/build_spec.rb +0 -86
- data/spec/acceptance/build_stubbed_spec.rb +0 -161
- data/spec/acceptance/callbacks_spec.rb +0 -177
- data/spec/acceptance/create_list_spec.rb +0 -97
- data/spec/acceptance/create_spec.rb +0 -116
- data/spec/acceptance/define_child_before_parent_spec.rb +0 -21
- data/spec/acceptance/definition_spec.rb +0 -21
- data/spec/acceptance/definition_without_block_spec.rb +0 -15
- data/spec/acceptance/global_initialize_with_spec.rb +0 -82
- data/spec/acceptance/global_to_create_spec.rb +0 -122
- data/spec/acceptance/initialize_with_spec.rb +0 -217
- data/spec/acceptance/keyed_by_class_spec.rb +0 -22
- data/spec/acceptance/modify_factories_spec.rb +0 -184
- data/spec/acceptance/modify_inherited_spec.rb +0 -52
- data/spec/acceptance/nested_attributes_spec.rb +0 -32
- data/spec/acceptance/overrides_spec.rb +0 -61
- data/spec/acceptance/parent_spec.rb +0 -90
- data/spec/acceptance/register_strategies_spec.rb +0 -128
- data/spec/acceptance/sequence_context_spec.rb +0 -52
- data/spec/acceptance/sequence_spec.rb +0 -61
- data/spec/acceptance/skip_create_spec.rb +0 -19
- data/spec/acceptance/stub_spec.rb +0 -62
- data/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb +0 -41
- data/spec/acceptance/traits_spec.rb +0 -727
- data/spec/acceptance/transient_attributes_spec.rb +0 -124
- data/spec/factory_girl/aliases_spec.rb +0 -31
- data/spec/factory_girl/attribute/association_spec.rb +0 -28
- data/spec/factory_girl/attribute/dynamic_spec.rb +0 -52
- data/spec/factory_girl/attribute/sequence_spec.rb +0 -16
- data/spec/factory_girl/attribute/static_spec.rb +0 -19
- data/spec/factory_girl/attribute_list_spec.rb +0 -143
- data/spec/factory_girl/attribute_spec.rb +0 -16
- data/spec/factory_girl/callback_spec.rb +0 -41
- data/spec/factory_girl/declaration/implicit_spec.rb +0 -25
- data/spec/factory_girl/declaration_list_spec.rb +0 -67
- data/spec/factory_girl/definition_proxy_spec.rb +0 -215
- data/spec/factory_girl/definition_spec.rb +0 -58
- data/spec/factory_girl/disallows_duplicates_registry_spec.rb +0 -19
- data/spec/factory_girl/evaluator_class_definer_spec.rb +0 -77
- data/spec/factory_girl/factory_spec.rb +0 -259
- data/spec/factory_girl/find_definitions_spec.rb +0 -110
- data/spec/factory_girl/null_factory_spec.rb +0 -13
- data/spec/factory_girl/null_object_spec.rb +0 -22
- data/spec/factory_girl/registry_spec.rb +0 -68
- data/spec/factory_girl/sequence_spec.rb +0 -95
- data/spec/factory_girl/strategy/attributes_for_spec.rb +0 -18
- data/spec/factory_girl/strategy/build_spec.rb +0 -7
- data/spec/factory_girl/strategy/create_spec.rb +0 -25
- data/spec/factory_girl/strategy/stub_spec.rb +0 -38
- data/spec/factory_girl/strategy_calculator_spec.rb +0 -29
- data/spec/factory_girl_spec.rb +0 -22
- data/spec/spec_helper.rb +0 -26
- data/spec/support/macros/define_constant.rb +0 -86
- data/spec/support/matchers/callback.rb +0 -9
- data/spec/support/matchers/declaration.rb +0 -83
- data/spec/support/matchers/delegate.rb +0 -44
- data/spec/support/matchers/trait.rb +0 -9
- data/spec/support/shared_examples/strategy.rb +0 -90
@@ -1,124 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "transient attributes" do
|
4
|
-
before do
|
5
|
-
define_model("User", name: :string, email: :string)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
sequence(:name) {|n| "John #{n}" }
|
9
|
-
|
10
|
-
factory :user do
|
11
|
-
ignore do
|
12
|
-
four { 2 + 2 }
|
13
|
-
rockstar true
|
14
|
-
upcased false
|
15
|
-
end
|
16
|
-
|
17
|
-
name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
|
18
|
-
email { "#{name.downcase}#{four}@example.com" }
|
19
|
-
|
20
|
-
after(:create) do |user, evaluator|
|
21
|
-
user.name.upcase! if evaluator.upcased
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context "returning attributes for a factory" do
|
28
|
-
subject { FactoryGirl.attributes_for(:user, rockstar: true) }
|
29
|
-
it { should_not have_key(:four) }
|
30
|
-
it { should_not have_key(:rockstar) }
|
31
|
-
it { should_not have_key(:upcased) }
|
32
|
-
it { should have_key(:name) }
|
33
|
-
it { should have_key(:email) }
|
34
|
-
end
|
35
|
-
|
36
|
-
context "with a transient variable assigned" do
|
37
|
-
let(:rockstar) { FactoryGirl.create(:user, rockstar: true, four: "1234") }
|
38
|
-
let(:rockstar_with_name) { FactoryGirl.create(:user, name: "Jane Doe", rockstar: true) }
|
39
|
-
let(:upcased_rockstar) { FactoryGirl.create(:user, rockstar: true, upcased: true) }
|
40
|
-
let(:groupie) { FactoryGirl.create(:user, rockstar: false) }
|
41
|
-
|
42
|
-
it "generates the correct attributes on a rockstar" do
|
43
|
-
expect(rockstar.name).to eq "John 1 - Rockstar"
|
44
|
-
expect(rockstar.email).to eq "john 1 - rockstar1234@example.com"
|
45
|
-
end
|
46
|
-
|
47
|
-
it "generates the correct attributes on an upcased rockstar" do
|
48
|
-
expect(upcased_rockstar.name).to eq "JOHN 1 - ROCKSTAR"
|
49
|
-
expect(upcased_rockstar.email).to eq "john 1 - rockstar4@example.com"
|
50
|
-
end
|
51
|
-
|
52
|
-
it "generates the correct attributes on a groupie" do
|
53
|
-
expect(groupie.name).to eq "John 1"
|
54
|
-
expect(groupie.email).to eq "john 14@example.com"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "generates the correct attributes on a rockstar with a name" do
|
58
|
-
expect(rockstar_with_name.name).to eq "Jane Doe"
|
59
|
-
expect(rockstar_with_name.email).to eq "jane doe4@example.com"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "without transient variables assigned" do
|
64
|
-
let(:rockstar) { FactoryGirl.create(:user) }
|
65
|
-
|
66
|
-
it "uses the default value of the attribute" do
|
67
|
-
expect(rockstar.name).to eq "John 1 - Rockstar"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe "transient sequences" do
|
73
|
-
before do
|
74
|
-
define_model("User", name: :string)
|
75
|
-
|
76
|
-
FactoryGirl.define do
|
77
|
-
factory :user do
|
78
|
-
ignore do
|
79
|
-
sequence(:counter)
|
80
|
-
end
|
81
|
-
|
82
|
-
name { "John Doe #{counter}" }
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
it "increments sequences correctly" do
|
88
|
-
expect(FactoryGirl.build(:user).name).to eq "John Doe 1"
|
89
|
-
expect(FactoryGirl.build(:user).name).to eq "John Doe 2"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "assigning values from a transient attribute" do
|
94
|
-
before do
|
95
|
-
define_class("User") do
|
96
|
-
attr_accessor :foo_id, :foo_name
|
97
|
-
end
|
98
|
-
|
99
|
-
define_class("Foo") do
|
100
|
-
attr_accessor :id, :name
|
101
|
-
def initialize(id, name)
|
102
|
-
@id = id
|
103
|
-
@name = name
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
FactoryGirl.define do
|
108
|
-
factory :user do
|
109
|
-
ignore do
|
110
|
-
foo { Foo.new('id-of-foo', 'name-of-foo')}
|
111
|
-
end
|
112
|
-
|
113
|
-
foo_id { foo.id }
|
114
|
-
foo_name { foo.name }
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
it "does not ignore an _id attribute that is an alias for a transient attribute" do
|
120
|
-
user = FactoryGirl.build(:user, :foo => Foo.new('passed-in-id-of-foo', 'passed-in-name-of-foo'))
|
121
|
-
expect(user.foo_id).to eq 'passed-in-id-of-foo'
|
122
|
-
expect(user.foo_name).to eq 'passed-in-name-of-foo'
|
123
|
-
end
|
124
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl, "aliases" do
|
4
|
-
context "aliases for an attribute" do
|
5
|
-
subject { FactoryGirl.aliases_for(:test) }
|
6
|
-
it { should include(:test) }
|
7
|
-
it { should include(:test_id) }
|
8
|
-
end
|
9
|
-
|
10
|
-
context "aliases for a foreign key" do
|
11
|
-
subject { FactoryGirl.aliases_for(:test_id) }
|
12
|
-
it { should include(:test) }
|
13
|
-
it { should include(:test_id) }
|
14
|
-
end
|
15
|
-
|
16
|
-
context "aliases for an attribute starting with an underscore" do
|
17
|
-
subject { FactoryGirl.aliases_for(:_id) }
|
18
|
-
it { should_not include(:id) }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe FactoryGirl, "after defining an alias" do
|
23
|
-
before do
|
24
|
-
FactoryGirl.aliases << [/(.*)_suffix/, '\1']
|
25
|
-
end
|
26
|
-
|
27
|
-
subject { FactoryGirl.aliases_for(:test_suffix) }
|
28
|
-
|
29
|
-
it { should include(:test) }
|
30
|
-
it { should include(:test_suffix_id) }
|
31
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Attribute::Association do
|
4
|
-
let(:name) { :author }
|
5
|
-
let(:factory) { :user }
|
6
|
-
let(:overrides) { { first_name: "John" } }
|
7
|
-
let(:association) { stub("association") }
|
8
|
-
|
9
|
-
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
|
10
|
-
before { subject.stubs(association: association) }
|
11
|
-
|
12
|
-
it { should be_association }
|
13
|
-
its(:name) { should eq name }
|
14
|
-
|
15
|
-
it "builds the association when calling the proc" do
|
16
|
-
expect(subject.to_proc.call).to eq association
|
17
|
-
end
|
18
|
-
|
19
|
-
it "builds the association when calling the proc" do
|
20
|
-
subject.to_proc.call
|
21
|
-
expect(subject).to have_received(:association).with(factory, overrides)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe FactoryGirl::Attribute::Association, "with a string name" do
|
26
|
-
subject { FactoryGirl::Attribute::Association.new("name", :user, {}) }
|
27
|
-
its(:name) { should eq :name }
|
28
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Attribute::Dynamic do
|
4
|
-
let(:name) { :first_name }
|
5
|
-
let(:block) { -> { } }
|
6
|
-
|
7
|
-
subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }
|
8
|
-
|
9
|
-
its(:name) { should eq name }
|
10
|
-
|
11
|
-
context "with a block returning a static value" do
|
12
|
-
let(:block) { -> { "value" } }
|
13
|
-
|
14
|
-
it "returns the value when executing the proc" do
|
15
|
-
expect(subject.to_proc.call).to eq "value"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context "with a block returning its block-level variable" do
|
20
|
-
let(:block) { ->(thing) { thing } }
|
21
|
-
|
22
|
-
it "returns self when executing the proc" do
|
23
|
-
expect(subject.to_proc.call).to eq subject
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context "with a block referencing an attribute on the attribute" do
|
28
|
-
let(:block) { -> { attribute_defined_on_attribute } }
|
29
|
-
let(:result) { "other attribute value" }
|
30
|
-
|
31
|
-
before do
|
32
|
-
subject.stubs(attribute_defined_on_attribute: result)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "evaluates the attribute from the attribute" do
|
36
|
-
expect(subject.to_proc.call).to eq result
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "with a block returning a sequence" do
|
41
|
-
let(:block) { -> { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }
|
42
|
-
|
43
|
-
it "raises a sequence abuse error" do
|
44
|
-
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe FactoryGirl::Attribute::Dynamic, "with a string name" do
|
50
|
-
subject { FactoryGirl::Attribute::Dynamic.new("name", false, -> { } ) }
|
51
|
-
its(:name) { should eq :name }
|
52
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Attribute::Sequence do
|
4
|
-
let(:sequence_name) { :name }
|
5
|
-
let(:name) { :first_name }
|
6
|
-
let(:sequence) { FactoryGirl::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } }
|
7
|
-
|
8
|
-
subject { FactoryGirl::Attribute::Sequence.new(name, sequence_name, false) }
|
9
|
-
before { FactoryGirl.register_sequence(sequence) }
|
10
|
-
|
11
|
-
its(:name) { should eq name }
|
12
|
-
|
13
|
-
it "assigns the next value in the sequence" do
|
14
|
-
expect(subject.to_proc.call).to eq "Name 5"
|
15
|
-
end
|
16
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Attribute::Static do
|
4
|
-
let(:name) { :first_name }
|
5
|
-
let(:value) { "John" }
|
6
|
-
|
7
|
-
subject { FactoryGirl::Attribute::Static.new(name, value, false) }
|
8
|
-
|
9
|
-
its(:name) { should eq name }
|
10
|
-
|
11
|
-
it "returns the value when executing the proc" do
|
12
|
-
expect(subject.to_proc.call).to eq value
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe FactoryGirl::Attribute::Static, "with a string name" do
|
17
|
-
subject { FactoryGirl::Attribute::Static.new("name", nil, false) }
|
18
|
-
its(:name) { should eq :name }
|
19
|
-
end
|
@@ -1,143 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe FactoryGirl::AttributeList, "#define_attribute" do
|
4
|
-
let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
|
5
|
-
let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) }
|
6
|
-
|
7
|
-
it "maintains a list of attributes" do
|
8
|
-
subject.define_attribute(static_attribute)
|
9
|
-
expect(subject.to_a).to eq [static_attribute]
|
10
|
-
|
11
|
-
subject.define_attribute(dynamic_attribute)
|
12
|
-
expect(subject.to_a).to eq [static_attribute, dynamic_attribute]
|
13
|
-
end
|
14
|
-
|
15
|
-
it "returns the attribute" do
|
16
|
-
expect(subject.define_attribute(static_attribute)).to eq static_attribute
|
17
|
-
expect(subject.define_attribute(dynamic_attribute)).to eq dynamic_attribute
|
18
|
-
end
|
19
|
-
|
20
|
-
it "raises if an attribute has already been defined" do
|
21
|
-
expect {
|
22
|
-
2.times { subject.define_attribute(static_attribute) }
|
23
|
-
}.to raise_error(FactoryGirl::AttributeDefinitionError, "Attribute already defined: full_name")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe FactoryGirl::AttributeList, "#define_attribute with a named attribute list" do
|
28
|
-
subject { FactoryGirl::AttributeList.new(:author) }
|
29
|
-
|
30
|
-
let(:association_with_same_name) { FactoryGirl::Attribute::Association.new(:author, :author, {}) }
|
31
|
-
let(:association_with_different_name) { FactoryGirl::Attribute::Association.new(:author, :post, {}) }
|
32
|
-
|
33
|
-
it "raises when the attribute is a self-referencing association" do
|
34
|
-
expect { subject.define_attribute(association_with_same_name) }.to raise_error(FactoryGirl::AssociationDefinitionError, "Self-referencing association 'author' in 'author'")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "does not raise when the attribute is not a self-referencing association" do
|
38
|
-
expect { subject.define_attribute(association_with_different_name) }.to_not raise_error
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe FactoryGirl::AttributeList, "#apply_attributes" do
|
43
|
-
let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "John Adams", false) }
|
44
|
-
let(:city_attribute) { FactoryGirl::Attribute::Static.new(:city, "Boston", false) }
|
45
|
-
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(model) { "#{model.full_name}@example.com" }) }
|
46
|
-
let(:login_attribute) { FactoryGirl::Attribute::Dynamic.new(:login, false, ->(model) { "username-#{model.full_name}" }) }
|
47
|
-
|
48
|
-
def list(*attributes)
|
49
|
-
FactoryGirl::AttributeList.new.tap do |list|
|
50
|
-
attributes.each { |attribute| list.define_attribute(attribute) }
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
it "adds attributes in the order defined regardless of attribute type" do
|
55
|
-
subject.define_attribute(full_name_attribute)
|
56
|
-
subject.define_attribute(login_attribute)
|
57
|
-
subject.apply_attributes(list(city_attribute, email_attribute))
|
58
|
-
expect(subject.to_a).to eq [full_name_attribute, login_attribute, city_attribute, email_attribute]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe FactoryGirl::AttributeList, "#associations" do
|
63
|
-
let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
|
64
|
-
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) }
|
65
|
-
let(:author_attribute) { FactoryGirl::Attribute::Association.new(:author, :user, {}) }
|
66
|
-
let(:profile_attribute) { FactoryGirl::Attribute::Association.new(:profile, :profile, {}) }
|
67
|
-
|
68
|
-
before do
|
69
|
-
subject.define_attribute(full_name_attribute)
|
70
|
-
subject.define_attribute(email_attribute)
|
71
|
-
subject.define_attribute(author_attribute)
|
72
|
-
subject.define_attribute(profile_attribute)
|
73
|
-
end
|
74
|
-
|
75
|
-
it "returns associations" do
|
76
|
-
expect(subject.associations.to_a).to eq [author_attribute, profile_attribute]
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe FactoryGirl::AttributeList, "filter based on ignored attributes" do
|
81
|
-
def build_ignored_attribute(name)
|
82
|
-
FactoryGirl::Attribute::Static.new(name, "value", true)
|
83
|
-
end
|
84
|
-
|
85
|
-
def build_non_ignored_attribute(name)
|
86
|
-
FactoryGirl::Attribute::Static.new(name, "value", false)
|
87
|
-
end
|
88
|
-
|
89
|
-
before do
|
90
|
-
subject.define_attribute(build_ignored_attribute(:comments_count))
|
91
|
-
subject.define_attribute(build_ignored_attribute(:posts_count))
|
92
|
-
subject.define_attribute(build_non_ignored_attribute(:email))
|
93
|
-
subject.define_attribute(build_non_ignored_attribute(:first_name))
|
94
|
-
subject.define_attribute(build_non_ignored_attribute(:last_name))
|
95
|
-
end
|
96
|
-
|
97
|
-
it "filters #ignored attributes" do
|
98
|
-
expect(subject.ignored.map(&:name)).to eq [:comments_count, :posts_count]
|
99
|
-
end
|
100
|
-
|
101
|
-
it "filters #non_ignored attributes" do
|
102
|
-
expect(subject.non_ignored.map(&:name)).to eq [:email, :first_name, :last_name]
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe FactoryGirl::AttributeList, "generating names" do
|
107
|
-
def build_ignored_attribute(name)
|
108
|
-
FactoryGirl::Attribute::Static.new(name, "value", true)
|
109
|
-
end
|
110
|
-
|
111
|
-
def build_non_ignored_attribute(name)
|
112
|
-
FactoryGirl::Attribute::Static.new(name, "value", false)
|
113
|
-
end
|
114
|
-
|
115
|
-
def build_association(name)
|
116
|
-
FactoryGirl::Attribute::Association.new(name, :user, {})
|
117
|
-
end
|
118
|
-
|
119
|
-
before do
|
120
|
-
subject.define_attribute(build_ignored_attribute(:comments_count))
|
121
|
-
subject.define_attribute(build_ignored_attribute(:posts_count))
|
122
|
-
subject.define_attribute(build_non_ignored_attribute(:email))
|
123
|
-
subject.define_attribute(build_non_ignored_attribute(:first_name))
|
124
|
-
subject.define_attribute(build_non_ignored_attribute(:last_name))
|
125
|
-
subject.define_attribute(build_association(:avatar))
|
126
|
-
end
|
127
|
-
|
128
|
-
it "knows all its #names" do
|
129
|
-
expect(subject.names).to eq [:comments_count, :posts_count, :email, :first_name, :last_name, :avatar]
|
130
|
-
end
|
131
|
-
|
132
|
-
it "knows all its #names for #ignored attributes" do
|
133
|
-
expect(subject.ignored.names).to eq [:comments_count, :posts_count]
|
134
|
-
end
|
135
|
-
|
136
|
-
it "knows all its #names for #non_ignored attributes" do
|
137
|
-
expect(subject.non_ignored.names).to eq [:email, :first_name, :last_name, :avatar]
|
138
|
-
end
|
139
|
-
|
140
|
-
it "knows all its #names for #associations" do
|
141
|
-
expect(subject.associations.names).to eq [:avatar]
|
142
|
-
end
|
143
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Attribute do
|
4
|
-
let(:name) { "user" }
|
5
|
-
subject { FactoryGirl::Attribute.new(name, false) }
|
6
|
-
|
7
|
-
its(:name) { should eq name.to_sym }
|
8
|
-
it { should_not be_association }
|
9
|
-
|
10
|
-
it "raises an error when defining an attribute writer" do
|
11
|
-
error_message = %{factory_girl uses 'test value' syntax rather than 'test = value'}
|
12
|
-
expect {
|
13
|
-
FactoryGirl::Attribute.new('test=', false)
|
14
|
-
}.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
|
15
|
-
end
|
16
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Callback do
|
4
|
-
it "has a name" do
|
5
|
-
expect(FactoryGirl::Callback.new(:after_create, -> {}).name).to eq :after_create
|
6
|
-
end
|
7
|
-
|
8
|
-
it "converts strings to symbols" do
|
9
|
-
expect(FactoryGirl::Callback.new("after_create", -> {}).name).to eq :after_create
|
10
|
-
end
|
11
|
-
|
12
|
-
it "runs its block with no parameters" do
|
13
|
-
ran_with = nil
|
14
|
-
FactoryGirl::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two)
|
15
|
-
expect(ran_with).to eq []
|
16
|
-
end
|
17
|
-
|
18
|
-
it "runs its block with one parameter" do
|
19
|
-
ran_with = nil
|
20
|
-
FactoryGirl::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two)
|
21
|
-
expect(ran_with).to eq [:one]
|
22
|
-
end
|
23
|
-
|
24
|
-
it "runs its block with two parameters" do
|
25
|
-
ran_with = nil
|
26
|
-
FactoryGirl::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two)
|
27
|
-
expect(ran_with).to eq [:one, :two]
|
28
|
-
end
|
29
|
-
|
30
|
-
it "allows valid callback names to be assigned" do
|
31
|
-
FactoryGirl.callback_names.each do |callback_name|
|
32
|
-
expect { FactoryGirl::Callback.new(callback_name, -> {}) }.
|
33
|
-
to_not raise_error(FactoryGirl::InvalidCallbackNameError)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
it "raises if an invalid callback name is assigned" do
|
38
|
-
expect { FactoryGirl::Callback.new(:magic_fairies, -> {}) }.
|
39
|
-
to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/)
|
40
|
-
end
|
41
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Declaration::Implicit do
|
4
|
-
let(:name) { :author }
|
5
|
-
let(:declaration) { FactoryGirl::Declaration::Implicit.new(name) }
|
6
|
-
subject { declaration.to_attributes.first }
|
7
|
-
|
8
|
-
context "with a known factory" do
|
9
|
-
before do
|
10
|
-
FactoryGirl.factories.stubs(:registered? => true)
|
11
|
-
end
|
12
|
-
|
13
|
-
it { should be_association }
|
14
|
-
its(:factory) { should eq name }
|
15
|
-
end
|
16
|
-
|
17
|
-
context "with a known sequence" do
|
18
|
-
before do
|
19
|
-
FactoryGirl.sequences.stubs(:registered? => true)
|
20
|
-
end
|
21
|
-
|
22
|
-
it { should_not be_association }
|
23
|
-
it { should be_a(FactoryGirl::Attribute::Sequence) }
|
24
|
-
end
|
25
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe FactoryGirl::DeclarationList, "#attributes" do
|
4
|
-
let(:static_attribute_1) { stub("static attribute 1") }
|
5
|
-
let(:static_attribute_2) { stub("static attribute 2") }
|
6
|
-
let(:dynamic_attribute_1) { stub("dynamic attribute 1") }
|
7
|
-
let(:static_declaration) { stub("static declaration", to_attributes: [static_attribute_1, static_attribute_2]) }
|
8
|
-
let(:dynamic_declaration) { stub("static declaration", to_attributes: [dynamic_attribute_1]) }
|
9
|
-
|
10
|
-
it "returns an AttributeList" do
|
11
|
-
expect(subject.attributes).to be_a(FactoryGirl::AttributeList)
|
12
|
-
end
|
13
|
-
|
14
|
-
let(:attribute_list) { stub("attribute list", define_attribute: true) }
|
15
|
-
|
16
|
-
it "defines each attribute on the attribute list" do
|
17
|
-
FactoryGirl::AttributeList.stubs(new: attribute_list)
|
18
|
-
|
19
|
-
subject.declare_attribute(static_declaration)
|
20
|
-
subject.declare_attribute(dynamic_declaration)
|
21
|
-
|
22
|
-
subject.attributes
|
23
|
-
|
24
|
-
expect(attribute_list).to have_received(:define_attribute).with(static_attribute_1)
|
25
|
-
expect(attribute_list).to have_received(:define_attribute).with(static_attribute_2)
|
26
|
-
expect(attribute_list).to have_received(:define_attribute).with(dynamic_attribute_1)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe FactoryGirl::DeclarationList, "#declare_attribute" do
|
31
|
-
let(:declaration_1) { stub("declaration", name: "declaration 1") }
|
32
|
-
let(:declaration_2) { stub("declaration", name: "declaration 2") }
|
33
|
-
let(:declaration_with_same_name) { stub("declaration", name: "declaration 1") }
|
34
|
-
|
35
|
-
context "when not overridable" do
|
36
|
-
it "adds the declaration to the list" do
|
37
|
-
subject.declare_attribute(declaration_1)
|
38
|
-
expect(subject.to_a).to eq [declaration_1]
|
39
|
-
|
40
|
-
subject.declare_attribute(declaration_2)
|
41
|
-
expect(subject.to_a).to eq [declaration_1, declaration_2]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "when overridable" do
|
46
|
-
before { subject.overridable }
|
47
|
-
|
48
|
-
it "adds the declaration to the list" do
|
49
|
-
subject.declare_attribute(declaration_1)
|
50
|
-
expect(subject.to_a).to eq [declaration_1]
|
51
|
-
|
52
|
-
subject.declare_attribute(declaration_2)
|
53
|
-
expect(subject.to_a).to eq [declaration_1, declaration_2]
|
54
|
-
end
|
55
|
-
|
56
|
-
it "deletes declarations with the same name" do
|
57
|
-
subject.declare_attribute(declaration_1)
|
58
|
-
expect(subject.to_a).to eq [declaration_1]
|
59
|
-
|
60
|
-
subject.declare_attribute(declaration_2)
|
61
|
-
expect(subject.to_a).to eq [declaration_1, declaration_2]
|
62
|
-
|
63
|
-
subject.declare_attribute(declaration_with_same_name)
|
64
|
-
expect(subject.to_a).to eq [declaration_2, declaration_with_same_name]
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|