1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
describe "modifying factories" do
include FactoryBot::Syntax::Methods
before do
define_model("User", name: :string, admin: :boolean, email: :string, login: :string)
FactoryBot.define do
sequence(:email) { |n| "user#{n}@example.com" }
factory :user do
email
after(:create) do |user|
user.login = user.name.upcase if user.name
end
factory :admin do
admin { true }
end
end
end
end
context "simple modification" do
before do
FactoryBot.modify do
factory :user do
name { "Great User" }
end
end
end
subject { create(:user) }
its(:name) { should eq "Great User" }
its(:login) { should eq "GREAT USER" }
it "doesn't allow the factory to be subsequently defined" do
expect {
FactoryBot.define { factory :user }
}.to raise_error(FactoryBot::DuplicateDefinitionError, "Factory already registered: user")
end
it "does allow the factory to be subsequently modified" do
FactoryBot.modify do
factory :user do
name { "Overridden again!" }
end
end
expect(create(:user).name).to eq "Overridden again!"
end
end
context "adding callbacks" do
before do
FactoryBot.modify do
factory :user do
name { "Great User" }
after(:create) do |user|
user.name = user.name.downcase
user.login = nil
end
end
end
end
subject { create(:user) }
its(:name) { should eq "great user" }
its(:login) { should be_nil }
end
context "reusing traits" do
before do
FactoryBot.define do
trait :rockstar do
name { "Johnny Rockstar!!!" }
end
end
FactoryBot.modify do
factory :user do
rockstar
email { "#{name}@example.com" }
end
end
end
subject { create(:user) }
its(:name) { should eq "Johnny Rockstar!!!" }
its(:email) { should eq "Johnny Rockstar!!!@example.com" }
its(:login) { should eq "JOHNNY ROCKSTAR!!!" }
end
context "redefining attributes" do
before do
FactoryBot.modify do
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) }
its(:name) { should eq "Great User" }
its(:email) { should eq "Great User-modified@example.com" }
end
context "overriding the email" do
subject { create(:user, email: "perfect@example.com") }
its(:name) { should eq "Great User" }
its(:email) { should eq "perfect@example.com" }
end
context "overriding the name" do
subject { create(:user, name: "wonderful") }
its(:name) { should eq "wonderful" }
its(:email) { should eq "wonderful-modified@example.com" }
end
end
context "creating admin" do
context "without overrides" do
subject { create(:admin) }
its(:name) { should eq "Great User" }
its(:email) { should eq "Great User-modified@example.com" }
its(:admin) { should be true }
end
context "overriding the email" do
subject { create(:admin, email: "perfect@example.com") }
its(:name) { should eq "Great User" }
its(:email) { should eq "perfect@example.com" }
its(:admin) { should be true }
end
context "overriding the name" do
subject { create(:admin, name: "wonderful") }
its(:name) { should eq "wonderful" }
its(:email) { should eq "wonderful-modified@example.com" }
its(:admin) { should be true }
end
end
end
it "doesn't overwrite already defined child's attributes" do
FactoryBot.modify do
factory :user do
admin { false }
end
end
expect(create(:admin)).to be_admin
end
it "allows for overriding child classes" do
FactoryBot.modify do
factory :admin do
admin { false }
end
end
expect(create(:admin)).not_to be_admin
end
it "raises an exception if the factory was not defined before" do
modify_unknown_factory = -> do
FactoryBot.modify do
factory :unknown_factory
end
end
expect(&modify_unknown_factory).to raise_error(KeyError)
end
end
|