2017-10-20 15:20:28 -04:00
|
|
|
describe 'FactoryBot.lint' do
|
2014-02-07 10:09:20 -05:00
|
|
|
it 'raises when a factory is invalid' do
|
|
|
|
define_model 'User', name: :string do
|
|
|
|
validates :name, presence: true
|
|
|
|
end
|
|
|
|
|
|
|
|
define_model 'AlwaysValid'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
error_message = <<-ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
|
|
|
|
2014-10-17 12:03:22 -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
|
|
|
|
|
|
|
|
it 'does not raise when all factories are valid' do
|
|
|
|
define_model 'User', name: :string do
|
|
|
|
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
|
|
|
|
name 'assigned'
|
|
|
|
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
|
|
|
|
|
2014-05-29 16:06:08 -04:00
|
|
|
it 'allows for selective linting' do
|
2014-10-17 12:03:22 -04:00
|
|
|
define_model 'InvalidThing', name: :string do
|
|
|
|
validates :name, presence: true
|
2014-05-29 16:06:08 -04:00
|
|
|
end
|
|
|
|
|
2014-10-17 12:03:22 -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
|
|
|
|
|
|
|
it "allows for explicit factories and additional options" 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
|
|
|
|
name "assigned"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint FactoryBot.factories, validate_traits: true
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows for additional options without explicit factories" 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
|
|
|
|
name "assigned"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.lint validate_traits: true
|
2015-07-31 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
name "Yep"
|
|
|
|
trait :unnamed do
|
|
|
|
name nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error_message = <<-ERROR_MESSAGE.strip
|
|
|
|
The following factories are invalid:
|
|
|
|
|
|
|
|
* user+unnamed - Validation failed: Name can't be blank (ActiveRecord::RecordInvalid)
|
|
|
|
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
|
|
|
|
name "Yep"
|
|
|
|
trait :renamed do
|
|
|
|
name "Yessir"
|
|
|
|
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
|
|
|
|
name "Yep"
|
|
|
|
trait :unnamed do
|
|
|
|
name nil
|
|
|
|
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
|
|
|
|
name "Barbara"
|
|
|
|
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
|
|
|
|
name "Barbara"
|
|
|
|
|
|
|
|
trait :male do
|
|
|
|
name "Bob"
|
|
|
|
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
|
2014-02-07 10:09:20 -05:00
|
|
|
end
|