mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
parent
c61808742c
commit
5f465f718f
4 changed files with 82 additions and 18 deletions
|
@ -104,6 +104,18 @@ database, as built factories will create associated records. The provided
|
|||
example above uses the database_cleaner gem to clear out the database; be sure
|
||||
to add the gem to your Gemfile under the appropriate groups.
|
||||
|
||||
You can lint factories selectively by passing only factories you want linted:
|
||||
|
||||
```ruby
|
||||
factories_to_lint = FactoryGirl.factories.reject do |factory|
|
||||
factory.name =~ /^old_/
|
||||
end
|
||||
|
||||
FactoryGirl.lint factories_to_lint
|
||||
```
|
||||
|
||||
This would lint all factories that aren't prefixed with `old_`.
|
||||
|
||||
Defining factories
|
||||
------------------
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ require 'factory_girl/decorator/class_key_hash'
|
|||
require 'factory_girl/decorator/disallows_duplicates_registry'
|
||||
require 'factory_girl/decorator/invocation_tracker'
|
||||
require 'factory_girl/decorator/new_constructor'
|
||||
require 'factory_girl/linter'
|
||||
require 'factory_girl/version'
|
||||
|
||||
module FactoryGirl
|
||||
|
@ -54,24 +55,8 @@ module FactoryGirl
|
|||
@configuration = nil
|
||||
end
|
||||
|
||||
def self.lint
|
||||
invalid_factories = FactoryGirl.factories.select do |factory|
|
||||
built_factory = FactoryGirl.build(factory.name)
|
||||
|
||||
if built_factory.respond_to?(:valid?)
|
||||
!built_factory.valid?
|
||||
end
|
||||
end
|
||||
|
||||
if invalid_factories.any?
|
||||
error_message = <<-ERROR_MESSAGE.strip
|
||||
The following factories are invalid:
|
||||
|
||||
#{invalid_factories.map {|factory| "* #{factory.name}" }.join("\n")}
|
||||
ERROR_MESSAGE
|
||||
|
||||
raise InvalidFactoryError, error_message
|
||||
end
|
||||
def self.lint(factories_to_lint = FactoryGirl.factories)
|
||||
Linter.lint!(factories_to_lint)
|
||||
end
|
||||
|
||||
class << self
|
||||
|
|
40
lib/factory_girl/linter.rb
Normal file
40
lib/factory_girl/linter.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
module FactoryGirl
|
||||
class Linter
|
||||
def self.lint!(factories_to_lint)
|
||||
new(factories_to_lint).lint!
|
||||
end
|
||||
|
||||
def initialize(factories_to_lint)
|
||||
@factories_to_lint = factories_to_lint
|
||||
@invalid_factories = calculate_invalid_factories
|
||||
end
|
||||
|
||||
def lint!
|
||||
if invalid_factories.any?
|
||||
raise InvalidFactoryError, error_message
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :factories_to_lint, :invalid_factories
|
||||
|
||||
def calculate_invalid_factories
|
||||
factories_to_lint.select do |factory|
|
||||
built_factory = FactoryGirl.build(factory.name)
|
||||
|
||||
if built_factory.respond_to?(:valid?)
|
||||
!built_factory.valid?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def error_message
|
||||
<<-ERROR_MESSAGE.strip
|
||||
The following factories are invalid:
|
||||
|
||||
#{invalid_factories.map {|factory| "* #{factory.name}" }.join("\n")}
|
||||
ERROR_MESSAGE
|
||||
end
|
||||
end
|
||||
end
|
|
@ -52,4 +52,31 @@ The following factories are invalid:
|
|||
expect(Thing.new).not_to respond_to(:valid?)
|
||||
expect { FactoryGirl.lint }.not_to raise_error
|
||||
end
|
||||
|
||||
it 'allows for selective linting' do
|
||||
define_class 'InvalidThing' do
|
||||
def valid?
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
define_class 'ValidThing' do
|
||||
def valid?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :valid_thing
|
||||
factory :invalid_thing
|
||||
end
|
||||
|
||||
expect do
|
||||
only_valid_factories = FactoryGirl.factories.reject do |factory|
|
||||
factory.name =~ /invalid/
|
||||
end
|
||||
|
||||
FactoryGirl.lint only_valid_factories
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue