mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
a56b68bc9c
* Alphabetize gem listing in various Gemfiles [Rubocop Bundler/OrderedGems] * Fix alignment of if/else/end statement [Rubocop Layout/ElseAlignment] * Method definitions should have a empty line between them [Rubocop Layout/EmptyLineBetweenDefs] * Modules, Classes, and blocks should have an empty line around them [Rubocop] Cops: Layout/EmptyLinesAroundBlockBody Layout/EmptyLinesAroundModuleBody Layout/EmptyLinesAroundClassBody Layout/EmptyLinesAroundAccessModifier * Keep a blank line before and after access modifiers [Rubocop Layout/EmptyLinesAroundAccessModifier] * Remove misc extra whitespace [Rubocop Layout/ExtraSpacing] * Indent the first line of the right-hand-side of a multi-line assignment [Rubocop Layout/IndentAssignment] * Remove extraneous whitespace [Rubocop] Cops: Layout/IndentationWidth Layout/LeadingCommentSpace Layout/SpaceAroundEqualsInParameterDefault Layout/SpaceInsideArrayLiteralBrackets Layout/SpaceInsideBlockBraces Layout/SpaceInsideParens Layout/TrailingBlankLines * Revert rubocop changes to gemfiles; exclude files from rubocop checks The files in gemfiles/ are generated by Appraisal, so we shouldn't edit them. Instead, let's tell RuboCop to exclude this directory.
88 lines
2.5 KiB
Ruby
88 lines
2.5 KiB
Ruby
describe "an instance generated by a factory that inherits from another factory" do
|
|
before do
|
|
define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string)
|
|
|
|
FactoryBot.define do
|
|
factory :user do
|
|
name { "John" }
|
|
email { "#{name.downcase}@example.com" }
|
|
login { email }
|
|
|
|
factory :admin do
|
|
name { "admin" }
|
|
admin { true }
|
|
upper_email { email.upcase }
|
|
end
|
|
|
|
factory :guest do
|
|
email { "#{name}-guest@example.com" }
|
|
end
|
|
|
|
factory :no_email do
|
|
email { "" }
|
|
end
|
|
|
|
factory :bill do
|
|
name { "Bill" } # block to make attribute dynamic
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "the parent class" do
|
|
subject { FactoryBot.create(:user) }
|
|
it { should_not be_admin }
|
|
its(:email) { should eq "john@example.com" }
|
|
end
|
|
|
|
describe "the child class redefining parent's static method used by a dynamic method" do
|
|
subject { FactoryBot.create(:admin) }
|
|
it { should be_kind_of(User) }
|
|
it { should be_admin }
|
|
its(:name) { should eq "admin" }
|
|
its(:email) { should eq "admin@example.com" }
|
|
its(:upper_email) { should eq "ADMIN@EXAMPLE.COM" }
|
|
end
|
|
|
|
describe "the child class redefining parent's dynamic method" do
|
|
subject { FactoryBot.create(:guest) }
|
|
it { should_not be_admin }
|
|
its(:name) { should eq "John" }
|
|
its(:email) { should eql "John-guest@example.com" }
|
|
its(:login) { should eq "John-guest@example.com" }
|
|
end
|
|
|
|
describe "the child class redefining parent's dynamic attribute with static attribute" do
|
|
subject { FactoryBot.create(:no_email) }
|
|
its(:email) { should eq "" }
|
|
end
|
|
|
|
describe "the child class redefining parent's static attribute with dynamic attribute" do
|
|
subject { FactoryBot.create(:bill) }
|
|
its(:name) { should eq "Bill" }
|
|
end
|
|
end
|
|
|
|
describe "nested factories with different parents" do
|
|
before do
|
|
define_model("User", name: :string)
|
|
|
|
FactoryBot.define do
|
|
factory :user do
|
|
name { "Basic User" }
|
|
|
|
factory :male_user do
|
|
name { "John Doe" }
|
|
end
|
|
|
|
factory :uppercase_male_user, parent: :male_user do
|
|
after(:build) { |user| user.name = user.name.upcase }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
it "honors :parent over the factory block nesting" do
|
|
expect(FactoryBot.build(:uppercase_male_user).name).to eq "JOHN DOE"
|
|
end
|
|
end
|