2018-08-04 10:56:37 -04:00
|
|
|
describe "FactoryBot.lint" do
|
|
|
|
it "raises when a factory is invalid" do
|
|
|
|
define_model "User", name: :string do
|
2014-02-07 10:09:20 -05:00
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
define_model "AlwaysValid"
|
2014-02-07 10:09:20 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2014-02-07 10:09:20 -05:00
|
|
|
factory :user do
|
|
|
|
factory :admin_user
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :always_valid
|
|
|
|
end
|
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
error_message = <<~ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
2014-02-07 10:09:20 -05:00
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
* user - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
|
|
|
* admin_user - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
2014-02-07 10:09:20 -05:00
|
|
|
ERROR_MESSAGE
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint
|
|
|
|
end.to raise_error FactoryBot::InvalidFactoryError, error_message
|
2014-02-07 10:09:20 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "does not raise when all factories are valid" do
|
|
|
|
define_model "User", name: :string do
|
2014-02-07 10:09:20 -05:00
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2014-02-07 10:09:20 -05:00
|
|
|
factory :user do
|
2018-08-04 10:56:37 -04:00
|
|
|
name { "assigned" }
|
2014-02-07 10:09:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect { FactoryBot.lint }.not_to raise_error
|
2014-02-07 10:09:20 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "allows for selective linting" do
|
|
|
|
define_model "InvalidThing", name: :string do
|
2014-10-17 12:03:22 -04:00
|
|
|
validates :name, presence: true
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
define_model "ValidThing", name: :string
|
2014-05-29 16:06:08 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2014-05-29 16:06:08 -04:00
|
|
|
factory :valid_thing
|
|
|
|
factory :invalid_thing
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
only_valid_factories = FactoryBot.factories.reject do |factory|
|
2014-05-29 16:06:08 -04:00
|
|
|
factory.name =~ /invalid/
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint only_valid_factories
|
2014-05-29 16:06:08 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
2015-07-31 23:07:49 -04:00
|
|
|
|
|
|
|
describe "trait validation" do
|
|
|
|
context "enabled" do
|
|
|
|
it "raises if a trait produces an invalid object" do
|
|
|
|
define_model "User", name: :string do
|
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2015-07-31 23:07:49 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Yep" }
|
2015-07-31 23:07:49 -04:00
|
|
|
trait :unnamed do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { nil }
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
error_message = <<~ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
2015-07-31 23:07:49 -04:00
|
|
|
|
2018-10-07 22:46:58 -04:00
|
|
|
* user+unnamed - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
2015-07-31 23:07:49 -04:00
|
|
|
ERROR_MESSAGE
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint traits: true
|
|
|
|
end.to raise_error FactoryBot::InvalidFactoryError, error_message
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not raise if a trait produces a valid object" do
|
|
|
|
define_model "User", name: :string do
|
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2015-07-31 23:07:49 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Yep" }
|
2015-07-31 23:07:49 -04:00
|
|
|
trait :renamed do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Yessir" }
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint traits: true
|
2015-07-31 23:07:49 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "disabled" do
|
|
|
|
it "does not raises if a trait produces an invalid object" do
|
|
|
|
define_model "User", name: :string do
|
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2015-07-31 23:07:49 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Yep" }
|
2015-07-31 23:07:49 -04:00
|
|
|
trait :unnamed do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { nil }
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint traits: false
|
|
|
|
FactoryBot.lint
|
2015-07-31 23:07:49 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-28 08:22:16 -04:00
|
|
|
|
|
|
|
describe "factory strategy for linting" do
|
|
|
|
it "uses the requested strategy" do
|
|
|
|
define_class "User" do
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def save!
|
|
|
|
raise "expected :build strategy, #save! shouldn't be invoked"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2017-09-28 08:22:16 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Barbara" }
|
2017-09-28 08:22:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint strategy: :build
|
2017-09-28 08:22:16 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the requested strategy during trait validation" do
|
|
|
|
define_class "User" do
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def save!
|
|
|
|
raise "expected :build strategy, #save! shouldn't be invoked"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2017-09-28 08:22:16 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Barbara" }
|
2017-09-28 08:22:16 -04:00
|
|
|
|
|
|
|
trait :male do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "Bob" }
|
2017-09-28 08:22:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint traits: true, strategy: :build
|
2017-09-28 08:22:16 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2018-11-21 15:28:28 -05:00
|
|
|
|
|
|
|
describe "verbose linting" do
|
|
|
|
it "prints the backtrace for each factory error" do
|
|
|
|
define_class("InvalidThing") do
|
|
|
|
def save!
|
|
|
|
raise "invalid"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryBot.define do
|
|
|
|
factory :invalid_thing
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
|
|
|
FactoryBot.lint(verbose: true)
|
|
|
|
end.to raise_error(
|
|
|
|
FactoryBot::InvalidFactoryError,
|
|
|
|
%r{#{__FILE__}:\d*:in `save!'},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2014-02-07 10:09:20 -05:00
|
|
|
end
|