Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding error messages to lint output #692

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/factory_girl/linter.rb
Expand Up @@ -6,34 +6,34 @@ def self.lint!(factories_to_lint)

def initialize(factories_to_lint)
@factories_to_lint = factories_to_lint
@invalid_factories = calculate_invalid_factories
@invalid_factory_messages = calculate_invalid_factory_messages
end

def lint!
if invalid_factories.any?
if invalid_factory_messages.any?
raise InvalidFactoryError, error_message
end
end

private

attr_reader :factories_to_lint, :invalid_factories
attr_reader :factories_to_lint, :invalid_factory_messages

def calculate_invalid_factories
factories_to_lint.select do |factory|
def calculate_invalid_factory_messages
factories_to_lint.map do |factory|
built_factory = FactoryGirl.build(factory.name)

if built_factory.respond_to?(:valid?)
!built_factory.valid?
if built_factory.respond_to?(:valid?) && !built_factory.valid?
"* #{factory.name} -- #{built_factory.errors.full_messages.join('; ')}"
end
end
end.compact
end

def error_message
<<-ERROR_MESSAGE.strip
The following factories are invalid:

#{invalid_factories.map {|factory| "* #{factory.name}" }.join("\n")}
#{invalid_factory_messages.join("\n")}
ERROR_MESSAGE
end
end
Expand Down
26 changes: 24 additions & 2 deletions spec/acceptance/lint_spec.rb
Expand Up @@ -19,8 +19,30 @@
error_message = <<-ERROR_MESSAGE.strip
The following factories are invalid:

* user
* admin_user
* user -- Name can't be blank
* admin_user -- Name can't be blank
ERROR_MESSAGE

expect do
FactoryGirl.lint
end.to raise_error FactoryGirl::InvalidFactoryError, error_message
end

it 'lists all errors when multiple are present' do
define_model 'Person', first_name: :string, last_name: :string do
validates :first_name, presence: true
validates :last_name, presence: true
end

FactoryGirl.define do
factory :person do
end
end

error_message = <<-ERROR_MESSAGE.strip
The following factories are invalid:

* person -- First name can't be blank; Last name can't be blank
ERROR_MESSAGE

expect do
Expand Down