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
|
2018-10-07 21:45:51 -04: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
|
2018-07-29 11:30:02 -04:00
|
|
|
admin { true }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Great User" }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Overridden again!" }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Johnny Rockstar!!!" }
|
2011-09-02 12:05:00 -04:00
|
|
|
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" }
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Great User" }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
|
|
|
|
2018-09-28 16:57:46 -04:00
|
|
|
context "overriding the email" 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
|
|
|
|
|
2018-09-28 16:57:46 -04:00
|
|
|
context "overriding the name" 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
|
|
|
|
|
2018-09-28 16:57:46 -04:00
|
|
|
context "overriding the email" 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
|
|
|
|
|
2018-09-28 16:57:46 -04:00
|
|
|
context "overriding the name" 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
|
2018-07-29 11:30:02 -04:00
|
|
|
admin { false }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
admin { false }
|
2011-09-02 12:05:00 -04:00
|
|
|
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
|
2018-10-21 11:39:48 -04:00
|
|
|
modify_unknown_factory = -> do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-02 12:05:00 -04:00
|
|
|
factory :unknown_factory
|
|
|
|
end
|
2018-10-21 11:39:48 -04:00
|
|
|
end
|
|
|
|
|
2018-12-14 08:27:48 -05:00
|
|
|
expect(modify_unknown_factory).to raise_error(KeyError)
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
end
|