2011-09-02 12:05:00 -04:00
|
|
|
describe "modifying factories" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-09-02 12:05:00 -04:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('User', name: :string, admin: :boolean, email: :string, login: :string)
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:email) { |n| "user#{n}@example.com" }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
|
|
|
factory :user do
|
|
|
|
email
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:create) do |user|
|
2011-09-02 12:05:00 -04:00
|
|
|
user.login = user.name.upcase if user.name
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :admin do
|
|
|
|
admin true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "simple modification" do
|
|
|
|
before do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
name "Great User"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { create(:user) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Great User" }
|
|
|
|
its(:login) { should eq "GREAT USER" }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
|
|
|
it "doesn't allow the factory to be subsequently defined" do
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define { factory :user }
|
|
|
|
end.to raise_error(FactoryBot::DuplicateDefinitionError, "Factory already registered: user")
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does allow the factory to be subsequently modified" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
name "Overridden again!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(create(:user).name).to eq "Overridden again!"
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "adding callbacks" do
|
|
|
|
before do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
name "Great User"
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:create) do |user|
|
2011-09-02 12:05:00 -04:00
|
|
|
user.name = user.name.downcase
|
|
|
|
user.login = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { create(:user) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "great user" }
|
2011-09-02 12:05:00 -04:00
|
|
|
its(:login) { should be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "reusing traits" do
|
|
|
|
before do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-09-02 12:05:00 -04:00
|
|
|
trait :rockstar do
|
|
|
|
name "Johnny Rockstar!!!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
rockstar
|
|
|
|
email { "#{name}@example.com" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { create(:user) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Johnny Rockstar!!!" }
|
|
|
|
its(:email) { should eq "Johnny Rockstar!!!@example.com" }
|
|
|
|
its(:login) { should eq "JOHNNY ROCKSTAR!!!" }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "redefining attributes" do
|
|
|
|
before do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
email { "#{name}-modified@example.com" }
|
|
|
|
name "Great User"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating user" do
|
|
|
|
context "without overrides" do
|
|
|
|
subject { create(:user) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Great User" }
|
|
|
|
its(:email) { should eq "Great User-modified@example.com" }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "overriding dynamic attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { create(:user, email: "perfect@example.com") }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Great User" }
|
|
|
|
its(:email) { should eq "perfect@example.com" }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "overriding static attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { create(:user, name: "wonderful") }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "wonderful" }
|
|
|
|
its(:email) { should eq "wonderful-modified@example.com" }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating admin" do
|
|
|
|
context "without overrides" do
|
|
|
|
subject { create(:admin) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Great User" }
|
|
|
|
its(:email) { should eq "Great User-modified@example.com" }
|
2015-09-11 09:16:23 -04:00
|
|
|
its(:admin) { should be true }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "overriding dynamic attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { create(:admin, email: "perfect@example.com") }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Great User" }
|
|
|
|
its(:email) { should eq "perfect@example.com" }
|
2015-09-11 09:16:23 -04:00
|
|
|
its(:admin) { should be true }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "overriding static attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { create(:admin, name: "wonderful") }
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "wonderful" }
|
|
|
|
its(:email) { should eq "wonderful-modified@example.com" }
|
2015-09-11 09:16:23 -04:00
|
|
|
its(:admin) { should be true }
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't overwrite already defined child's attributes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :user do
|
|
|
|
admin false
|
|
|
|
end
|
|
|
|
end
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(create(:admin)).to be_admin
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows for overriding child classes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :admin do
|
|
|
|
admin false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(create(:admin)).not_to be_admin
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception if the factory was not defined before" do
|
2012-04-20 16:58:26 -04:00
|
|
|
expect {
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :unknown_factory
|
|
|
|
end
|
2012-04-20 16:58:26 -04:00
|
|
|
}.to raise_error(ArgumentError)
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
end
|