From 3d7917d2d52823b7ac96f40ef4dc3c42ae9add9e Mon Sep 17 00:00:00 2001 From: Avielle Wolfe Date: Fri, 20 Oct 2017 14:32:09 -0400 Subject: [PATCH] Replace 'girl' with 'bot' everywhere Also: add a deprecation warning to factory_girl, asking users to switch to factory_bot https://github.com/thoughtbot/factory_girl/issues/921 --- CONTRIBUTING.md | 6 +- GETTING_STARTED.md | 184 +++++++++--------- Gemfile | 2 +- Gemfile.lock | 97 ++++----- NEWS | 32 +-- README.md | 44 +++-- Rakefile | 4 +- factory_bot.gemspec | 36 ++++ factory_girl.gemspec | 7 +- features/find_definitions.feature | 30 +-- .../step_definitions/factory_bot_steps.rb | 36 ++++ .../step_definitions/factory_girl_steps.rb | 36 ---- features/support/env.rb | 2 +- gemfiles/3.2.gemfile.lock | 4 +- gemfiles/4.0.gemfile.lock | 4 +- gemfiles/4.1.gemfile.lock | 4 +- gemfiles/4.2.gemfile.lock | 4 +- gemfiles/5.0.gemfile.lock | 4 +- lib/factory_bot.rb | 156 +++++++++++++++ lib/{factory_girl => factory_bot}/aliases.rb | 2 +- .../attribute.rb | 14 +- .../attribute/association.rb | 2 +- .../attribute/dynamic.rb | 4 +- .../attribute/sequence.rb | 4 +- .../attribute/static.rb | 2 +- .../attribute_assigner.rb | 2 +- .../attribute_list.rb | 2 +- lib/{factory_girl => factory_bot}/callback.rb | 6 +- .../callbacks_observer.rb | 2 +- .../configuration.rb | 4 +- .../declaration.rb | 10 +- .../declaration/association.rb | 2 +- .../declaration/dynamic.rb | 2 +- .../declaration/implicit.rb | 6 +- .../declaration/static.rb | 2 +- .../declaration_list.rb | 2 +- .../decorator.rb | 2 +- .../decorator/attribute_hash.rb | 2 +- .../decorator/class_key_hash.rb | 6 +- .../disallows_duplicates_registry.rb | 2 +- .../decorator/invocation_tracker.rb | 2 +- .../decorator/new_constructor.rb | 2 +- .../definition.rb | 6 +- .../definition_hierarchy.rb | 8 +- .../definition_proxy.rb | 10 +- lib/{factory_girl => factory_bot}/errors.rb | 2 +- .../evaluation.rb | 2 +- .../evaluator.rb | 4 +- .../evaluator_class_definer.rb | 2 +- lib/{factory_girl => factory_bot}/factory.rb | 8 +- .../factory_runner.rb | 6 +- .../find_definitions.rb | 4 +- lib/{factory_girl => factory_bot}/linter.rb | 6 +- .../null_factory.rb | 6 +- .../null_object.rb | 2 +- lib/{factory_girl => factory_bot}/registry.rb | 2 +- lib/{factory_girl => factory_bot}/reload.rb | 2 +- lib/{factory_girl => factory_bot}/sequence.rb | 4 +- .../strategy/attributes_for.rb | 2 +- .../strategy/build.rb | 2 +- .../strategy/create.rb | 2 +- .../strategy/null.rb | 2 +- .../strategy/stub.rb | 2 +- .../strategy_calculator.rb | 4 +- .../strategy_syntax_method_registrar.rb | 4 +- lib/factory_bot/syntax.rb | 7 + .../syntax/default.rb | 22 +-- .../syntax/methods.rb | 10 +- .../syntax_runner.rb | 2 +- lib/{factory_girl => factory_bot}/trait.rb | 4 +- lib/factory_bot/version.rb | 3 + lib/factory_girl.rb | 157 +-------------- lib/factory_girl/syntax.rb | 7 - lib/factory_girl/version.rb | 4 +- .../activesupport_instrumentation_spec.rb | 20 +- spec/acceptance/aliases_spec.rb | 6 +- spec/acceptance/attribute_aliases_spec.rb | 6 +- .../attribute_existing_on_object_spec.rb | 12 +- spec/acceptance/attributes_for_spec.rb | 12 +- .../attributes_from_instance_spec.rb | 16 +- spec/acceptance/attributes_ordered_spec.rb | 8 +- spec/acceptance/build_list_spec.rb | 8 +- spec/acceptance/build_spec.rb | 18 +- spec/acceptance/build_stubbed_spec.rb | 20 +- spec/acceptance/callbacks_spec.rb | 60 +++--- spec/acceptance/create_list_spec.rb | 18 +- spec/acceptance/create_pair_spec.rb | 4 +- spec/acceptance/create_spec.rb | 22 +-- .../define_child_before_parent_spec.rb | 4 +- .../defining_methods_inside_a_factory_spec.rb | 6 +- .../definition_camel_string_spec.rb | 4 +- spec/acceptance/definition_spec.rb | 12 +- .../definition_without_block_spec.rb | 4 +- .../acceptance/global_initialize_with_spec.rb | 26 +-- spec/acceptance/global_to_create_spec.rb | 44 ++--- spec/acceptance/initialize_with_spec.rb | 36 ++-- spec/acceptance/keyed_by_class_spec.rb | 8 +- spec/acceptance/lint_spec.rb | 50 ++--- spec/acceptance/modify_factories_spec.rb | 26 +-- spec/acceptance/modify_inherited_spec.rb | 18 +- spec/acceptance/nested_attributes_spec.rb | 8 +- spec/acceptance/overrides_spec.rb | 12 +- spec/acceptance/parent_spec.rb | 16 +- spec/acceptance/private_attributes_spec.rb | 4 +- spec/acceptance/register_strategies_spec.rb | 50 ++--- spec/acceptance/sequence_context_spec.rb | 16 +- spec/acceptance/sequence_spec.rb | 12 +- spec/acceptance/skip_create_spec.rb | 4 +- spec/acceptance/stub_spec.rb | 8 +- ..._methods_within_dynamic_attributes_spec.rb | 16 +- spec/acceptance/traits_spec.rb | 160 +++++++-------- spec/acceptance/transient_attributes_spec.rb | 30 +-- .../aliases_spec.rb | 14 +- .../attribute/association_spec.rb | 8 +- .../attribute/dynamic_spec.rb | 12 +- spec/factory_bot/attribute/sequence_spec.rb | 16 ++ .../attribute/static_spec.rb | 8 +- .../attribute_list_spec.rb | 54 ++--- spec/factory_bot/attribute_spec.rb | 16 ++ spec/factory_bot/callback_spec.rb | 41 ++++ .../declaration/implicit_spec.rb | 10 +- .../declaration_list_spec.rb | 8 +- .../definition_proxy_spec.rb | 78 ++++---- .../definition_spec.rb | 20 +- .../disallows_duplicates_registry_spec.rb | 4 +- .../evaluator_class_definer_spec.rb | 12 +- .../factory_spec.rb | 116 +++++------ .../find_definitions_spec.rb | 14 +- .../null_factory_spec.rb | 6 +- .../null_object_spec.rb | 4 +- .../registry_spec.rb | 4 +- .../sequence_spec.rb | 18 +- .../strategy/attributes_for_spec.rb | 2 +- .../strategy/build_spec.rb | 2 +- .../strategy/create_spec.rb | 2 +- .../strategy/stub_spec.rb | 2 +- .../strategy_calculator_spec.rb | 10 +- spec/factory_bot_spec.rb | 22 +++ spec/factory_girl/attribute/sequence_spec.rb | 16 -- spec/factory_girl/attribute_spec.rb | 16 -- spec/factory_girl/callback_spec.rb | 41 ---- spec/factory_girl_spec.rb | 22 --- spec/spec_helper.rb | 4 +- spec/support/matchers/callback.rb | 2 +- spec/support/matchers/declaration.rb | 10 +- spec/support/shared_examples/strategy.rb | 30 +-- 146 files changed, 1285 insertions(+), 1247 deletions(-) create mode 100644 factory_bot.gemspec create mode 100644 features/step_definitions/factory_bot_steps.rb delete mode 100644 features/step_definitions/factory_girl_steps.rb create mode 100644 lib/factory_bot.rb rename lib/{factory_girl => factory_bot}/aliases.rb (94%) rename lib/{factory_girl => factory_bot}/attribute.rb (73%) rename lib/{factory_girl => factory_bot}/attribute/association.rb (96%) rename lib/{factory_girl => factory_bot}/attribute/dynamic.rb (83%) rename lib/{factory_girl => factory_bot}/attribute/sequence.rb (79%) rename lib/{factory_girl => factory_bot}/attribute/static.rb (93%) rename lib/{factory_girl => factory_bot}/attribute_assigner.rb (99%) rename lib/{factory_girl => factory_bot}/attribute_list.rb (98%) rename lib/{factory_girl => factory_bot}/callback.rb (84%) rename lib/{factory_girl => factory_bot}/callbacks_observer.rb (95%) rename lib/{factory_girl => factory_bot}/configuration.rb (92%) rename lib/{factory_girl => factory_bot}/declaration.rb (56%) rename lib/{factory_girl => factory_bot}/declaration/association.rb (96%) rename lib/{factory_girl => factory_bot}/declaration/dynamic.rb (96%) rename lib/{factory_girl => factory_bot}/declaration/implicit.rb (83%) rename lib/{factory_girl => factory_bot}/declaration/static.rb (96%) rename lib/{factory_girl => factory_bot}/declaration_list.rb (98%) rename lib/{factory_girl => factory_bot}/decorator.rb (95%) rename lib/{factory_girl => factory_bot}/decorator/attribute_hash.rb (94%) rename lib/{factory_girl => factory_bot}/decorator/class_key_hash.rb (82%) rename lib/{factory_girl => factory_bot}/decorator/disallows_duplicates_registry.rb (94%) rename lib/{factory_girl => factory_bot}/decorator/invocation_tracker.rb (94%) rename lib/{factory_girl => factory_bot}/decorator/new_constructor.rb (92%) rename lib/{factory_girl => factory_bot}/definition.rb (96%) rename lib/{factory_girl => factory_bot}/definition_hierarchy.rb (89%) rename lib/{factory_girl => factory_bot}/definition_proxy.rb (94%) rename lib/{factory_girl => factory_bot}/errors.rb (98%) rename lib/{factory_girl => factory_bot}/evaluation.rb (96%) rename lib/{factory_girl => factory_bot}/evaluator.rb (95%) rename lib/{factory_girl => factory_bot}/evaluator_class_definer.rb (96%) rename lib/{factory_girl => factory_bot}/factory.rb (96%) rename lib/{factory_girl => factory_bot}/factory_runner.rb (79%) rename lib/{factory_girl => factory_bot}/find_definitions.rb (88%) rename lib/{factory_girl => factory_bot}/linter.rb (93%) rename lib/{factory_girl => factory_bot}/null_factory.rb (69%) rename lib/{factory_girl => factory_bot}/null_object.rb (96%) rename lib/{factory_girl => factory_bot}/registry.rb (96%) rename lib/{factory_girl => factory_bot}/reload.rb (87%) rename lib/{factory_girl => factory_bot}/sequence.rb (91%) rename lib/{factory_girl => factory_bot}/strategy/attributes_for.rb (90%) rename lib/{factory_girl => factory_bot}/strategy/build.rb (93%) rename lib/{factory_girl => factory_bot}/strategy/create.rb (95%) rename lib/{factory_girl => factory_bot}/strategy/null.rb (87%) rename lib/{factory_girl => factory_bot}/strategy/stub.rb (99%) rename lib/{factory_girl => factory_bot}/strategy_calculator.rb (84%) rename lib/{factory_girl => factory_bot}/strategy_syntax_method_registrar.rb (95%) create mode 100644 lib/factory_bot/syntax.rb rename lib/{factory_girl => factory_bot}/syntax/default.rb (69%) rename lib/{factory_girl => factory_bot}/syntax/methods.rb (93%) rename lib/{factory_girl => factory_bot}/syntax_runner.rb (80%) rename lib/{factory_girl => factory_bot}/trait.rb (87%) create mode 100644 lib/factory_bot/version.rb delete mode 100644 lib/factory_girl/syntax.rb rename spec/{factory_girl => factory_bot}/aliases_spec.rb (59%) rename spec/{factory_girl => factory_bot}/attribute/association_spec.rb (76%) rename spec/{factory_girl => factory_bot}/attribute/dynamic_spec.rb (75%) create mode 100644 spec/factory_bot/attribute/sequence_spec.rb rename spec/{factory_girl => factory_bot}/attribute/static_spec.rb (50%) rename spec/{factory_girl => factory_bot}/attribute_list_spec.rb (60%) create mode 100644 spec/factory_bot/attribute_spec.rb create mode 100644 spec/factory_bot/callback_spec.rb rename spec/{factory_girl => factory_bot}/declaration/implicit_spec.rb (52%) rename spec/{factory_girl => factory_bot}/declaration_list_spec.rb (89%) rename spec/{factory_girl => factory_bot}/definition_proxy_spec.rb (69%) rename spec/{factory_girl => factory_bot}/definition_spec.rb (70%) rename spec/{factory_girl => factory_bot}/disallows_duplicates_registry_spec.rb (77%) rename spec/{factory_girl => factory_bot}/evaluator_class_definer_spec.rb (86%) rename spec/{factory_girl => factory_bot}/factory_spec.rb (56%) rename spec/{factory_girl => factory_bot}/find_definitions_spec.rb (91%) rename spec/{factory_girl => factory_bot}/null_factory_spec.rb (64%) rename spec/{factory_girl => factory_bot}/null_object_spec.rb (85%) rename spec/{factory_girl => factory_bot}/registry_spec.rb (95%) rename spec/{factory_girl => factory_bot}/sequence_spec.rb (76%) rename spec/{factory_girl => factory_bot}/strategy/attributes_for_spec.rb (90%) rename spec/{factory_girl => factory_bot}/strategy/build_spec.rb (84%) rename spec/{factory_girl => factory_bot}/strategy/create_spec.rb (91%) rename spec/{factory_girl => factory_bot}/strategy/stub_spec.rb (98%) rename spec/{factory_girl => factory_bot}/strategy_calculator_spec.rb (55%) create mode 100644 spec/factory_bot_spec.rb delete mode 100644 spec/factory_girl/attribute/sequence_spec.rb delete mode 100644 spec/factory_girl/attribute_spec.rb delete mode 100644 spec/factory_girl/callback_spec.rb delete mode 100644 spec/factory_girl_spec.rb diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f085946cd..4eb5ab6a2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -# Contributing to Factory Girl +# Contributing to Factory Bot We love pull requests from everyone. By participating in this project, you agree to abide by the thoughtbot [code of conduct]. @@ -17,7 +17,7 @@ Here are some ways *you* can contribute: * by closing [issues][] * by reviewing patches -[issues]: https://github.com/thoughtbot/factory_girl/issues +[issues]: https://github.com/thoughtbot/factory_bot/issues ## Submitting an Issue @@ -52,7 +52,7 @@ already been submitted. asking for help. We love helping! * Please don't update the Gem version. -[repo]: https://github.com/thoughtbot/factory_girl/tree/master +[repo]: https://github.com/thoughtbot/factory_bot/tree/master [fork]: https://help.github.com/articles/fork-a-repo/ [branch]: https://help.github.com/articles/creating-and-deleting-branches-within-your-repository/ [pr]: https://help.github.com/articles/using-pull-requests/ diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 1ecae390a..aa218994d 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -4,19 +4,19 @@ Getting Started Update Your Gemfile ------------------- -If you're using Rails, you'll need to change the required version of `factory_girl_rails`: +If you're using Rails, you'll need to change the required version of `factory_bot_rails`: ```ruby -gem "factory_girl_rails", "~> 4.0" +gem "factory_bot_rails", "~> 4.0" ``` -If you're *not* using Rails, you'll just have to change the required version of `factory_girl`: +If you're *not* using Rails, you'll just have to change the required version of `factory_bot`: ```ruby -gem "factory_girl", "~> 4.0" +gem "factory_bot", "~> 4.0" ``` -JRuby users: factory_girl works with JRuby starting with 1.6.7.2 (latest stable, as per July 2012). +JRuby users: factory_bot works with JRuby starting with 1.6.7.2 (latest stable, as per July 2012). JRuby has to be used in 1.9 mode, for that, use JRUBY_OPTS environment variable: ```bash @@ -31,17 +31,17 @@ Configure your test suite # RSpec ```ruby -# spec/support/factory_girl.rb +# spec/support/factory_bot.rb RSpec.configure do |config| - config.include FactoryGirl::Syntax::Methods + config.include FactoryBot::Syntax::Methods end # RSpec without Rails RSpec.configure do |config| - config.include FactoryGirl::Syntax::Methods + config.include FactoryBot::Syntax::Methods config.before(:suite) do - FactoryGirl.find_definitions + FactoryBot.find_definitions end end ``` @@ -49,14 +49,14 @@ end Remember to require the above file in your rails_helper since the support folder isn't eagerly loaded ```ruby -require 'support/factory_girl' +require 'support/factory_bot' ``` # Test::Unit ```ruby class Test::Unit::TestCase - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods end ``` @@ -64,14 +64,14 @@ end ```ruby # env.rb (Rails example location - RAILS_ROOT/features/support/env.rb) -World(FactoryGirl::Syntax::Methods) +World(FactoryBot::Syntax::Methods) ``` # Spinach ```ruby class Spinach::FeatureSteps - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods end ``` @@ -79,7 +79,7 @@ end ```ruby class Minitest::Unit::TestCase - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods end ``` @@ -87,7 +87,7 @@ end ```ruby class Minitest::Spec - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods end ``` @@ -95,11 +95,11 @@ end ```ruby class ActiveSupport::TestCase - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods end ``` -If you do not include `FactoryGirl::Syntax::Methods` in your test suite, then all factory_girl methods will need to be prefaced with `FactoryGirl`. +If you do not include `FactoryBot::Syntax::Methods` in your test suite, then all factory_bot methods will need to be prefaced with `FactoryBot`. Defining factories ------------------ @@ -108,7 +108,7 @@ Each factory has a name and a set of attributes. The name is used to guess the c ```ruby # This will guess the User class -FactoryGirl.define do +FactoryBot.define do factory :user do first_name "John" last_name "Doe" @@ -129,7 +129,7 @@ It is highly recommended that you have one factory for each class that provides Attempting to define multiple factories with the same name will raise an error. Factories can be defined anywhere, but will be automatically loaded after -calling `FactoryGirl.find_definitions` if factories are defined in files at the +calling `FactoryBot.find_definitions` if factories are defined in files at the following locations: test/factories.rb @@ -140,7 +140,7 @@ following locations: Using factories --------------- -factory\_girl supports several different build strategies: build, create, attributes\_for and build\_stubbed: +factory\_bot supports several different build strategies: build, create, attributes\_for and build\_stubbed: ```ruby # Returns a User instance that's not saved @@ -198,7 +198,7 @@ end Aliases ------- -factory_girl allows you to define aliases to existing factories to make them easier to re-use. This could come in handy when, for example, your Post object has an author attribute that actually refers to an instance of a User class. While normally factory_girl can infer the factory name from the association name, in this case it will look for a author factory in vain. So, alias your user factory so it can be used under alias names. +factory_bot allows you to define aliases to existing factories to make them easier to re-use. This could come in handy when, for example, your Post object has an author attribute that actually refers to an instance of a User class. While normally factory_bot can infer the factory name from the association name, in this case it will look for a author factory in vain. So, alias your user factory so it can be used under alias names. ```ruby factory :user, aliases: [:author, :commenter] do @@ -268,8 +268,8 @@ Static and dynamic attributes can be created as transient attributes. Transient attributes will be ignored within attributes\_for and won't be set on the model, even if the attribute exists or you attempt to override it. -Within factory_girl's dynamic attributes, you can access transient attributes as -you would expect. If you need to access the evaluator in a factory_girl callback, +Within factory_bot's dynamic attributes, you can access transient attributes as +you would expect. If you need to access the evaluator in a factory_bot callback, you'll need to declare a second block argument (for the evaluator) and access transient attributes from there. @@ -388,7 +388,7 @@ depending on the amount of flexibility desired, but here's a surefire example of generating associated data. ```ruby -FactoryGirl.define do +FactoryBot.define do # post factory with a `belongs_to` association for the user factory :post do @@ -437,7 +437,7 @@ Here's an example with two models that are related via `has_and_belongs_to_many`: ```ruby -FactoryGirl.define do +FactoryBot.define do # language factory with a `belongs_to` association for the profile factory :language do @@ -489,7 +489,7 @@ definition block, and values in a sequence are generated by calling ```ruby # Defines a new sequence -FactoryGirl.define do +FactoryBot.define do sequence :email do |n| "person#{n}@example.com" end @@ -670,7 +670,7 @@ factory :user do end ``` -Traits can also be passed in as a list of symbols when you construct an instance from factory_girl. +Traits can also be passed in as a list of symbols when you construct an instance from factory_bot. ```ruby factory :user do @@ -785,12 +785,12 @@ create :invoice, :with_amount, amount: 2 Callbacks --------- -factory\_girl makes available four callbacks for injecting some code: +factory\_bot makes available four callbacks for injecting some code: -* after(:build) - called after a factory is built (via `FactoryGirl.build`, `FactoryGirl.create`) -* before(:create) - called before a factory is saved (via `FactoryGirl.create`) -* after(:create) - called after a factory is saved (via `FactoryGirl.create`) -* after(:stub) - called after a factory is stubbed (via `FactoryGirl.build_stubbed`) +* after(:build) - called after a factory is built (via `FactoryBot.build`, `FactoryBot.create`) +* before(:create) - called before a factory is saved (via `FactoryBot.create`) +* after(:create) - called after a factory is saved (via `FactoryBot.create`) +* after(:stub) - called after a factory is stubbed (via `FactoryBot.build_stubbed`) Examples: @@ -836,10 +836,10 @@ end ``` To override callbacks for all factories, define them within the -`FactoryGirl.define` block: +`FactoryBot.define` block: ```ruby -FactoryGirl.define do +FactoryBot.define do after(:build) { |object| puts "Built #{object}" } after(:create) { |object| AuditLog.create(attrs: object.attributes) } @@ -860,7 +860,7 @@ class User < ActiveRecord::Base end # spec/factories.rb -FactoryGirl.define do +FactoryBot.define do factory :user do after :create, &:confirm! end @@ -878,7 +878,7 @@ modify that factory instead of creating a child factory and adding attributes th If a gem were to give you a User factory: ```ruby -FactoryGirl.define do +FactoryBot.define do factory :user do full_name "John Doe" sequence(:username) { |n| "user#{n}" } @@ -890,7 +890,7 @@ end Instead of creating a child factory that added additional attributes: ```ruby -FactoryGirl.define do +FactoryBot.define do factory :application_user, parent: :user do full_name "Jane Doe" date_of_birth { 21.years.ago } @@ -903,7 +903,7 @@ end You could modify that factory instead. ```ruby -FactoryGirl.modify do +FactoryBot.modify do factory :user do full_name "Jane Doe" date_of_birth { 21.years.ago } @@ -915,7 +915,7 @@ end When modifying a factory, you can change any of the attributes you want (aside from callbacks). -`FactoryGirl.modify` must be called outside of a `FactoryGirl.define` block as it operates on factories differently. +`FactoryBot.modify` must be called outside of a `FactoryBot.define` block as it operates on factories differently. A caveat: you can only modify factories (not sequences or traits) and callbacks *still compound as they normally would*. So, if the factory you're modifying defines an `after(:create)` callback, you defining an `after(:create)` won't override it, it'll just get run after the first callback. @@ -959,18 +959,18 @@ users_attrs = attributes_for_list(:user, 25) # array of attribute hashes Linting Factories ----------------- -factory_girl allows for linting known factories: +factory_bot allows for linting known factories: ```ruby -FactoryGirl.lint +FactoryBot.lint ``` -`FactoryGirl.lint` creates each factory and catches any exceptions raised -during the creation process. `FactoryGirl::InvalidFactoryError` is raised with +`FactoryBot.lint` creates each factory and catches any exceptions raised +during the creation process. `FactoryBot::InvalidFactoryError` is raised with a list of factories (and corresponding exceptions) for factories which could not be created. -Recommended usage of `FactoryGirl.lint` +Recommended usage of `FactoryBot.lint` is to run this in a task before your test suite is executed. Running it in a `before(:suite)`, @@ -981,23 +981,23 @@ when running single tests. Example Rake task: ```ruby -# lib/tasks/factory_girl.rake -namespace :factory_girl do - desc "Verify that all FactoryGirl factories are valid" +# lib/tasks/factory_bot.rake +namespace :factory_bot do + desc "Verify that all FactoryBot factories are valid" task lint: :environment do if Rails.env.test? DatabaseCleaner.cleaning do - FactoryGirl.lint + FactoryBot.lint end else - system("bundle exec rake factory_girl:lint RAILS_ENV='test'") + system("bundle exec rake factory_bot:lint RAILS_ENV='test'") exit $?.exitstatus end end end ``` -After calling `FactoryGirl.lint`, you'll likely want to clear out the +After calling `FactoryBot.lint`, you'll likely want to clear out the database, as records will most likely be created. 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. @@ -1005,11 +1005,11 @@ 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| +factories_to_lint = FactoryBot.factories.reject do |factory| factory.name =~ /^old_/ end -FactoryGirl.lint factories_to_lint +FactoryBot.lint factories_to_lint ``` This would lint all factories that aren't prefixed with `old_`. @@ -1019,13 +1019,13 @@ and every trait of a factory generates a valid object on its own. This is turned on by passing `traits: true` to the `lint` method: ```ruby -FactoryGirl.lint traits: true +FactoryBot.lint traits: true ``` This can also be combined with other arguments: ```ruby -FactoryGirl.lint factories_to_lint, traits: true +FactoryBot.lint factories_to_lint, traits: true ``` You can also specify the strategy used for linting: @@ -1037,7 +1037,7 @@ FactoryGirl.lint strategy: :build Custom Construction ------------------- -If you want to use factory_girl to construct an object where some attributes +If you want to use factory_bot to construct an object where some attributes are passed to `initialize` or if you want to do something other than simply calling `new` on your build class, you can override the default behavior by defining `initialize_with` on your factory. Example: @@ -1065,7 +1065,7 @@ end build(:user).name # Jane Doe ``` -Although factory_girl is written to work with ActiveRecord out of the box, it +Although factory_bot is written to work with ActiveRecord out of the box, it can also work with any Ruby class. For maximum compatibility with ActiveRecord, the default initializer builds all instances by calling `new` on your build class without any arguments. It then calls attribute writer methods to assign all the @@ -1112,10 +1112,10 @@ include transient attributes, but everything else defined in the factory will be passed (associations, evalued sequences, etc.) You can define `initialize_with` for all factories by including it in the -`FactoryGirl.define` block: +`FactoryBot.define` block: ```ruby -FactoryGirl.define do +FactoryBot.define do initialize_with { new("Awesome first argument") } end ``` @@ -1125,7 +1125,7 @@ block are assigned *only* in the constructor; this equates to roughly the following code: ```ruby -FactoryGirl.define do +FactoryBot.define do factory :user do initialize_with { new(name) } @@ -1138,11 +1138,11 @@ build(:user) User.new('value') ``` -This prevents duplicate assignment; in versions of factory_girl before 4.0, it +This prevents duplicate assignment; in versions of factory_bot before 4.0, it would run this: ```ruby -FactoryGirl.define do +FactoryBot.define do factory :user do initialize_with { new(name) } @@ -1159,27 +1159,27 @@ user.name = 'value' Custom Strategies ----------------- -There are times where you may want to extend behavior of factory\_girl by +There are times where you may want to extend behavior of factory\_bot by adding a custom build strategy. Strategies define two methods: `association` and `result`. `association` -receives a `FactoryGirl::FactoryRunner` instance, upon which you can call +receives a `FactoryBot::FactoryRunner` instance, upon which you can call `run`, overriding the strategy if you want. The second method, `result`, -receives a `FactoryGirl::Evaluation` instance. It provides a way to trigger +receives a `FactoryBot::Evaluation` instance. It provides a way to trigger callbacks (with `notify`), `object` or `hash` (to get the result instance or a hash based on the attributes defined in the factory), and `create`, which executes the `to_create` callback defined on the factory. -To understand how factory\_girl uses strategies internally, it's probably +To understand how factory\_bot uses strategies internally, it's probably easiest to just view the source for each of the four default strategies. Here's an example of composing a strategy using -`FactoryGirl::Strategy::Create` to build a JSON representation of your model. +`FactoryBot::Strategy::Create` to build a JSON representation of your model. ```ruby class JsonStrategy def initialize - @strategy = FactoryGirl.strategy_by_name(:create).new + @strategy = FactoryBot.strategy_by_name(:create).new end delegate :association, to: :@strategy @@ -1190,19 +1190,19 @@ class JsonStrategy end ``` -For factory\_girl to recognize the new strategy, you can register it: +For factory\_bot to recognize the new strategy, you can register it: ```ruby -FactoryGirl.register_strategy(:json, JsonStrategy) +FactoryBot.register_strategy(:json, JsonStrategy) ``` This allows you to call ```ruby -FactoryGirl.json(:user) +FactoryBot.json(:user) ``` -Finally, you can override factory\_girl's own strategies if you'd like by +Finally, you can override factory\_bot's own strategies if you'd like by registering a new object in place of the strategies. Custom Callbacks @@ -1213,7 +1213,7 @@ Custom callbacks can be defined if you're using custom strategies: ```ruby class JsonStrategy def initialize - @strategy = FactoryGirl.strategy_by_name(:create).new + @strategy = FactoryBot.strategy_by_name(:create).new end delegate :association, to: :@strategy @@ -1229,9 +1229,9 @@ class JsonStrategy end end -FactoryGirl.register_strategy(:json, JsonStrategy) +FactoryBot.register_strategy(:json, JsonStrategy) -FactoryGirl.define do +FactoryBot.define do factory :user do before(:json) { |user| do_something_to(user) } after(:json) { |user_json| do_something_to(user_json) } @@ -1263,10 +1263,10 @@ end ``` To override `to_create` for all factories, define it within the -`FactoryGirl.define` block: +`FactoryBot.define` block: ```ruby -FactoryGirl.define do +FactoryBot.define do to_create { |instance| instance.persist! } @@ -1285,7 +1285,7 @@ factories being run. One example would be to track factories based on a threshold of execution time. ```ruby -ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload| +ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start, finish, id, payload| execution_time_in_seconds = finish - start if execution_time_in_seconds >= 0.5 @@ -1299,19 +1299,19 @@ throughout your test suite. If you're using RSpec, it's as simple as adding a `before(:suite)` and `after(:suite)`: ```ruby -factory_girl_results = {} +factory_bot_results = {} config.before(:suite) do - ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload| + ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start, finish, id, payload| factory_name = payload[:name] strategy_name = payload[:strategy] - factory_girl_results[factory_name] ||= {} - factory_girl_results[factory_name][strategy_name] ||= 0 - factory_girl_results[factory_name][strategy_name] += 1 + factory_bot_results[factory_name] ||= {} + factory_bot_results[factory_name][strategy_name] ||= 0 + factory_bot_results[factory_name][strategy_name] += 1 end end config.after(:suite) do - puts factory_girl_results + puts factory_bot_results end ``` @@ -1323,7 +1323,7 @@ to encounter an `ActiveRecord::AssociationTypeMismatch` error when creating a fa with associations, as below: ```ruby -FactoryGirl.define do +FactoryBot.define do factory :united_states, class: Location do name 'United States' association :location_group, factory: :north_america @@ -1344,11 +1344,11 @@ ActiveRecord::AssociationTypeMismatch: ``` The two possible solutions are to either run the suite without the preloader, or -to add `FactoryGirl.reload` to the RSpec configuration, like so: +to add `FactoryBot.reload` to the RSpec configuration, like so: ```ruby RSpec.configure do |config| - config.before(:suite) { FactoryGirl.reload } + config.before(:suite) { FactoryBot.reload } end ``` @@ -1358,31 +1358,31 @@ Using Without Bundler If you're not using Bundler, be sure to have the gem installed and call: ```ruby -require 'factory_girl' +require 'factory_bot' ``` Once required, assuming you have a directory structure of `spec/factories` or `test/factories`, all you'll need to do is run ```ruby -FactoryGirl.find_definitions +FactoryBot.find_definitions ``` If you're using a separate directory structure for your factories, you can change the definition file paths before trying to find definitions: ```ruby -FactoryGirl.definition_file_paths = %w(custom_factories_directory) -FactoryGirl.find_definitions +FactoryBot.definition_file_paths = %w(custom_factories_directory) +FactoryBot.find_definitions ``` If you don't have a separate directory of factories and would like to define them inline, that's possible as well: ```ruby -require 'factory_girl' +require 'factory_bot' -FactoryGirl.define do +FactoryBot.define do factory :user do name 'John Doe' date_of_birth { 21.years.ago } diff --git a/Gemfile b/Gemfile index ef05c5703..79b1e8bb1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' -gemspec +gemspec name: 'factory_bot' gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby gem 'jdbc-sqlite3', platforms: :jruby diff --git a/Gemfile.lock b/Gemfile.lock index 45cc8faa8..b9715f95b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,24 +1,19 @@ PATH remote: . specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM remote: https://rubygems.org/ specs: - activemodel (5.0.0.1) - activesupport (= 5.0.0.1) - activerecord (5.0.0.1) - activemodel (= 5.0.0.1) - activesupport (= 5.0.0.1) - arel (~> 7.0) - activerecord-jdbc-adapter (1.3.21) - activerecord (>= 2.2) - activerecord-jdbcsqlite3-adapter (1.3.21) - activerecord-jdbc-adapter (~> 1.3.21) - jdbc-sqlite3 (>= 3.7.2, < 3.9) - activesupport (5.0.0.1) + activemodel (5.1.4) + activesupport (= 5.1.4) + activerecord (5.1.4) + activemodel (= 5.1.4) + activesupport (= 5.1.4) + arel (~> 8.0) + activesupport (5.1.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) @@ -27,74 +22,66 @@ GEM bundler rake thor (>= 0.14.0) - arel (7.1.4) - aruba (0.13.0) + arel (8.0.0) + aruba (0.14.2) childprocess (~> 0.5.6) contracts (~> 0.9) cucumber (>= 1.3.19) ffi (~> 1.9.10) rspec-expectations (>= 2.99) thor (~> 0.19) - builder (3.2.2) + builder (3.2.3) childprocess (0.5.9) ffi (~> 1.0, >= 1.0.11) - concurrent-ruby (1.0.2) - concurrent-ruby (1.0.2-java) - contracts (0.13.0) + concurrent-ruby (1.0.5) + contracts (0.16.0) cucumber (1.3.20) builder (>= 2.1.2) diff-lcs (>= 1.1.3) gherkin (~> 2.12) multi_json (>= 1.7.5, < 2.0) multi_test (>= 0.1.2) - diff-lcs (1.2.5) + diff-lcs (1.3) docile (1.1.5) - ffi (1.9.10) - ffi (1.9.10-java) + ffi (1.9.18) gherkin (2.12.2) multi_json (~> 1.3) - gherkin (2.12.2-java) - multi_json (~> 1.3) - i18n (0.7.0) - jdbc-sqlite3 (3.8.11.2) - json (1.8.6) - json (1.8.6-java) - minitest (5.9.1) - multi_json (1.11.2) + i18n (0.8.6) + json (2.1.0) + minitest (5.10.3) + multi_json (1.12.2) multi_test (0.1.2) - rake (10.5.0) - rspec (3.4.0) - rspec-core (~> 3.4.0) - rspec-expectations (~> 3.4.0) - rspec-mocks (~> 3.4.0) - rspec-core (3.4.2) - rspec-support (~> 3.4.0) - rspec-expectations (3.4.0) + rake (12.1.0) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) + rspec-support (~> 3.6.0) rspec-its (1.2.0) rspec-core (>= 3.0.0) rspec-expectations (>= 3.0.0) - rspec-mocks (3.4.1) + rspec-mocks (3.6.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) - rspec-support (3.4.1) - simplecov (0.11.2) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) + simplecov (0.15.1) docile (~> 1.1.0) - json (~> 1.8) + json (>= 1.8, < 3) simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) - sqlite3 (1.3.12) - thor (0.19.1) - thread_safe (0.3.5) - thread_safe (0.3.5-java) - timecop (0.8.0) - tzinfo (1.2.2) + simplecov-html (0.10.2) + sqlite3 (1.3.13) + thor (0.20.0) + thread_safe (0.3.6) + timecop (0.9.1) + tzinfo (1.2.3) thread_safe (~> 0.1) - yard (0.8.7.6) + yard (0.9.9) PLATFORMS - java ruby DEPENDENCIES @@ -103,7 +90,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) @@ -113,4 +100,4 @@ DEPENDENCIES yard BUNDLED WITH - 1.15.4 + 1.15.3 diff --git a/NEWS b/NEWS index 1a700b1e1..d62189f1f 100644 --- a/NEWS +++ b/NEWS @@ -20,14 +20,14 @@ 4.6.0 (skipped) 4.5.0 (October 17, 2014) - Improve FactoryGirl.lint by including exception and message in output + Improve FactoryBot.lint by including exception and message in output Allow selective linting Use more explicit #public_send when doing attribute assignment - Improve documentation around FactoryGirl.lint and initialize_with + Improve documentation around FactoryBot.lint and initialize_with Deprecate #ignore in favor of #transient 4.4.0 (February 10, 2014) - Add FactoryGirl.lint + Add FactoryBot.lint Fix memory leak in duplicate traits Update documentation @@ -35,7 +35,7 @@ Start testing against Rails 4.0 and Ruby 2.0.0 Stop testing against Rails 3.0 and Ruby 1.9.2 Add *_pair methods to only build two objects - Raise if a method is defined with a FactoryGirl block (factory or trait) + Raise if a method is defined with a FactoryBot block (factory or trait) Allow use of Symbol#to_proc in callbacks Add global callbacks Improve GETTING_STARTED and README @@ -74,11 +74,11 @@ custom constructor, and custom to_create Add memoization to speed up factories providing attribute overrides Add initial support of JRuby when running in 1.9 mode - Improve docs on what happens when including FactoryGirl::Syntax::Methods + Improve docs on what happens when including FactoryBot::Syntax::Methods 3.5.0 (June 22, 2012) Allow created_at to be set when using build_stubbed - Deprecate FactoryGirl step definitions + Deprecate FactoryBot step definitions 3.4.2 (June 19, 2012) Fix bug in traits with callbacks called implicitly in factories whose @@ -111,7 +111,7 @@ Skip to_create with skip_create Allow registration of custom strategies Deprecate alternate syntaxes - Implicitly call factory_girl's syntax methods from dynamic attributes + Implicitly call factory_bot's syntax methods from dynamic attributes 3.1.0 (April 6, 2012) Sequences support aliases, which reference the same block @@ -126,7 +126,7 @@ Remove Ruby 1.8 support Remove deprecated features, including default_strategy, factory_name, :method for defining default strategy, ignore on individual attributes, and - interacting with Factory the way you would FactoryGirl + interacting with Factory the way you would FactoryBot 2.6.4 (March 16, 2012) Do not ignore names of transient attributes @@ -144,9 +144,9 @@ Ruby 1.9.2-p3p18 2.6.1 (March 2, 2012) - Use FactoryGirl.reload in specs + Use FactoryBot.reload in specs Clean up running named factories with a particular strategy with - FactoryGirl::FactoryRunner + FactoryBot::FactoryRunner 2.6.0 (February 17, 2012) Improve documentation of has_many associations in the GETTING_STARTED @@ -182,14 +182,14 @@ Fix inline traits 2.4.0 (January 13, 2012) - Refactor internals of FactoryGirl to use anonymous class on which attributes + Refactor internals of FactoryBot to use anonymous class on which attributes get defined Explicitly require Ruby 1.8.7 or higher in gemspec Fix documentation Add Gemnasium status to documentation Supplying a Class to a factory that overrides to_s no longer results in getting the wrong Class constructed - Be more agnostic about ORMs when using columns in FactoryGirl step + Be more agnostic about ORMs when using columns in FactoryBot step definitions Test against Active Record 3.2.0.rc2 Update GETTING_STARTED to use Ruby syntax highlighting @@ -204,9 +204,9 @@ class so attributes work correctly all the time. 2.3.1 (November 23, 2011) - Remove internally-used associate method from all the FactoryGirl::Proxy subclasses + Remove internally-used associate method from all the FactoryBot::Proxy subclasses Move around requiring of files - Consolidate errors into factory_girl.rb + Consolidate errors into factory_bot.rb Refactor AttributeList to deal with priority only when iterating over attributes Refactor internals of some of the Proxy subclasses @@ -216,8 +216,8 @@ Registries are named, resulting in better messages when factories, traits, or sequences cannot be found Fix incorrect tests - Internals refactoring introducing FactoryGirl::NullFactory, - FactoryGirl::Definition, and FactoryGirl::DeclarationList + Internals refactoring introducing FactoryBot::NullFactory, + FactoryBot::Definition, and FactoryBot::DeclarationList Use ActiveSupport for Hash#except and its delegation capabilities Fix usage of callbacks when added via implicit traits Use Bundler tasks and clean up dependencies diff --git a/README.md b/README.md index 123cabd7f..5c4f82d1c 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,18 @@ -# factory_girl [![Build Status](https://travis-ci.org/thoughtbot/factory_girl.svg)](http://travis-ci.org/thoughtbot/factory_girl?branch=master) [![Dependency Status](https://gemnasium.com/thoughtbot/factory_girl.svg)](https://gemnasium.com/thoughtbot/factory_girl) [![Code Climate](https://codeclimate.com/github/thoughtbot/factory_girl/badges/gpa.svg)](https://codeclimate.com/github/thoughtbot/factory_girl) +# factory_bot [![Build Status](https://travis-ci.org/thoughtbot/factory_bot.svg)](http://travis-ci.org/thoughtbot/factory_bot?branch=master) [![Dependency Status](https://gemnasium.com/thoughtbot/factory_bot.svg)](https://gemnasium.com/thoughtbot/factory_bot) [![Code Climate](https://codeclimate.com/github/thoughtbot/factory_bot/badges/gpa.svg)](https://codeclimate.com/github/thoughtbot/factory_bot) -factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance. +factory_bot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance. -If you want to use factory_girl with Rails, see -[factory_girl_rails](https://github.com/thoughtbot/factory_girl_rails). +If you want to use factory_bot with Rails, see +[factory_bot_rails](https://github.com/thoughtbot/factory_bot_rails). + +**A historical note:** factory_bot used to be named factory_girl. An explanation of the name change can be found in the [old factory_girl repository](https://github.com/thoughtbot/factory_girl). _[Interested in the project name?](NAME.md)._ Documentation ------------- -You should find the documentation for your version of factory_girl on [Rubygems](https://rubygems.org/gems/factory_girl). +You should find the documentation for your version of factory_bot on [Rubygems](https://rubygems.org/gems/factory_bot). See [GETTING_STARTED] for information on defining and using factories. We also have [a detailed introductory video][], available for free on Upcase. @@ -23,7 +25,7 @@ Install Add the following line to Gemfile: ```ruby -gem 'factory_girl' +gem 'factory_bot' ``` and run `bundle install` from your shell. @@ -31,7 +33,7 @@ and run `bundle install` from your shell. To install the gem manually from your shell, run: ```shell -gem install factory_girl +gem install factory_bot ``` **Caveat:** As of ActiveSupport 5.0 and above, Ruby 2.2.2+ is required. Because @@ -39,8 +41,8 @@ of Rubygems' dependency resolution when installing gems, you may see an error similar to: ``` -$ gem install factory_girl -ERROR: Error installing factory_girl: +$ gem install factory_bot +ERROR: Error installing factory_bot: activesupport requires Ruby version >= 2.2.2. ``` @@ -50,35 +52,37 @@ manually. Supported Ruby versions ----------------------- -The factory_girl 3.x+ series supports MRI Ruby 1.9. Additionally, factory_girl +The factory_bot 3.x+ series supports MRI Ruby 1.9. Additionally, factory_bot 3.6+ supports JRuby 1.6.7.2+ while running in 1.9 mode. See [GETTING_STARTED] for more information on configuring the JRuby environment. -For versions of Ruby prior to 1.9, please use factory_girl 2.x. +For versions of Ruby prior to 1.9, please use factory_bot 2.x. More Information ---------------- -* [Rubygems](https://rubygems.org/gems/factory_girl) -* [Stack Overflow](http://stackoverflow.com/questions/tagged/factory-girl) -* [Issues](https://github.com/thoughtbot/factory_girl/issues) +* [Rubygems](https://rubygems.org/gems/factory_bot) +* [Stack Overflow](http://stackoverflow.com/questions/tagged/factory-bot) +* [Issues](https://github.com/thoughtbot/factory_bot/issues) * [GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS](http://robots.thoughtbot.com/) -[GETTING_STARTED]: http://rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md +You may also find useful information under the [factory_girl tag on Stack Overflow](http://stackoverflow.com/questions/tagged/factory-girl). + +[GETTING_STARTED]: http://rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md Contributing ------------ -Please see [CONTRIBUTING.md](https://github.com/thoughtbot/factory_girl/blob/master/CONTRIBUTING.md). +Please see [CONTRIBUTING.md](https://github.com/thoughtbot/factory_bot/blob/master/CONTRIBUTING.md). -factory_girl was originally written by Joe Ferris and is now maintained by Josh +factory_bot was originally written by Joe Ferris and is now maintained by Josh Clayton. Many improvements and bugfixes were contributed by the [open source -community](https://github.com/thoughtbot/factory_girl/graphs/contributors). +community](https://github.com/thoughtbot/factory_bot/graphs/contributors). License ------- -factory_girl is Copyright © 2008-2017 Joe Ferris and thoughtbot. It is free +factory_bot is Copyright © 2008-2016 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the [LICENSE](/LICENSE) file. @@ -87,7 +91,7 @@ About thoughtbot ![thoughtbot](http://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg) -factory_girl is maintained and funded by thoughtbot, inc. +factory_bot is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc. We love open source software! diff --git a/Rakefile b/Rakefile index 2dd97483c..a55a5f039 100644 --- a/Rakefile +++ b/Rakefile @@ -6,7 +6,7 @@ require 'yard' require 'rspec/core/rake_task' require 'cucumber/rake/task' -Bundler::GemHelper.install_tasks +Bundler::GemHelper.install_tasks(name: 'factory_bot') desc 'Default: run the specs and features.' task default: %w(spec:unit spec:acceptance features) @@ -14,7 +14,7 @@ task default: %w(spec:unit spec:acceptance features) namespace :spec do desc "Run unit specs" RSpec::Core::RakeTask.new('unit') do |t| - t.pattern = 'spec/{*_spec.rb,factory_girl/**/*_spec.rb}' + t.pattern = 'spec/{*_spec.rb,factory_bot/**/*_spec.rb}' end desc "Run acceptance specs" diff --git a/factory_bot.gemspec b/factory_bot.gemspec new file mode 100644 index 000000000..a1d5ce14b --- /dev/null +++ b/factory_bot.gemspec @@ -0,0 +1,36 @@ +$LOAD_PATH << File.expand_path("../lib", __FILE__) +require 'factory_bot/version' + +Gem::Specification.new do |s| + s.name = %q{factory_bot} + s.version = FactoryBot::VERSION + s.summary = %q{factory_bot provides a framework and DSL for defining and + using model instance factories.} + s.description = %q{factory_bot provides a framework and DSL for defining and + using factories - less error-prone, more explicit, and + all-around easier to work with than fixtures.} + + s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(spec|features)/}) } + + s.require_path = 'lib' + s.required_ruby_version = Gem::Requirement.new(">= 1.9.2") + + s.authors = ["Josh Clayton", "Joe Ferris"] + s.email = ["jclayton@thoughtbot.com", "jferris@thoughtbot.com"] + + s.homepage = "https://github.com/thoughtbot/factory_bot" + + s.add_dependency("activesupport", ">= 3.0.0") + + s.add_development_dependency("rspec", "~> 3.0") + s.add_development_dependency("rspec-its", "~> 1.0") + s.add_development_dependency("cucumber", "~> 1.3.15") + s.add_development_dependency("timecop") + s.add_development_dependency("simplecov") + s.add_development_dependency("aruba") + s.add_development_dependency("appraisal", "~> 2.1.0") + s.add_development_dependency("activerecord", ">= 3.0.0") + s.add_development_dependency("yard") + + s.license = "MIT" +end diff --git a/factory_girl.gemspec b/factory_girl.gemspec index 24f1cc4da..452a48401 100644 --- a/factory_girl.gemspec +++ b/factory_girl.gemspec @@ -3,12 +3,15 @@ require 'factory_girl/version' Gem::Specification.new do |s| s.name = %q{factory_girl} - s.version = FactoryGirl::VERSION + s.version = VERSION s.summary = %q{factory_girl provides a framework and DSL for defining and using model instance factories.} s.description = %q{factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures.} + s.post_install_message = "The factory_girl gem has been deprecated and has "\ + "been replaced by factory_bot. Please switch to "\ + "factory_bot as soon as possible." s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(spec|features)/}) } @@ -28,6 +31,8 @@ Gem::Specification.new do |s| s.add_development_dependency("timecop") s.add_development_dependency("simplecov") s.add_development_dependency("aruba") + s.add_development_dependency("mocha", ">= 0.12.8") + s.add_development_dependency("bourne") s.add_development_dependency("appraisal", "~> 2.1.0") s.add_development_dependency("activerecord", ">= 3.0.0") s.add_development_dependency("yard") diff --git a/features/find_definitions.feature b/features/find_definitions.feature index d1c4c7998..20be57903 100644 --- a/features/find_definitions.feature +++ b/features/find_definitions.feature @@ -1,15 +1,15 @@ -Feature: FactoryGirl can find factory definitions correctly +Feature: FactoryBot can find factory definitions correctly Scenario: Find definitions with a path Given a file named "awesome_factories.rb" with: """ - FactoryGirl.define do + FactoryBot.define do factory :awesome_category, :class => Category do name "awesome!!!" end end """ - When "awesome_factories.rb" is added to FactoryGirl's file definitions path - And I create a "awesome_category" instance from FactoryGirl + When "awesome_factories.rb" is added to FactoryBot's file definitions path + And I create a "awesome_category" instance from FactoryBot Then I should find the following for the last category: | name | | awesome!!! | @@ -17,14 +17,14 @@ Feature: FactoryGirl can find factory definitions correctly Scenario: Find definitions with an absolute path Given a file named "awesome_factories.rb" with: """ - FactoryGirl.define do + FactoryBot.define do factory :another_awesome_category, :class => Category do name "awesome!!!" end end """ - When "awesome_factories.rb" is added to FactoryGirl's file definitions path as an absolute path - And I create a "another_awesome_category" instance from FactoryGirl + When "awesome_factories.rb" is added to FactoryBot's file definitions path as an absolute path + And I create a "another_awesome_category" instance from FactoryBot Then I should find the following for the last category: | name | | awesome!!! | @@ -32,22 +32,22 @@ Feature: FactoryGirl can find factory definitions correctly Scenario: Find definitions with a folder Given a file named "nested/great_factories.rb" with: """ - FactoryGirl.define do + FactoryBot.define do factory :great_category, :class => Category do name "great!!!" end end """ - When "nested" is added to FactoryGirl's file definitions path - And I create a "great_category" instance from FactoryGirl + When "nested" is added to FactoryBot's file definitions path + And I create a "great_category" instance from FactoryBot Then I should find the following for the last category: | name | | great!!! | - Scenario: Reload FactoryGirl + Scenario: Reload FactoryBot Given a file named "nested/reload_factories.rb" with: """ - FactoryGirl.define do + FactoryBot.define do sequence(:great) trait :admin do admin true @@ -58,18 +58,18 @@ Feature: FactoryGirl can find factory definitions correctly end end """ - When "nested" is added to FactoryGirl's file definitions path + When "nested" is added to FactoryBot's file definitions path And I append to "nested/reload_factories.rb" with: """ - FactoryGirl.modify do + FactoryBot.modify do factory :handy_category do name "HANDY!!!" end end """ And I reload factories - And I create a "handy_category" instance from FactoryGirl + And I create a "handy_category" instance from FactoryBot Then I should find the following for the last category: | name | | HANDY!!! | diff --git a/features/step_definitions/factory_bot_steps.rb b/features/step_definitions/factory_bot_steps.rb new file mode 100644 index 000000000..9510d9684 --- /dev/null +++ b/features/step_definitions/factory_bot_steps.rb @@ -0,0 +1,36 @@ +module FactoryBotDefinitionsHelper + def append_file_to_factory_bot_definitions_path(path_to_file) + FactoryBot.definition_file_paths ||= [] + FactoryBot.definition_file_paths << path_to_file + end +end + +World(FactoryBotDefinitionsHelper) + +When /^"([^"]*)" is added to FactoryBot's file definitions path$/ do |file_name| + new_factory_file = File.join(expand_path("."), file_name.gsub(".rb", "")) + + append_file_to_factory_bot_definitions_path(new_factory_file) + + step %{I find definitions} +end + +When /^"([^"]*)" is added to FactoryBot's file definitions path as an absolute path$/ do |file_name| + new_factory_file = File.expand_path(File.join(expand_path("."), file_name.gsub(".rb", ""))) + + append_file_to_factory_bot_definitions_path(new_factory_file) + + step %{I find definitions} +end + +When /^I create a "([^"]*)" instance from FactoryBot$/ do |factory_name| + FactoryBot.create(factory_name) +end + +When /^I find definitions$/ do + FactoryBot.find_definitions +end + +When /^I reload factories$/ do + FactoryBot.reload +end diff --git a/features/step_definitions/factory_girl_steps.rb b/features/step_definitions/factory_girl_steps.rb deleted file mode 100644 index 9a16caaea..000000000 --- a/features/step_definitions/factory_girl_steps.rb +++ /dev/null @@ -1,36 +0,0 @@ -module FactoryGirlDefinitionsHelper - def append_file_to_factory_girl_definitions_path(path_to_file) - FactoryGirl.definition_file_paths ||= [] - FactoryGirl.definition_file_paths << path_to_file - end -end - -World(FactoryGirlDefinitionsHelper) - -When /^"([^"]*)" is added to FactoryGirl's file definitions path$/ do |file_name| - new_factory_file = File.join(expand_path("."), file_name.gsub(".rb", "")) - - append_file_to_factory_girl_definitions_path(new_factory_file) - - step %{I find definitions} -end - -When /^"([^"]*)" is added to FactoryGirl's file definitions path as an absolute path$/ do |file_name| - new_factory_file = File.expand_path(File.join(expand_path("."), file_name.gsub(".rb", ""))) - - append_file_to_factory_girl_definitions_path(new_factory_file) - - step %{I find definitions} -end - -When /^I create a "([^"]*)" instance from FactoryGirl$/ do |factory_name| - FactoryGirl.create(factory_name) -end - -When /^I find definitions$/ do - FactoryGirl.find_definitions -end - -When /^I reload factories$/ do - FactoryGirl.reload -end diff --git a/features/support/env.rb b/features/support/env.rb index 520925e4a..48f083318 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -5,6 +5,6 @@ $: << File.join(PROJECT_ROOT, 'lib') require 'active_record' -require 'factory_girl' +require 'factory_bot' require 'aruba/cucumber' diff --git a/gemfiles/3.2.gemfile.lock b/gemfiles/3.2.gemfile.lock index 3c04a81c2..012f5a379 100644 --- a/gemfiles/3.2.gemfile.lock +++ b/gemfiles/3.2.gemfile.lock @@ -18,7 +18,7 @@ GIT PATH remote: ../ specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM @@ -92,7 +92,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) diff --git a/gemfiles/4.0.gemfile.lock b/gemfiles/4.0.gemfile.lock index 29a2fdcad..8447aab91 100644 --- a/gemfiles/4.0.gemfile.lock +++ b/gemfiles/4.0.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM @@ -92,7 +92,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) diff --git a/gemfiles/4.1.gemfile.lock b/gemfiles/4.1.gemfile.lock index dfd5060f1..f72198fcb 100644 --- a/gemfiles/4.1.gemfile.lock +++ b/gemfiles/4.1.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM @@ -91,7 +91,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) diff --git a/gemfiles/4.2.gemfile.lock b/gemfiles/4.2.gemfile.lock index 40f1d1346..4b0a81d0f 100644 --- a/gemfiles/4.2.gemfile.lock +++ b/gemfiles/4.2.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM @@ -91,7 +91,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) diff --git a/gemfiles/5.0.gemfile.lock b/gemfiles/5.0.gemfile.lock index e0b11b9a0..3a6f60b0c 100644 --- a/gemfiles/5.0.gemfile.lock +++ b/gemfiles/5.0.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - factory_girl (4.8.1) + factory_bot (4.8.1) activesupport (>= 3.0.0) GEM @@ -90,7 +90,7 @@ DEPENDENCIES appraisal (~> 2.1.0) aruba cucumber (~> 1.3.15) - factory_girl! + factory_bot! jdbc-sqlite3 rspec (~> 3.0) rspec-its (~> 1.0) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb new file mode 100644 index 000000000..b17ab0910 --- /dev/null +++ b/lib/factory_bot.rb @@ -0,0 +1,156 @@ +require 'set' +require 'active_support/core_ext/module/delegation' +require 'active_support/deprecation' +require 'active_support/notifications' + +require 'factory_bot/definition_hierarchy' +require 'factory_bot/configuration' +require 'factory_bot/errors' +require 'factory_bot/factory_runner' +require 'factory_bot/strategy_syntax_method_registrar' +require 'factory_bot/strategy_calculator' +require 'factory_bot/strategy/build' +require 'factory_bot/strategy/create' +require 'factory_bot/strategy/attributes_for' +require 'factory_bot/strategy/stub' +require 'factory_bot/strategy/null' +require 'factory_bot/registry' +require 'factory_bot/null_factory' +require 'factory_bot/null_object' +require 'factory_bot/evaluation' +require 'factory_bot/factory' +require 'factory_bot/attribute_assigner' +require 'factory_bot/evaluator' +require 'factory_bot/evaluator_class_definer' +require 'factory_bot/attribute' +require 'factory_bot/callback' +require 'factory_bot/callbacks_observer' +require 'factory_bot/declaration_list' +require 'factory_bot/declaration' +require 'factory_bot/sequence' +require 'factory_bot/attribute_list' +require 'factory_bot/trait' +require 'factory_bot/aliases' +require 'factory_bot/definition' +require 'factory_bot/definition_proxy' +require 'factory_bot/syntax' +require 'factory_bot/syntax_runner' +require 'factory_bot/find_definitions' +require 'factory_bot/reload' +require 'factory_bot/decorator' +require 'factory_bot/decorator/attribute_hash' +require 'factory_bot/decorator/class_key_hash' +require 'factory_bot/decorator/disallows_duplicates_registry' +require 'factory_bot/decorator/invocation_tracker' +require 'factory_bot/decorator/new_constructor' +require 'factory_bot/linter' +require 'factory_bot/version' + +module FactoryBot + def self.configuration + @configuration ||= Configuration.new + end + + def self.reset_configuration + @configuration = nil + end + + # Look for errors in factories and (optionally) their traits. + # Parameters: + # factories - which factories to lint; omit for all factories + # options: + # traits: true - to lint traits as well as factories + # strategy: :create - to specify the strategy for linting + def self.lint(*args) + options = args.extract_options! + factories_to_lint = args[0] || FactoryBot.factories + linting_strategy = options[:traits] ? :factory_and_traits : :factory + factory_strategy = options[:strategy] || :create + Linter.new(factories_to_lint, linting_strategy, factory_strategy).lint! + end + + class << self + delegate :factories, + :sequences, + :traits, + :callbacks, + :strategies, + :callback_names, + :to_create, + :skip_create, + :initialize_with, + :constructor, + :duplicate_attribute_assignment_from_initialize_with, + :duplicate_attribute_assignment_from_initialize_with=, + :allow_class_lookup, + :allow_class_lookup=, + :use_parent_strategy, + :use_parent_strategy=, + to: :configuration + end + + def self.register_factory(factory) + factory.names.each do |name| + factories.register(name, factory) + end + factory + end + + def self.factory_by_name(name) + factories.find(name) + end + + def self.register_sequence(sequence) + sequence.names.each do |name| + sequences.register(name, sequence) + end + sequence + end + + def self.sequence_by_name(name) + sequences.find(name) + end + + def self.register_trait(trait) + trait.names.each do |name| + traits.register(name, trait) + end + trait + end + + def self.trait_by_name(name) + traits.find(name) + end + + def self.register_strategy(strategy_name, strategy_class) + strategies.register(strategy_name, strategy_class) + StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods + end + + def self.strategy_by_name(name) + strategies.find(name) + end + + def self.register_default_strategies + register_strategy(:build, FactoryBot::Strategy::Build) + register_strategy(:create, FactoryBot::Strategy::Create) + register_strategy(:attributes_for, FactoryBot::Strategy::AttributesFor) + register_strategy(:build_stubbed, FactoryBot::Strategy::Stub) + register_strategy(:null, FactoryBot::Strategy::Null) + end + + def self.register_default_callbacks + register_callback(:after_build) + register_callback(:after_create) + register_callback(:after_stub) + register_callback(:before_create) + end + + def self.register_callback(name) + name = name.to_sym + callback_names << name + end +end + +FactoryBot.register_default_strategies +FactoryBot.register_default_callbacks diff --git a/lib/factory_girl/aliases.rb b/lib/factory_bot/aliases.rb similarity index 94% rename from lib/factory_girl/aliases.rb rename to lib/factory_bot/aliases.rb index 47314fb97..cc5d602c1 100644 --- a/lib/factory_girl/aliases.rb +++ b/lib/factory_bot/aliases.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class << self attr_accessor :aliases end diff --git a/lib/factory_girl/attribute.rb b/lib/factory_bot/attribute.rb similarity index 73% rename from lib/factory_girl/attribute.rb rename to lib/factory_bot/attribute.rb index 2d2019f4d..9fab4c7e9 100644 --- a/lib/factory_girl/attribute.rb +++ b/lib/factory_bot/attribute.rb @@ -1,9 +1,9 @@ -require 'factory_girl/attribute/static' -require 'factory_girl/attribute/dynamic' -require 'factory_girl/attribute/association' -require 'factory_girl/attribute/sequence' +require 'factory_bot/attribute/static' +require 'factory_bot/attribute/dynamic' +require 'factory_bot/attribute/association' +require 'factory_bot/attribute/sequence' -module FactoryGirl +module FactoryBot # @api private class Attribute attr_reader :name, :ignored @@ -23,7 +23,7 @@ def association? end def alias_for?(attr) - FactoryGirl.aliases_for(attr).include?(name) + FactoryBot.aliases_for(attr).include?(name) end private @@ -55,7 +55,7 @@ def attribute_name end def error_message - "factory_girl uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'" + "factory_bot uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'" end end end diff --git a/lib/factory_girl/attribute/association.rb b/lib/factory_bot/attribute/association.rb similarity index 96% rename from lib/factory_girl/attribute/association.rb rename to lib/factory_bot/attribute/association.rb index bbf24fe81..219704701 100644 --- a/lib/factory_girl/attribute/association.rb +++ b/lib/factory_bot/attribute/association.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Attribute # @api private class Association < Attribute diff --git a/lib/factory_girl/attribute/dynamic.rb b/lib/factory_bot/attribute/dynamic.rb similarity index 83% rename from lib/factory_girl/attribute/dynamic.rb rename to lib/factory_bot/attribute/dynamic.rb index 55e832517..753ab6514 100644 --- a/lib/factory_girl/attribute/dynamic.rb +++ b/lib/factory_bot/attribute/dynamic.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Attribute # @api private class Dynamic < Attribute @@ -15,7 +15,7 @@ def to_proc when 1, -1 then instance_exec(self, &block) else instance_exec(&block) end - raise SequenceAbuseError if FactoryGirl::Sequence === value + raise SequenceAbuseError if FactoryBot::Sequence === value value } end diff --git a/lib/factory_girl/attribute/sequence.rb b/lib/factory_bot/attribute/sequence.rb similarity index 79% rename from lib/factory_girl/attribute/sequence.rb rename to lib/factory_bot/attribute/sequence.rb index deceb5394..e5c8ec6a8 100644 --- a/lib/factory_girl/attribute/sequence.rb +++ b/lib/factory_bot/attribute/sequence.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Attribute # @api private class Sequence < Attribute @@ -9,7 +9,7 @@ def initialize(name, sequence, ignored) def to_proc sequence = @sequence - -> { FactoryGirl.generate(sequence) } + -> { FactoryBot.generate(sequence) } end end end diff --git a/lib/factory_girl/attribute/static.rb b/lib/factory_bot/attribute/static.rb similarity index 93% rename from lib/factory_girl/attribute/static.rb rename to lib/factory_bot/attribute/static.rb index 3592c6e9c..fae30dc49 100644 --- a/lib/factory_girl/attribute/static.rb +++ b/lib/factory_bot/attribute/static.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Attribute # @api private class Static < Attribute diff --git a/lib/factory_girl/attribute_assigner.rb b/lib/factory_bot/attribute_assigner.rb similarity index 99% rename from lib/factory_girl/attribute_assigner.rb rename to lib/factory_bot/attribute_assigner.rb index fe3d097f8..b5cbe0c5a 100644 --- a/lib/factory_girl/attribute_assigner.rb +++ b/lib/factory_bot/attribute_assigner.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class AttributeAssigner def initialize(evaluator, build_class, &instance_builder) diff --git a/lib/factory_girl/attribute_list.rb b/lib/factory_bot/attribute_list.rb similarity index 98% rename from lib/factory_girl/attribute_list.rb rename to lib/factory_bot/attribute_list.rb index 2a96281e1..d74c18de4 100644 --- a/lib/factory_girl/attribute_list.rb +++ b/lib/factory_bot/attribute_list.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class AttributeList include Enumerable diff --git a/lib/factory_girl/callback.rb b/lib/factory_bot/callback.rb similarity index 84% rename from lib/factory_girl/callback.rb rename to lib/factory_bot/callback.rb index 34df4e613..fce422c0b 100644 --- a/lib/factory_girl/callback.rb +++ b/lib/factory_bot/callback.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Callback attr_reader :name @@ -27,9 +27,9 @@ def ==(other) private def ensure_valid_callback_name! - unless FactoryGirl.callback_names.include?(name) + unless FactoryBot.callback_names.include?(name) raise InvalidCallbackNameError, "#{name} is not a valid callback name. " + - "Valid callback names are #{FactoryGirl.callback_names.inspect}" + "Valid callback names are #{FactoryBot.callback_names.inspect}" end end diff --git a/lib/factory_girl/callbacks_observer.rb b/lib/factory_bot/callbacks_observer.rb similarity index 95% rename from lib/factory_girl/callbacks_observer.rb rename to lib/factory_bot/callbacks_observer.rb index 3056c833e..20c1b06c4 100644 --- a/lib/factory_girl/callbacks_observer.rb +++ b/lib/factory_bot/callbacks_observer.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class CallbacksObserver def initialize(callbacks, evaluator) diff --git a/lib/factory_girl/configuration.rb b/lib/factory_bot/configuration.rb similarity index 92% rename from lib/factory_girl/configuration.rb rename to lib/factory_bot/configuration.rb index 0b7e97234..53d0a8dfa 100644 --- a/lib/factory_girl/configuration.rb +++ b/lib/factory_bot/configuration.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class Configuration attr_reader :factories, :sequences, :traits, :strategies, :callback_names @@ -31,7 +31,7 @@ def duplicate_attribute_assignment_from_initialize_with end def duplicate_attribute_assignment_from_initialize_with=(value) - ActiveSupport::Deprecation.warn 'Assignment of duplicate_attribute_assignment_from_initialize_with is unnecessary as this is now default behavior in FactoryGirl 4.0; this line can be removed', caller + ActiveSupport::Deprecation.warn 'Assignment of duplicate_attribute_assignment_from_initialize_with is unnecessary as this is now default behavior in FactoryBot 4.0; this line can be removed', caller end end end diff --git a/lib/factory_girl/declaration.rb b/lib/factory_bot/declaration.rb similarity index 56% rename from lib/factory_girl/declaration.rb rename to lib/factory_bot/declaration.rb index 7ff60d835..4f71c1c0b 100644 --- a/lib/factory_girl/declaration.rb +++ b/lib/factory_bot/declaration.rb @@ -1,9 +1,9 @@ -require 'factory_girl/declaration/static' -require 'factory_girl/declaration/dynamic' -require 'factory_girl/declaration/association' -require 'factory_girl/declaration/implicit' +require 'factory_bot/declaration/static' +require 'factory_bot/declaration/dynamic' +require 'factory_bot/declaration/association' +require 'factory_bot/declaration/implicit' -module FactoryGirl +module FactoryBot # @api private class Declaration attr_reader :name diff --git a/lib/factory_girl/declaration/association.rb b/lib/factory_bot/declaration/association.rb similarity index 96% rename from lib/factory_girl/declaration/association.rb rename to lib/factory_bot/declaration/association.rb index 2821e3433..cbe4d2f26 100644 --- a/lib/factory_girl/declaration/association.rb +++ b/lib/factory_bot/declaration/association.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Declaration # @api private class Association < Declaration diff --git a/lib/factory_girl/declaration/dynamic.rb b/lib/factory_bot/declaration/dynamic.rb similarity index 96% rename from lib/factory_girl/declaration/dynamic.rb rename to lib/factory_bot/declaration/dynamic.rb index ae575e30f..ec05dfbbd 100644 --- a/lib/factory_girl/declaration/dynamic.rb +++ b/lib/factory_bot/declaration/dynamic.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Declaration # @api private class Dynamic < Declaration diff --git a/lib/factory_girl/declaration/implicit.rb b/lib/factory_bot/declaration/implicit.rb similarity index 83% rename from lib/factory_girl/declaration/implicit.rb rename to lib/factory_bot/declaration/implicit.rb index 529a4b432..e08532c2d 100644 --- a/lib/factory_girl/declaration/implicit.rb +++ b/lib/factory_bot/declaration/implicit.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Declaration # @api private class Implicit < Declaration @@ -19,9 +19,9 @@ def ==(other) private def build - if FactoryGirl.factories.registered?(name) + if FactoryBot.factories.registered?(name) [Attribute::Association.new(name, name, {})] - elsif FactoryGirl.sequences.registered?(name) + elsif FactoryBot.sequences.registered?(name) [Attribute::Sequence.new(name, name, @ignored)] else @factory.inherit_traits([name]) diff --git a/lib/factory_girl/declaration/static.rb b/lib/factory_bot/declaration/static.rb similarity index 96% rename from lib/factory_girl/declaration/static.rb rename to lib/factory_bot/declaration/static.rb index 7cc651ad9..1b773a032 100644 --- a/lib/factory_girl/declaration/static.rb +++ b/lib/factory_bot/declaration/static.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Declaration # @api private class Static < Declaration diff --git a/lib/factory_girl/declaration_list.rb b/lib/factory_bot/declaration_list.rb similarity index 98% rename from lib/factory_girl/declaration_list.rb rename to lib/factory_bot/declaration_list.rb index d39c5afd3..932326cdd 100644 --- a/lib/factory_girl/declaration_list.rb +++ b/lib/factory_bot/declaration_list.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class DeclarationList include Enumerable diff --git a/lib/factory_girl/decorator.rb b/lib/factory_bot/decorator.rb similarity index 95% rename from lib/factory_girl/decorator.rb rename to lib/factory_bot/decorator.rb index f2209532e..cdcaea85c 100644 --- a/lib/factory_girl/decorator.rb +++ b/lib/factory_bot/decorator.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator < BasicObject undef_method :== diff --git a/lib/factory_girl/decorator/attribute_hash.rb b/lib/factory_bot/decorator/attribute_hash.rb similarity index 94% rename from lib/factory_girl/decorator/attribute_hash.rb rename to lib/factory_bot/decorator/attribute_hash.rb index 9d86eafef..62f9fed9c 100644 --- a/lib/factory_girl/decorator/attribute_hash.rb +++ b/lib/factory_bot/decorator/attribute_hash.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator class AttributeHash < Decorator def initialize(component, attributes = []) diff --git a/lib/factory_girl/decorator/class_key_hash.rb b/lib/factory_bot/decorator/class_key_hash.rb similarity index 82% rename from lib/factory_girl/decorator/class_key_hash.rb rename to lib/factory_bot/decorator/class_key_hash.rb index 52b14791b..afcf76ecd 100644 --- a/lib/factory_girl/decorator/class_key_hash.rb +++ b/lib/factory_bot/decorator/class_key_hash.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator class ClassKeyHash < Decorator def [](key) @@ -18,8 +18,8 @@ def key?(key) def symbolized_key(key) if key.respond_to?(:to_sym) key.to_sym - elsif FactoryGirl.allow_class_lookup - ActiveSupport::Deprecation.warn "Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryGirl.allow_class_lookup = false", caller + elsif FactoryBot.allow_class_lookup + ActiveSupport::Deprecation.warn "Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryBot.allow_class_lookup = false", caller key.to_s.underscore.to_sym end end diff --git a/lib/factory_girl/decorator/disallows_duplicates_registry.rb b/lib/factory_bot/decorator/disallows_duplicates_registry.rb similarity index 94% rename from lib/factory_girl/decorator/disallows_duplicates_registry.rb rename to lib/factory_bot/decorator/disallows_duplicates_registry.rb index fe042909f..25ab2d689 100644 --- a/lib/factory_girl/decorator/disallows_duplicates_registry.rb +++ b/lib/factory_bot/decorator/disallows_duplicates_registry.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator class DisallowsDuplicatesRegistry < Decorator def register(name, item) diff --git a/lib/factory_girl/decorator/invocation_tracker.rb b/lib/factory_bot/decorator/invocation_tracker.rb similarity index 94% rename from lib/factory_girl/decorator/invocation_tracker.rb rename to lib/factory_bot/decorator/invocation_tracker.rb index 12bd004c9..9ac27a0b2 100644 --- a/lib/factory_girl/decorator/invocation_tracker.rb +++ b/lib/factory_bot/decorator/invocation_tracker.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator class InvocationTracker < Decorator def initialize(component) diff --git a/lib/factory_girl/decorator/new_constructor.rb b/lib/factory_bot/decorator/new_constructor.rb similarity index 92% rename from lib/factory_girl/decorator/new_constructor.rb rename to lib/factory_bot/decorator/new_constructor.rb index 0d2845e12..5e12c4925 100644 --- a/lib/factory_girl/decorator/new_constructor.rb +++ b/lib/factory_bot/decorator/new_constructor.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Decorator class NewConstructor < Decorator def initialize(component, build_class) diff --git a/lib/factory_girl/definition.rb b/lib/factory_bot/definition.rb similarity index 96% rename from lib/factory_girl/definition.rb rename to lib/factory_bot/definition.rb index 307c5ef96..48c70a064 100644 --- a/lib/factory_girl/definition.rb +++ b/lib/factory_bot/definition.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class Definition attr_reader :defined_traits, :declarations @@ -94,7 +94,7 @@ def after(*names, &block) def callback(*names, &block) names.each do |name| - FactoryGirl.register_callback(name) + FactoryBot.register_callback(name) add_callback(Callback.new(name, block)) end end @@ -110,7 +110,7 @@ def additional_traits end def trait_by_name(name) - trait_for(name) || FactoryGirl.trait_by_name(name) + trait_for(name) || FactoryBot.trait_by_name(name) end def trait_for(name) diff --git a/lib/factory_girl/definition_hierarchy.rb b/lib/factory_bot/definition_hierarchy.rb similarity index 89% rename from lib/factory_girl/definition_hierarchy.rb rename to lib/factory_bot/definition_hierarchy.rb index 77807f554..de7dc628a 100644 --- a/lib/factory_girl/definition_hierarchy.rb +++ b/lib/factory_bot/definition_hierarchy.rb @@ -1,15 +1,15 @@ -module FactoryGirl +module FactoryBot class DefinitionHierarchy def callbacks - FactoryGirl.callbacks + FactoryBot.callbacks end def constructor - FactoryGirl.constructor + FactoryBot.constructor end def to_create - FactoryGirl.to_create + FactoryBot.to_create end def self.build_from_definition(definition) diff --git a/lib/factory_girl/definition_proxy.rb b/lib/factory_bot/definition_proxy.rb similarity index 94% rename from lib/factory_girl/definition_proxy.rb rename to lib/factory_bot/definition_proxy.rb index 93372d97f..3b31b1e40 100644 --- a/lib/factory_girl/definition_proxy.rb +++ b/lib/factory_bot/definition_proxy.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class DefinitionProxy UNPROXIED_METHODS = %w(__send__ __id__ nil? send object_id extend instance_eval initialize block_given? raise caller method) @@ -18,7 +18,7 @@ def initialize(definition, ignore = false) def singleton_method_added(name) message = "Defining methods in blocks (trait or factory) is not supported (#{name})" - raise FactoryGirl::MethodDefinitionError, message + raise FactoryBot::MethodDefinitionError, message end # Adds an attribute that should be assigned on generated instances for this @@ -29,7 +29,7 @@ def singleton_method_added(name) # instance is generated. Lazy attribute blocks will not be called if that # attribute is overridden for a specific instance. # - # When defining lazy attributes, an instance of FactoryGirl::Strategy will + # When defining lazy attributes, an instance of FactoryBot::Strategy will # be yielded, allowing associations to be built using the correct build # strategy. # @@ -77,7 +77,7 @@ def transient(&block) # # are equivalent. # - # If no argument or block is given, factory_girl will look for a sequence + # If no argument or block is given, factory_bot will look for a sequence # or association with the same name. This means that: # # factory :user do @@ -115,7 +115,7 @@ def method_missing(name, *args, &block) # sequence(:email) { |n| "person#{n}@example.com" } # # factory :user do - # email { FactoryGirl.generate(:email) } + # email { FactoryBot.generate(:email) } # end # # Except that no globally available sequence will be defined. diff --git a/lib/factory_girl/errors.rb b/lib/factory_bot/errors.rb similarity index 98% rename from lib/factory_girl/errors.rb rename to lib/factory_bot/errors.rb index 5c0956d16..07692d3b5 100644 --- a/lib/factory_girl/errors.rb +++ b/lib/factory_bot/errors.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # Raised when a factory is defined that attempts to instantiate itself. class AssociationDefinitionError < RuntimeError; end diff --git a/lib/factory_girl/evaluation.rb b/lib/factory_bot/evaluation.rb similarity index 96% rename from lib/factory_girl/evaluation.rb rename to lib/factory_bot/evaluation.rb index ad66298d2..e5336fc6a 100644 --- a/lib/factory_girl/evaluation.rb +++ b/lib/factory_bot/evaluation.rb @@ -1,6 +1,6 @@ require 'observer' -module FactoryGirl +module FactoryBot class Evaluation include Observable diff --git a/lib/factory_girl/evaluator.rb b/lib/factory_bot/evaluator.rb similarity index 95% rename from lib/factory_girl/evaluator.rb rename to lib/factory_bot/evaluator.rb index 19e58d2a2..40e33947d 100644 --- a/lib/factory_girl/evaluator.rb +++ b/lib/factory_bot/evaluator.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/class/attribute' -module FactoryGirl +module FactoryBot # @api private class Evaluator class_attribute :attribute_lists @@ -24,7 +24,7 @@ def initialize(build_strategy, overrides = {}) def association(factory_name, *traits_and_overrides) overrides = traits_and_overrides.extract_options! strategy_override = overrides.fetch(:strategy) do - FactoryGirl.use_parent_strategy ? @build_strategy.class : :create + FactoryBot.use_parent_strategy ? @build_strategy.class : :create end traits_and_overrides += [overrides.except(:strategy)] diff --git a/lib/factory_girl/evaluator_class_definer.rb b/lib/factory_bot/evaluator_class_definer.rb similarity index 96% rename from lib/factory_girl/evaluator_class_definer.rb rename to lib/factory_bot/evaluator_class_definer.rb index 7ab0f9706..8090f0138 100644 --- a/lib/factory_girl/evaluator_class_definer.rb +++ b/lib/factory_bot/evaluator_class_definer.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class EvaluatorClassDefiner def initialize(attributes, parent_class) diff --git a/lib/factory_girl/factory.rb b/lib/factory_bot/factory.rb similarity index 96% rename from lib/factory_girl/factory.rb rename to lib/factory_bot/factory.rb index 4ffabb49b..7aa05ad25 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_bot/factory.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext/hash/keys' require 'active_support/inflector' -module FactoryGirl +module FactoryBot # @api private class Factory attr_reader :name, :definition @@ -59,7 +59,7 @@ def associations # # ... # end # - # FactoryGirl.create(:author).class + # FactoryBot.create(:author).class # # => User # # Because an attribute defined without a value or block will build an @@ -74,7 +74,7 @@ def associations # author # end # - # FactoryGirl.create(:post).author.class + # FactoryBot.create(:post).author.class # # => User def names [name] + @aliases @@ -145,7 +145,7 @@ def assert_valid_options(options) def parent if @parent - FactoryGirl.factory_by_name(@parent) + FactoryBot.factory_by_name(@parent) else NullFactory.new end diff --git a/lib/factory_girl/factory_runner.rb b/lib/factory_bot/factory_runner.rb similarity index 79% rename from lib/factory_girl/factory_runner.rb rename to lib/factory_bot/factory_runner.rb index 89cd8a3f7..91cd1cba4 100644 --- a/lib/factory_girl/factory_runner.rb +++ b/lib/factory_bot/factory_runner.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class FactoryRunner def initialize(name, strategy, traits_and_overrides) @name = name @@ -9,7 +9,7 @@ def initialize(name, strategy, traits_and_overrides) end def run(runner_strategy = @strategy, &block) - factory = FactoryGirl.factory_by_name(@name) + factory = FactoryBot.factory_by_name(@name) factory.compile @@ -25,7 +25,7 @@ def run(runner_strategy = @strategy, &block) factory: factory } - ActiveSupport::Notifications.instrument('factory_girl.run_factory', instrumentation_payload) do + ActiveSupport::Notifications.instrument('factory_bot.run_factory', instrumentation_payload) do factory.run(runner_strategy, @overrides, &block) end end diff --git a/lib/factory_girl/find_definitions.rb b/lib/factory_bot/find_definitions.rb similarity index 88% rename from lib/factory_girl/find_definitions.rb rename to lib/factory_bot/find_definitions.rb index a88d251ff..85177d7a5 100644 --- a/lib/factory_girl/find_definitions.rb +++ b/lib/factory_bot/find_definitions.rb @@ -1,7 +1,7 @@ -module FactoryGirl +module FactoryBot class << self # An Array of strings specifying locations that should be searched for - # factory definitions. By default, factory_girl will attempt to require + # factory definitions. By default, factory_bot will attempt to require # "factories", "test/factories" and "spec/factories". Only the first # existing file will be loaded. attr_accessor :definition_file_paths diff --git a/lib/factory_girl/linter.rb b/lib/factory_bot/linter.rb similarity index 93% rename from lib/factory_girl/linter.rb rename to lib/factory_bot/linter.rb index 34a327094..ecde3d855 100644 --- a/lib/factory_girl/linter.rb +++ b/lib/factory_bot/linter.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Linter def initialize(factories, linting_strategy, factory_strategy = :create) @@ -56,7 +56,7 @@ def location def lint_factory(factory) result = [] begin - FactoryGirl.public_send(factory_strategy, factory.name) + FactoryBot.public_send(factory_strategy, factory.name) rescue => error result |= [FactoryError.new(error, factory)] end @@ -67,7 +67,7 @@ def lint_traits(factory) result = [] factory.definition.defined_traits.map(&:name).each do |trait_name| begin - FactoryGirl.public_send(factory_strategy, factory.name, trait_name) + FactoryBot.public_send(factory_strategy, factory.name, trait_name) rescue => error result |= [FactoryTraitError.new(error, factory, trait_name)] diff --git a/lib/factory_girl/null_factory.rb b/lib/factory_bot/null_factory.rb similarity index 69% rename from lib/factory_girl/null_factory.rb rename to lib/factory_bot/null_factory.rb index fe0b9d60f..bc9720dc6 100644 --- a/lib/factory_girl/null_factory.rb +++ b/lib/factory_bot/null_factory.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class NullFactory attr_reader :definition @@ -12,7 +12,7 @@ def initialize def compile; end def class_name; end - def evaluator_class; FactoryGirl::Evaluator; end - def hierarchy_class; FactoryGirl::DefinitionHierarchy; end + def evaluator_class; FactoryBot::Evaluator; end + def hierarchy_class; FactoryBot::DefinitionHierarchy; end end end diff --git a/lib/factory_girl/null_object.rb b/lib/factory_bot/null_object.rb similarity index 96% rename from lib/factory_girl/null_object.rb rename to lib/factory_bot/null_object.rb index 1737439f4..c576b7fc8 100644 --- a/lib/factory_girl/null_object.rb +++ b/lib/factory_bot/null_object.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class NullObject < ::BasicObject def initialize(methods_to_respond_to) diff --git a/lib/factory_girl/registry.rb b/lib/factory_bot/registry.rb similarity index 96% rename from lib/factory_girl/registry.rb rename to lib/factory_bot/registry.rb index 19c5f34bd..c7240fdd4 100644 --- a/lib/factory_girl/registry.rb +++ b/lib/factory_bot/registry.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot class Registry include Enumerable diff --git a/lib/factory_girl/reload.rb b/lib/factory_bot/reload.rb similarity index 87% rename from lib/factory_girl/reload.rb rename to lib/factory_bot/reload.rb index 8cbb51aa5..171f12fe0 100644 --- a/lib/factory_girl/reload.rb +++ b/lib/factory_bot/reload.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot def self.reload reset_configuration register_default_strategies diff --git a/lib/factory_girl/sequence.rb b/lib/factory_bot/sequence.rb similarity index 91% rename from lib/factory_girl/sequence.rb rename to lib/factory_bot/sequence.rb index 1ec88c70f..6b50f9bfd 100644 --- a/lib/factory_girl/sequence.rb +++ b/lib/factory_bot/sequence.rb @@ -1,6 +1,6 @@ -module FactoryGirl +module FactoryBot - # Sequences are defined using sequence within a FactoryGirl.define block. + # Sequences are defined using sequence within a FactoryBot.define block. # Sequence values are generated using next. # @api private class Sequence diff --git a/lib/factory_girl/strategy/attributes_for.rb b/lib/factory_bot/strategy/attributes_for.rb similarity index 90% rename from lib/factory_girl/strategy/attributes_for.rb rename to lib/factory_bot/strategy/attributes_for.rb index 84862d162..c4273a0c6 100644 --- a/lib/factory_girl/strategy/attributes_for.rb +++ b/lib/factory_bot/strategy/attributes_for.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Strategy class AttributesFor def association(runner) diff --git a/lib/factory_girl/strategy/build.rb b/lib/factory_bot/strategy/build.rb similarity index 93% rename from lib/factory_girl/strategy/build.rb rename to lib/factory_bot/strategy/build.rb index f9ef70939..e65285227 100644 --- a/lib/factory_girl/strategy/build.rb +++ b/lib/factory_bot/strategy/build.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Strategy class Build def association(runner) diff --git a/lib/factory_girl/strategy/create.rb b/lib/factory_bot/strategy/create.rb similarity index 95% rename from lib/factory_girl/strategy/create.rb rename to lib/factory_bot/strategy/create.rb index fd5986d32..1e66e58cc 100644 --- a/lib/factory_girl/strategy/create.rb +++ b/lib/factory_bot/strategy/create.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Strategy class Create def association(runner) diff --git a/lib/factory_girl/strategy/null.rb b/lib/factory_bot/strategy/null.rb similarity index 87% rename from lib/factory_girl/strategy/null.rb rename to lib/factory_bot/strategy/null.rb index cd4ec3191..893826f58 100644 --- a/lib/factory_girl/strategy/null.rb +++ b/lib/factory_bot/strategy/null.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Strategy class Null def association(runner) diff --git a/lib/factory_girl/strategy/stub.rb b/lib/factory_bot/strategy/stub.rb similarity index 99% rename from lib/factory_girl/strategy/stub.rb rename to lib/factory_bot/strategy/stub.rb index 73b0db27b..efc776c00 100644 --- a/lib/factory_girl/strategy/stub.rb +++ b/lib/factory_bot/strategy/stub.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Strategy class Stub @@next_id = 1000 diff --git a/lib/factory_girl/strategy_calculator.rb b/lib/factory_bot/strategy_calculator.rb similarity index 84% rename from lib/factory_girl/strategy_calculator.rb rename to lib/factory_bot/strategy_calculator.rb index 4df322c5d..eae993923 100644 --- a/lib/factory_girl/strategy_calculator.rb +++ b/lib/factory_bot/strategy_calculator.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class StrategyCalculator def initialize(name_or_object) @@ -20,7 +20,7 @@ def strategy_is_object? end def strategy_name_to_object - FactoryGirl.strategy_by_name(@name_or_object) + FactoryBot.strategy_by_name(@name_or_object) end end end diff --git a/lib/factory_girl/strategy_syntax_method_registrar.rb b/lib/factory_bot/strategy_syntax_method_registrar.rb similarity index 95% rename from lib/factory_girl/strategy_syntax_method_registrar.rb rename to lib/factory_bot/strategy_syntax_method_registrar.rb index 9c9cffbec..6be719f03 100644 --- a/lib/factory_girl/strategy_syntax_method_registrar.rb +++ b/lib/factory_bot/strategy_syntax_method_registrar.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class StrategySyntaxMethodRegistrar def initialize(strategy_name) @@ -42,7 +42,7 @@ def define_pair_strategy_method end def define_syntax_method(name, &block) - FactoryGirl::Syntax::Methods.module_exec do + FactoryBot::Syntax::Methods.module_exec do if method_defined?(name) || private_method_defined?(name) undef_method(name) end diff --git a/lib/factory_bot/syntax.rb b/lib/factory_bot/syntax.rb new file mode 100644 index 000000000..dabde7c25 --- /dev/null +++ b/lib/factory_bot/syntax.rb @@ -0,0 +1,7 @@ +require 'factory_bot/syntax/methods' +require 'factory_bot/syntax/default' + +module FactoryBot + module Syntax + end +end diff --git a/lib/factory_girl/syntax/default.rb b/lib/factory_bot/syntax/default.rb similarity index 69% rename from lib/factory_girl/syntax/default.rb rename to lib/factory_bot/syntax/default.rb index 41a5266dc..a9b8235b6 100644 --- a/lib/factory_girl/syntax/default.rb +++ b/lib/factory_bot/syntax/default.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot module Syntax module Default include Methods @@ -14,10 +14,10 @@ def modify(&block) class DSL def factory(name, options = {}, &block) factory = Factory.new(name, options) - proxy = FactoryGirl::DefinitionProxy.new(factory.definition) + proxy = FactoryBot::DefinitionProxy.new(factory.definition) proxy.instance_eval(&block) if block_given? - FactoryGirl.register_factory(factory) + FactoryBot.register_factory(factory) proxy.child_factories.each do |(child_name, child_options, child_block)| parent_factory = child_options.delete(:parent) || name @@ -26,23 +26,23 @@ def factory(name, options = {}, &block) end def sequence(name, *args, &block) - FactoryGirl.register_sequence(Sequence.new(name, *args, &block)) + FactoryBot.register_sequence(Sequence.new(name, *args, &block)) end def trait(name, &block) - FactoryGirl.register_trait(Trait.new(name, &block)) + FactoryBot.register_trait(Trait.new(name, &block)) end def to_create(&block) - FactoryGirl.to_create(&block) + FactoryBot.to_create(&block) end def skip_create - FactoryGirl.skip_create + FactoryBot.skip_create end def initialize_with(&block) - FactoryGirl.initialize_with(&block) + FactoryBot.initialize_with(&block) end def self.run(block) @@ -54,14 +54,14 @@ def self.run(block) private def configuration - FactoryGirl.configuration + FactoryBot.configuration end end class ModifyDSL def factory(name, options = {}, &block) - factory = FactoryGirl.factory_by_name(name) - proxy = FactoryGirl::DefinitionProxy.new(factory.definition.overridable) + factory = FactoryBot.factory_by_name(name) + proxy = FactoryBot::DefinitionProxy.new(factory.definition.overridable) proxy.instance_eval(&block) end diff --git a/lib/factory_girl/syntax/methods.rb b/lib/factory_bot/syntax/methods.rb similarity index 93% rename from lib/factory_girl/syntax/methods.rb rename to lib/factory_bot/syntax/methods.rb index 1dcceaad5..286502e08 100644 --- a/lib/factory_girl/syntax/methods.rb +++ b/lib/factory_bot/syntax/methods.rb @@ -1,7 +1,7 @@ -module FactoryGirl +module FactoryBot module Syntax ## This module is a container for all strategy methods provided by - ## FactoryGirl. This includes all the default strategies provided ({Methods#build}, + ## FactoryBot. This includes all the default strategies provided ({Methods#build}, ## {Methods#create}, {Methods#build_stubbed}, and {Methods#attributes_for}), as well as ## the complementary *_list methods. ## @example singular factory execution @@ -30,7 +30,7 @@ module Syntax ## # factory with traits and attribute override ## build_stubbed_list(:user, 15, :admin, :male, name: "John Doe") module Methods - # @!parse FactoryGirl.register_default_strategies + # @!parse FactoryBot.register_default_strategies # @!method build(name, *traits_and_overrides, &block) # (see #strategy_method) # Builds a registered factory by name. @@ -88,7 +88,7 @@ module Methods # Returns: # The next value in the sequence. (Object) def generate(name) - FactoryGirl.sequence_by_name(name).next + FactoryBot.sequence_by_name(name).next end # Generates and returns the list of values in a sequence. @@ -103,7 +103,7 @@ def generate(name) # The next value in the sequence. (Object) def generate_list(name, count) (1..count).map do - FactoryGirl.sequence_by_name(name).next + FactoryBot.sequence_by_name(name).next end end end diff --git a/lib/factory_girl/syntax_runner.rb b/lib/factory_bot/syntax_runner.rb similarity index 80% rename from lib/factory_girl/syntax_runner.rb rename to lib/factory_bot/syntax_runner.rb index 7b6717c82..81fff1fe0 100644 --- a/lib/factory_girl/syntax_runner.rb +++ b/lib/factory_bot/syntax_runner.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class SyntaxRunner include Syntax::Methods diff --git a/lib/factory_girl/trait.rb b/lib/factory_bot/trait.rb similarity index 87% rename from lib/factory_girl/trait.rb rename to lib/factory_bot/trait.rb index 74ba1eb9a..84bc323e8 100644 --- a/lib/factory_girl/trait.rb +++ b/lib/factory_bot/trait.rb @@ -1,4 +1,4 @@ -module FactoryGirl +module FactoryBot # @api private class Trait attr_reader :name, :definition @@ -8,7 +8,7 @@ def initialize(name, &block) @block = block @definition = Definition.new(@name) - proxy = FactoryGirl::DefinitionProxy.new(@definition) + proxy = FactoryBot::DefinitionProxy.new(@definition) proxy.instance_eval(&@block) if block_given? end diff --git a/lib/factory_bot/version.rb b/lib/factory_bot/version.rb new file mode 100644 index 000000000..99d96ec2d --- /dev/null +++ b/lib/factory_bot/version.rb @@ -0,0 +1,3 @@ +module FactoryBot + VERSION = '4.8.1'.freeze +end diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 2dc4bc18e..4c4b98170 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -1,156 +1,5 @@ -require 'set' -require 'active_support/core_ext/module/delegation' -require 'active_support/deprecation' -require 'active_support/notifications' +require "factory_bot" -require 'factory_girl/definition_hierarchy' -require 'factory_girl/configuration' -require 'factory_girl/errors' -require 'factory_girl/factory_runner' -require 'factory_girl/strategy_syntax_method_registrar' -require 'factory_girl/strategy_calculator' -require 'factory_girl/strategy/build' -require 'factory_girl/strategy/create' -require 'factory_girl/strategy/attributes_for' -require 'factory_girl/strategy/stub' -require 'factory_girl/strategy/null' -require 'factory_girl/registry' -require 'factory_girl/null_factory' -require 'factory_girl/null_object' -require 'factory_girl/evaluation' -require 'factory_girl/factory' -require 'factory_girl/attribute_assigner' -require 'factory_girl/evaluator' -require 'factory_girl/evaluator_class_definer' -require 'factory_girl/attribute' -require 'factory_girl/callback' -require 'factory_girl/callbacks_observer' -require 'factory_girl/declaration_list' -require 'factory_girl/declaration' -require 'factory_girl/sequence' -require 'factory_girl/attribute_list' -require 'factory_girl/trait' -require 'factory_girl/aliases' -require 'factory_girl/definition' -require 'factory_girl/definition_proxy' -require 'factory_girl/syntax' -require 'factory_girl/syntax_runner' -require 'factory_girl/find_definitions' -require 'factory_girl/reload' -require 'factory_girl/decorator' -require 'factory_girl/decorator/attribute_hash' -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' +FactoryGirl = FactoryBot -module FactoryGirl - def self.configuration - @configuration ||= Configuration.new - end - - def self.reset_configuration - @configuration = nil - end - - # Look for errors in factories and (optionally) their traits. - # Parameters: - # factories - which factories to lint; omit for all factories - # options: - # traits: true - to lint traits as well as factories - # strategy: :create - to specify the strategy for linting - def self.lint(*args) - options = args.extract_options! - factories_to_lint = args[0] || FactoryGirl.factories - linting_strategy = options[:traits] ? :factory_and_traits : :factory - factory_strategy = options[:strategy] || :create - Linter.new(factories_to_lint, linting_strategy, factory_strategy).lint! - end - - class << self - delegate :factories, - :sequences, - :traits, - :callbacks, - :strategies, - :callback_names, - :to_create, - :skip_create, - :initialize_with, - :constructor, - :duplicate_attribute_assignment_from_initialize_with, - :duplicate_attribute_assignment_from_initialize_with=, - :allow_class_lookup, - :allow_class_lookup=, - :use_parent_strategy, - :use_parent_strategy=, - to: :configuration - end - - def self.register_factory(factory) - factory.names.each do |name| - factories.register(name, factory) - end - factory - end - - def self.factory_by_name(name) - factories.find(name) - end - - def self.register_sequence(sequence) - sequence.names.each do |name| - sequences.register(name, sequence) - end - sequence - end - - def self.sequence_by_name(name) - sequences.find(name) - end - - def self.register_trait(trait) - trait.names.each do |name| - traits.register(name, trait) - end - trait - end - - def self.trait_by_name(name) - traits.find(name) - end - - def self.register_strategy(strategy_name, strategy_class) - strategies.register(strategy_name, strategy_class) - StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods - end - - def self.strategy_by_name(name) - strategies.find(name) - end - - def self.register_default_strategies - register_strategy(:build, FactoryGirl::Strategy::Build) - register_strategy(:create, FactoryGirl::Strategy::Create) - register_strategy(:attributes_for, FactoryGirl::Strategy::AttributesFor) - register_strategy(:build_stubbed, FactoryGirl::Strategy::Stub) - register_strategy(:null, FactoryGirl::Strategy::Null) - end - - def self.register_default_callbacks - register_callback(:after_build) - register_callback(:after_create) - register_callback(:after_stub) - register_callback(:before_create) - end - - def self.register_callback(name) - name = name.to_sym - callback_names << name - end -end - -FactoryGirl.register_default_strategies -FactoryGirl.register_default_callbacks +warn "The factory_girl gem has been deprecated and has been replaced by factory_bot. Please switch to factory_bot as soon as possible." diff --git a/lib/factory_girl/syntax.rb b/lib/factory_girl/syntax.rb deleted file mode 100644 index 65f0ef3a9..000000000 --- a/lib/factory_girl/syntax.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'factory_girl/syntax/methods' -require 'factory_girl/syntax/default' - -module FactoryGirl - module Syntax - end -end diff --git a/lib/factory_girl/version.rb b/lib/factory_girl/version.rb index ff2ce5f6e..5a241f9a4 100644 --- a/lib/factory_girl/version.rb +++ b/lib/factory_girl/version.rb @@ -1,3 +1 @@ -module FactoryGirl - VERSION = '4.8.1'.freeze -end +VERSION = '4.8.1'.freeze diff --git a/spec/acceptance/activesupport_instrumentation_spec.rb b/spec/acceptance/activesupport_instrumentation_spec.rb index 3979a5d8d..0b8bcb4b6 100644 --- a/spec/acceptance/activesupport_instrumentation_spec.rb +++ b/spec/acceptance/activesupport_instrumentation_spec.rb @@ -14,11 +14,11 @@ def subscribed(callback, *args, &block) end describe "using ActiveSupport::Instrumentation to track factory interaction" do - let(:slow_user_factory) { FactoryGirl.factory_by_name("slow_user") } - let(:user_factory) { FactoryGirl.factory_by_name("user") } + let(:slow_user_factory) { FactoryBot.factory_by_name("slow_user") } + let(:user_factory) { FactoryBot.factory_by_name("user") } before do define_model("User", email: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do email "john@example.com" @@ -33,8 +33,8 @@ def subscribed(callback, *args, &block) it "tracks proper time of creating the record" do time_to_execute = 0 callback = ->(name, start, finish, id, payload) { time_to_execute = finish - start } - ActiveSupport::Notifications.subscribed(callback, "factory_girl.run_factory") do - FactoryGirl.build(:slow_user) + ActiveSupport::Notifications.subscribed(callback, "factory_bot.run_factory") do + FactoryBot.build(:slow_user) end expect(time_to_execute).to be >= 0.1 @@ -53,11 +53,11 @@ def subscribed(callback, *args, &block) tracked_invocations[factory_name][:factory] = factory end - ActiveSupport::Notifications.subscribed(callback, "factory_girl.run_factory") do - FactoryGirl.build_list(:slow_user, 2) - FactoryGirl.build_list(:user, 5) - FactoryGirl.create_list(:user, 2) - FactoryGirl.attributes_for(:slow_user) + ActiveSupport::Notifications.subscribed(callback, "factory_bot.run_factory") do + FactoryBot.build_list(:slow_user, 2) + FactoryBot.build_list(:user, 5) + FactoryBot.create_list(:user, 2) + FactoryBot.attributes_for(:slow_user) end expect(tracked_invocations[:slow_user][:build]).to eq(2) diff --git a/spec/acceptance/aliases_spec.rb b/spec/acceptance/aliases_spec.rb index 3b116fb72..88c30352f 100644 --- a/spec/acceptance/aliases_spec.rb +++ b/spec/acceptance/aliases_spec.rb @@ -2,18 +2,18 @@ describe "aliases and overrides" do before do - FactoryGirl.aliases << [/one/, "two"] + FactoryBot.aliases << [/one/, "two"] define_model("User", two: :string, one: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do two "set value" end end end - subject { FactoryGirl.create(:user, one: "override") } + subject { FactoryBot.create(:user, one: "override") } its(:one) { should eq "override" } its(:two) { should be_nil } end diff --git a/spec/acceptance/attribute_aliases_spec.rb b/spec/acceptance/attribute_aliases_spec.rb index d359f4228..d5bd5c59a 100644 --- a/spec/acceptance/attribute_aliases_spec.rb +++ b/spec/acceptance/attribute_aliases_spec.rb @@ -8,7 +8,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user do factory :user_with_name do name "John Doe" @@ -26,7 +26,7 @@ end context "assigning an association by foreign key" do - subject { FactoryGirl.build(:post, user_id: 1) } + subject { FactoryBot.build(:post, user_id: 1) } it "doesn't assign both an association and its foreign key" do expect(subject.user_id).to eq 1 @@ -34,7 +34,7 @@ end context "assigning an association by passing factory" do - subject { FactoryGirl.create(:post_with_named_user).user } + subject { FactoryBot.create(:post_with_named_user).user } it "assigns attributes correctly" do expect(subject.name).to eq "John Doe" diff --git a/spec/acceptance/attribute_existing_on_object_spec.rb b/spec/acceptance/attribute_existing_on_object_spec.rb index 8ace37cc4..197f7a4c0 100644 --- a/spec/acceptance/attribute_existing_on_object_spec.rb +++ b/spec/acceptance/attribute_existing_on_object_spec.rb @@ -4,7 +4,7 @@ before do define_model("Website", system: :boolean, link: :string, sleep: :integer) - FactoryGirl.define do + FactoryBot.define do factory :website do system false link "http://example.com" @@ -13,7 +13,7 @@ end end - subject { FactoryGirl.build(:website, sleep: -5) } + subject { FactoryBot.build(:website, sleep: -5) } its(:system) { should eq false } its(:link) { should eq "http://example.com" } @@ -30,7 +30,7 @@ def some_funky_method(args) end end - FactoryGirl.define do + FactoryBot.define do factory :website do more_format { "format: #{format}" } end @@ -41,7 +41,7 @@ def some_funky_method(args) Object.send(:undef_method, :some_funky_method) end - subject { FactoryGirl.build(:website, format: "Great", y: 12345, some_funky_method: "foobar!") } + subject { FactoryBot.build(:website, format: "Great", y: 12345, some_funky_method: "foobar!") } its(:format) { should eq "Great" } its(:y) { should eq 12345 } its(:more_format) { should eq "format: Great" } @@ -56,13 +56,13 @@ def format end end - FactoryGirl.define do + FactoryBot.define do factory :website do more_format { "format: #{format}" } end end end - subject { FactoryGirl.build(:website) } + subject { FactoryBot.build(:website) } its(:more_format) { should eq "format: This is an awesome format" } end diff --git a/spec/acceptance/attributes_for_spec.rb b/spec/acceptance/attributes_for_spec.rb index f47c5403a..7eb53ac09 100644 --- a/spec/acceptance/attributes_for_spec.rb +++ b/spec/acceptance/attributes_for_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a generated attributes hash" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -15,7 +15,7 @@ has_many :comments end - FactoryGirl.define do + FactoryBot.define do factory :user factory :comment @@ -52,12 +52,12 @@ end describe "calling `attributes_for` with a block" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :company end end @@ -85,13 +85,13 @@ def initialize(arg1, arg2); end end - FactoryGirl.define do + FactoryBot.define do factory :user do name "John Doe" end end end - subject { FactoryGirl.attributes_for(:user) } + subject { FactoryBot.attributes_for(:user) } its([:name]) { should eq "John Doe" } end diff --git a/spec/acceptance/attributes_from_instance_spec.rb b/spec/acceptance/attributes_from_instance_spec.rb index 7d644ec45..00bca52d7 100644 --- a/spec/acceptance/attributes_from_instance_spec.rb +++ b/spec/acceptance/attributes_from_instance_spec.rb @@ -8,7 +8,7 @@ def age end end - FactoryGirl.define do + FactoryBot.define do factory :user do age_copy { age } end @@ -17,37 +17,37 @@ def age context "without the attribute being overridden" do it "returns the correct value from the instance" do - expect(FactoryGirl.build(:user).age_copy).to eq 18 + expect(FactoryBot.build(:user).age_copy).to eq 18 end it "returns nil during attributes_for" do - expect(FactoryGirl.attributes_for(:user)[:age_copy]).to be_nil + expect(FactoryBot.attributes_for(:user)[:age_copy]).to be_nil end it "doesn't instantiate a record with attributes_for" do allow(User).to receive(:new) - FactoryGirl.attributes_for(:user) + FactoryBot.attributes_for(:user) expect(User).to_not have_received(:new) end end context "with the attribute being overridden" do it "uses the overridden value" do - expect(FactoryGirl.build(:user, age_copy: nil).age_copy).to be_nil + expect(FactoryBot.build(:user, age_copy: nil).age_copy).to be_nil end it "uses the overridden value during attributes_for" do - expect(FactoryGirl.attributes_for(:user, age_copy: 25)[:age_copy]).to eq 25 + expect(FactoryBot.attributes_for(:user, age_copy: 25)[:age_copy]).to eq 25 end end context "with the referenced attribute being overridden" do it "uses the overridden value" do - expect(FactoryGirl.build(:user, age: nil).age_copy).to be_nil + expect(FactoryBot.build(:user, age: nil).age_copy).to be_nil end it "uses the overridden value during attributes_for" do - expect(FactoryGirl.attributes_for(:user, age: 25)[:age_copy]).to eq 25 + expect(FactoryBot.attributes_for(:user, age: 25)[:age_copy]).to eq 25 end end end diff --git a/spec/acceptance/attributes_ordered_spec.rb b/spec/acceptance/attributes_ordered_spec.rb index ae6fe90df..2e014cd2f 100644 --- a/spec/acceptance/attributes_ordered_spec.rb +++ b/spec/acceptance/attributes_ordered_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a generated attributes hash where order matters" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('ParentModel', static: :integer, @@ -9,7 +9,7 @@ evaluates_second: :integer, evaluates_third: :integer) - FactoryGirl.define do + FactoryBot.define do factory :parent_model do evaluates_first { static } evaluates_second { evaluates_first } @@ -30,7 +30,7 @@ end context "factory with a parent" do - subject { FactoryGirl.build(:child_model) } + subject { FactoryBot.build(:child_model) } it "assigns attributes in the order they're defined with preference to static attributes" do expect(subject[:evaluates_first]).to eq 1 @@ -40,7 +40,7 @@ end context "factory without a parent" do - subject { FactoryGirl.build(:without_parent) } + subject { FactoryBot.build(:without_parent) } it "assigns attributes in the order they're defined with preference to static attributes without a parent class" do expect(subject[:evaluates_first]).to eq 1 diff --git a/spec/acceptance/build_list_spec.rb b/spec/acceptance/build_list_spec.rb index 49dd71a53..8cf193855 100644 --- a/spec/acceptance/build_list_spec.rb +++ b/spec/acceptance/build_list_spec.rb @@ -4,7 +4,7 @@ before do define_model('Post', title: :string, position: :integer) - FactoryGirl.define do + FactoryBot.define do factory(:post) do |post| post.title "Through the Looking Glass" post.position { rand(10**4) } @@ -13,7 +13,7 @@ end context "without default attributes" do - subject { FactoryGirl.build_list(:post, 20) } + subject { FactoryBot.build_list(:post, 20) } its(:length) { should eq 20 } @@ -31,7 +31,7 @@ end context "with default attributes" do - subject { FactoryGirl.build_list(:post, 20, title: "The Hunting of the Snark") } + subject { FactoryBot.build_list(:post, 20, title: "The Hunting of the Snark") } it "overrides the default values" do subject.each do |record| @@ -42,7 +42,7 @@ context "with a block" do subject do - FactoryGirl.build_list(:post, 20, title: "The Listing of the Block") do |post| + FactoryBot.build_list(:post, 20, title: "The Listing of the Block") do |post| post.position = post.id end end diff --git a/spec/acceptance/build_spec.rb b/spec/acceptance/build_spec.rb index fe09cd079..337553073 100644 --- a/spec/acceptance/build_spec.rb +++ b/spec/acceptance/build_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a built instance" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -10,7 +10,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -24,7 +24,7 @@ it { should be_new_record } context "when the :use_parent_strategy config option has not been set" do - before { FactoryGirl.use_parent_strategy = nil } + before { FactoryBot.use_parent_strategy = nil } it "assigns and saves associations" do expect(subject.user).to be_kind_of(User) @@ -33,7 +33,7 @@ end context "when the :use_parent_strategy config option has been enabled" do - before { FactoryGirl.use_parent_strategy = true } + before { FactoryBot.use_parent_strategy = true } it "assigns but does not save associations" do expect(subject.user).to be_kind_of(User) @@ -42,7 +42,7 @@ end it "assigns but does not save associations when using parent strategy" do - FactoryGirl.use_parent_strategy = true + FactoryBot.use_parent_strategy = true expect(subject.user).to be_kind_of(User) expect(subject.user).to be_new_record @@ -50,7 +50,7 @@ end describe "a built instance with strategy: :create" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -59,7 +59,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -79,12 +79,12 @@ end describe "calling `build` with a block" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :company end end diff --git a/spec/acceptance/build_stubbed_spec.rb b/spec/acceptance/build_stubbed_spec.rb index 173e5aab1..03f1f9c06 100644 --- a/spec/acceptance/build_stubbed_spec.rb +++ b/spec/acceptance/build_stubbed_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a generated stub instance" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -13,7 +13,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -89,12 +89,12 @@ end describe "calling `build_stubbed` with a block" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :company end end @@ -117,13 +117,13 @@ end describe "defaulting `created_at`" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('ThingWithTimestamp', created_at: :datetime) define_model('ThingWithoutTimestamp') - FactoryGirl.define do + FactoryBot.define do factory :thing_with_timestamp factory :thing_without_timestamp end @@ -159,13 +159,13 @@ end describe "defaulting `updated_at`" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model("ThingWithTimestamp", updated_at: :datetime) define_model("ThingWithoutTimestamp") - FactoryGirl.define do + FactoryBot.define do factory :thing_with_timestamp factory :thing_without_timestamp end @@ -197,12 +197,12 @@ before do define_model('Post') - FactoryGirl.define do + FactoryBot.define do factory :post end end it 'allows overriding id' do - expect(FactoryGirl.build_stubbed(:post, id: 12).id).to eq 12 + expect(FactoryBot.build_stubbed(:post, id: 12).id).to eq 12 end end diff --git a/spec/acceptance/callbacks_spec.rb b/spec/acceptance/callbacks_spec.rb index 13e4c3a4d..5448f9d5b 100644 --- a/spec/acceptance/callbacks_spec.rb +++ b/spec/acceptance/callbacks_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", first_name: :string, last_name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user_with_callbacks, class: :user do after(:stub) { |user| user.first_name = 'Stubby' } after(:build) { |user| user.first_name = 'Buildy' } @@ -19,29 +19,29 @@ end it "runs the after(:stub) callback when stubbing" do - user = FactoryGirl.build_stubbed(:user_with_callbacks) + user = FactoryBot.build_stubbed(:user_with_callbacks) expect(user.first_name).to eq 'Stubby' end it "runs the after(:build) callback when building" do - user = FactoryGirl.build(:user_with_callbacks) + user = FactoryBot.build(:user_with_callbacks) expect(user.first_name).to eq 'Buildy' end it "runs both the after(:build) and after(:create) callbacks when creating" do - user = FactoryGirl.create(:user_with_callbacks) + user = FactoryBot.create(:user_with_callbacks) expect(user.first_name).to eq 'Buildy' expect(user.last_name).to eq 'Createy' end it "runs both the after(:stub) callback on the factory and the inherited after(:stub) callback" do - user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks) + user = FactoryBot.build_stubbed(:user_with_inherited_callbacks) expect(user.first_name).to eq 'Stubby' expect(user.last_name).to eq 'Double-Stubby' end it "runs child callback after parent callback" do - user = FactoryGirl.build(:user_with_inherited_callbacks) + user = FactoryBot.build(:user_with_inherited_callbacks) expect(user.first_name).to eq 'Child-Buildy' end end @@ -58,7 +58,7 @@ def confirm! end end - FactoryGirl.define do + FactoryBot.define do factory :user do after :build, &:confirm! end @@ -66,16 +66,16 @@ def confirm! end it 'runs the callback correctly' do - user = FactoryGirl.build(:user) + user = FactoryBot.build(:user) expect(user).to be_confirmed end end -describe "callbacks using syntax methods without referencing FactoryGirl explicitly" do +describe "callbacks using syntax methods without referencing FactoryBot explicitly" do before do define_model("User", first_number: :integer, last_number: :integer) - FactoryGirl.define do + FactoryBot.define do sequence(:sequence_1) sequence(:sequence_2) sequence(:sequence_3) @@ -89,16 +89,16 @@ def confirm! end it "works when the callback has no variables" do - FactoryGirl.build_stubbed(:user) - expect(FactoryGirl.generate(:sequence_3)).to eq 2 + FactoryBot.build_stubbed(:user) + expect(FactoryBot.generate(:sequence_3)).to eq 2 end it "works when the callback has one variable" do - expect(FactoryGirl.build(:user).first_number).to eq 1 + expect(FactoryBot.build(:user).first_number).to eq 1 end it "works when the callback has two variables" do - expect(FactoryGirl.create(:user).last_number).to eq 1 + expect(FactoryBot.create(:user).last_number).to eq 1 end end @@ -140,11 +140,11 @@ def name end end - FactoryGirl.register_strategy(:custom_before, custom_before) - FactoryGirl.register_strategy(:custom_after, custom_after) - FactoryGirl.register_strategy(:totally_custom, totally_custom) + FactoryBot.register_strategy(:custom_before, custom_before) + FactoryBot.register_strategy(:custom_after, custom_after) + FactoryBot.register_strategy(:totally_custom, totally_custom) - FactoryGirl.define do + FactoryBot.define do factory :user do first_name "John" last_name "Doe" @@ -160,18 +160,18 @@ def name end it "runs a custom before callback when the proper strategy executes" do - expect(FactoryGirl.build(:user).name).to eq "John Doe" - expect(FactoryGirl.custom_before(:user).name).to eq "Overridden First Doe" + expect(FactoryBot.build(:user).name).to eq "John Doe" + expect(FactoryBot.custom_before(:user).name).to eq "Overridden First Doe" end it "runs a custom after callback when the proper strategy executes" do - expect(FactoryGirl.build(:user).name).to eq "John Doe" - expect(FactoryGirl.custom_after(:user).name).to eq "John Overridden Last" + expect(FactoryBot.build(:user).name).to eq "John Doe" + expect(FactoryBot.custom_after(:user).name).to eq "John Overridden Last" end it "runs a custom callback without prepending before or after when the proper strategy executes" do - expect(FactoryGirl.build(:user).name).to eq "John Doe" - expect(FactoryGirl.totally_custom(:user).name).to eq "Totally Custom" + expect(FactoryBot.build(:user).name).to eq "John Doe" + expect(FactoryBot.totally_custom(:user).name).to eq "Totally Custom" end end @@ -179,7 +179,7 @@ def name before do define_model('User', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do callback(:before_create, :after_stub) do |instance| instance.name = instance.name.upcase @@ -189,26 +189,26 @@ def name end it 'binds the callback to creation' do - expect(FactoryGirl.create(:user, name: 'John Doe').name).to eq 'JOHN DOE' + expect(FactoryBot.create(:user, name: 'John Doe').name).to eq 'JOHN DOE' end it 'does not bind the callback to building' do - expect(FactoryGirl.build(:user, name: 'John Doe').name).to eq 'John Doe' + expect(FactoryBot.build(:user, name: 'John Doe').name).to eq 'John Doe' end it 'binds the callback to stubbing' do - expect(FactoryGirl.build_stubbed(:user, name: 'John Doe').name).to eq 'JOHN DOE' + expect(FactoryBot.build_stubbed(:user, name: 'John Doe').name).to eq 'JOHN DOE' end end describe 'global callbacks' do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User', name: :string) define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do after :build do |object| object.name = case object.class.to_s when 'User' then 'John Doe' diff --git a/spec/acceptance/create_list_spec.rb b/spec/acceptance/create_list_spec.rb index 362436f04..5b135da8f 100644 --- a/spec/acceptance/create_list_spec.rb +++ b/spec/acceptance/create_list_spec.rb @@ -4,7 +4,7 @@ before do define_model('Post', title: :string, position: :integer) - FactoryGirl.define do + FactoryBot.define do factory(:post) do |post| post.title "Through the Looking Glass" post.position { rand(10**4) } @@ -13,7 +13,7 @@ end context "without default attributes" do - subject { FactoryGirl.create_list(:post, 20) } + subject { FactoryBot.create_list(:post, 20) } its(:length) { should eq 20 } @@ -31,7 +31,7 @@ end context "with default attributes" do - subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") } + subject { FactoryBot.create_list(:post, 20, title: "The Hunting of the Snark") } it "overrides the default values" do subject.each do |record| @@ -42,7 +42,7 @@ context "with a block" do subject do - FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post| + FactoryBot.create_list(:post, 20, title: "The Listing of the Block") do |post| post.position = post.id end end @@ -55,7 +55,7 @@ end context "without the count" do - subject { FactoryGirl.create_list(:post, title: "The Hunting of the Bear") } + subject { FactoryBot.create_list(:post, title: "The Hunting of the Bear") } it "raise ArgumentError with the proper error message" do expect { subject }.to raise_error(ArgumentError, /count missing/) @@ -73,7 +73,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :post do title "Through the Looking Glass" user @@ -88,7 +88,7 @@ end after(:create) do |user, evaluator| - FactoryGirl.create_list(:post, evaluator.posts_count, user: user) + FactoryBot.create_list(:post, evaluator.posts_count, user: user) end end end @@ -96,10 +96,10 @@ end it "generates the correct number of posts" do - expect(FactoryGirl.create(:user_with_posts).posts.length).to eq 5 + expect(FactoryBot.create(:user_with_posts).posts.length).to eq 5 end it "allows the number of posts to be modified" do - expect(FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length).to eq 2 + expect(FactoryBot.create(:user_with_posts, posts_count: 2).posts.length).to eq 2 end end diff --git a/spec/acceptance/create_pair_spec.rb b/spec/acceptance/create_pair_spec.rb index dbe0f85d3..3eab1e641 100644 --- a/spec/acceptance/create_pair_spec.rb +++ b/spec/acceptance/create_pair_spec.rb @@ -4,7 +4,7 @@ before do define_model('Post', title: :string, position: :integer) - FactoryGirl.define do + FactoryBot.define do factory(:post) do |post| post.title "Through the Looking Glass" post.position { rand(10**4) } @@ -13,7 +13,7 @@ end context "without default attributes" do - subject { FactoryGirl.create_pair(:post) } + subject { FactoryBot.create_pair(:post) } its(:length) { should eq 2 } diff --git a/spec/acceptance/create_spec.rb b/spec/acceptance/create_spec.rb index 86ff967c3..ddb26ae66 100644 --- a/spec/acceptance/create_spec.rb +++ b/spec/acceptance/create_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a created instance" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -10,7 +10,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -30,7 +30,7 @@ end describe "a created instance, specifying strategy: :build" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -39,7 +39,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -57,7 +57,7 @@ end describe "a custom create" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_class('User') do @@ -74,7 +74,7 @@ def persisted? end end - FactoryGirl.define do + FactoryBot.define do factory :user do to_create do |user| user.persist @@ -84,7 +84,7 @@ def persisted? end it "uses the custom create block instead of save" do - expect(FactoryGirl.create(:user)).to be_persisted + expect(FactoryBot.create(:user)).to be_persisted end end @@ -94,7 +94,7 @@ def persisted? attr_accessor :name end - FactoryGirl.define do + FactoryBot.define do factory :user do transient { creation_name "evaluator" } @@ -106,17 +106,17 @@ def persisted? end it "passes the evaluator to the custom create block" do - expect(FactoryGirl.create(:user).name).to eq "evaluator" + expect(FactoryBot.create(:user).name).to eq "evaluator" end end describe "calling `create` with a block" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :company end end diff --git a/spec/acceptance/define_child_before_parent_spec.rb b/spec/acceptance/define_child_before_parent_spec.rb index 6b7cdc31c..341a73a0d 100644 --- a/spec/acceptance/define_child_before_parent_spec.rb +++ b/spec/acceptance/define_child_before_parent_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string) - FactoryGirl.define do + FactoryBot.define do factory :admin, parent: :user do admin true end @@ -16,6 +16,6 @@ end it "creates admin factories correctly" do - expect(FactoryGirl.create(:admin)).to be_admin + expect(FactoryBot.create(:admin)).to be_admin end end diff --git a/spec/acceptance/defining_methods_inside_a_factory_spec.rb b/spec/acceptance/defining_methods_inside_a_factory_spec.rb index 46b369d8a..026692aaf 100644 --- a/spec/acceptance/defining_methods_inside_a_factory_spec.rb +++ b/spec/acceptance/defining_methods_inside_a_factory_spec.rb @@ -1,17 +1,17 @@ require 'spec_helper' -describe 'defining methods inside FactoryGirl' do +describe 'defining methods inside FactoryBot' do it 'raises with a meaningful message' do define_model('User') expect do - FactoryGirl.define do + FactoryBot.define do factory :user do def generate_name 'John Doe' end end end - end.to raise_error FactoryGirl::MethodDefinitionError, /Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/ + end.to raise_error FactoryBot::MethodDefinitionError, /Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/ end end diff --git a/spec/acceptance/definition_camel_string_spec.rb b/spec/acceptance/definition_camel_string_spec.rb index c33039524..ddfd1def8 100644 --- a/spec/acceptance/definition_camel_string_spec.rb +++ b/spec/acceptance/definition_camel_string_spec.rb @@ -4,12 +4,12 @@ before do define_model("UserModel") - FactoryGirl.define do + FactoryBot.define do factory 'UserModel', class: UserModel end end it "registers the UserModel factory" do - expect(FactoryGirl.factory_by_name('UserModel')).to be_a(FactoryGirl::Factory) + expect(FactoryBot.factory_by_name('UserModel')).to be_a(FactoryBot::Factory) end end diff --git a/spec/acceptance/definition_spec.rb b/spec/acceptance/definition_spec.rb index 04b4836e3..a6b201e6b 100644 --- a/spec/acceptance/definition_spec.rb +++ b/spec/acceptance/definition_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", admin: :boolean) - FactoryGirl.define do + FactoryBot.define do factory :user factory :admin, class: User do @@ -13,7 +13,7 @@ end end - subject { FactoryGirl.create(:admin) } + subject { FactoryBot.create(:admin) } it { should be_kind_of(User) } it { should be_admin } @@ -23,7 +23,7 @@ before do define_model("User", password: :string, password_confirmation: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do password "foo" password_confirmation &:password @@ -32,21 +32,21 @@ end it "assigns values correctly" do - user = FactoryGirl.build(:user) + user = FactoryBot.build(:user) expect(user.password).to eq "foo" expect(user.password_confirmation).to eq "foo" end it "assigns value with override correctly" do - user = FactoryGirl.build(:user, password: "bar") + user = FactoryBot.build(:user, password: "bar") expect(user.password).to eq "bar" expect(user.password_confirmation).to eq "bar" end it "assigns overridden value correctly" do - user = FactoryGirl.build(:user, password_confirmation: "bar") + user = FactoryBot.build(:user, password_confirmation: "bar") expect(user.password).to eq "foo" expect(user.password_confirmation).to eq "bar" diff --git a/spec/acceptance/definition_without_block_spec.rb b/spec/acceptance/definition_without_block_spec.rb index 4e21e56b4..437471131 100644 --- a/spec/acceptance/definition_without_block_spec.rb +++ b/spec/acceptance/definition_without_block_spec.rb @@ -4,12 +4,12 @@ before do define_model("User") - FactoryGirl.define do + FactoryBot.define do factory :user end end it "registers the user factory" do - expect(FactoryGirl.factory_by_name(:user)).to be_a(FactoryGirl::Factory) + expect(FactoryBot.factory_by_name(:user)).to be_a(FactoryBot::Factory) end end diff --git a/spec/acceptance/global_initialize_with_spec.rb b/spec/acceptance/global_initialize_with_spec.rb index 1bd352f45..f3e957c44 100644 --- a/spec/acceptance/global_initialize_with_spec.rb +++ b/spec/acceptance/global_initialize_with_spec.rb @@ -18,7 +18,7 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do initialize_with { new("initialize_with") } trait :with_initialize_with do @@ -44,26 +44,26 @@ def initialize(name) end it 'handles base initialize_with' do - expect(FactoryGirl.build(:user).name).to eq 'initialize_with' - expect(FactoryGirl.build(:post).name).to eq 'initialize_with' + expect(FactoryBot.build(:user).name).to eq 'initialize_with' + expect(FactoryBot.build(:post).name).to eq 'initialize_with' end it 'handles child initialize_with' do - expect(FactoryGirl.build(:child_user).name).to eq 'initialize_with' - expect(FactoryGirl.build(:child_post).name).to eq 'initialize_with' + expect(FactoryBot.build(:child_user).name).to eq 'initialize_with' + expect(FactoryBot.build(:child_post).name).to eq 'initialize_with' end it 'handles child initialize_with with trait' do - expect(FactoryGirl.build(:child_user_with_trait).name).to eq 'trait initialize_with' - expect(FactoryGirl.build(:child_post_with_trait).name).to eq 'trait initialize_with' + expect(FactoryBot.build(:child_user_with_trait).name).to eq 'trait initialize_with' + expect(FactoryBot.build(:child_post_with_trait).name).to eq 'trait initialize_with' end it 'handles inline trait override' do - expect(FactoryGirl.build(:child_user, :with_initialize_with).name).to eq 'trait initialize_with' - expect(FactoryGirl.build(:child_post, :with_initialize_with).name).to eq 'trait initialize_with' + expect(FactoryBot.build(:child_user, :with_initialize_with).name).to eq 'trait initialize_with' + expect(FactoryBot.build(:child_post, :with_initialize_with).name).to eq 'trait initialize_with' end - it 'uses initialize_with globally across FactoryGirl.define' do + it 'uses initialize_with globally across FactoryBot.define' do define_class('Company') do attr_reader :name @@ -72,11 +72,11 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do factory :company end - expect(FactoryGirl.build(:company).name).to eq 'initialize_with' - expect(FactoryGirl.build(:company, :with_initialize_with).name).to eq 'trait initialize_with' + expect(FactoryBot.build(:company).name).to eq 'initialize_with' + expect(FactoryBot.build(:company, :with_initialize_with).name).to eq 'trait initialize_with' end end diff --git a/spec/acceptance/global_to_create_spec.rb b/spec/acceptance/global_to_create_spec.rb index cf71d845f..102b8da7b 100644 --- a/spec/acceptance/global_to_create_spec.rb +++ b/spec/acceptance/global_to_create_spec.rb @@ -5,7 +5,7 @@ define_model('User', name: :string) define_model('Post', name: :string) - FactoryGirl.define do + FactoryBot.define do to_create { |instance| instance.name = 'persisted!' } trait :override_to_create do @@ -35,34 +35,34 @@ end it 'handles base to_create' do - expect(FactoryGirl.create(:user).name).to eq 'persisted!' - expect(FactoryGirl.create(:post).name).to eq 'persisted!' + expect(FactoryBot.create(:user).name).to eq 'persisted!' + expect(FactoryBot.create(:post).name).to eq 'persisted!' end it 'handles child to_create' do - expect(FactoryGirl.create(:child_user).name).to eq 'persisted!' - expect(FactoryGirl.create(:child_post).name).to eq 'persisted!' + expect(FactoryBot.create(:child_user).name).to eq 'persisted!' + expect(FactoryBot.create(:child_post).name).to eq 'persisted!' end it 'handles child to_create with trait' do - expect(FactoryGirl.create(:child_user_with_trait).name).to eq 'override' - expect(FactoryGirl.create(:child_post_with_trait).name).to eq 'override' + expect(FactoryBot.create(:child_user_with_trait).name).to eq 'override' + expect(FactoryBot.create(:child_post_with_trait).name).to eq 'override' end it 'handles inline trait override' do - expect(FactoryGirl.create(:child_user, :override_to_create).name).to eq 'override' - expect(FactoryGirl.create(:child_post, :override_to_create).name).to eq 'override' + expect(FactoryBot.create(:child_user, :override_to_create).name).to eq 'override' + expect(FactoryBot.create(:child_post, :override_to_create).name).to eq 'override' end - it 'uses to_create globally across FactoryGirl.define' do + it 'uses to_create globally across FactoryBot.define' do define_model('Company', name: :string) - FactoryGirl.define do + FactoryBot.define do factory :company end - expect(FactoryGirl.create(:company).name).to eq 'persisted!' - expect(FactoryGirl.create(:company, :override_to_create).name).to eq 'override' + expect(FactoryBot.create(:company).name).to eq 'persisted!' + expect(FactoryBot.create(:company, :override_to_create).name).to eq 'override' end end @@ -71,7 +71,7 @@ define_model('User', name: :string) define_model('Post', name: :string) - FactoryGirl.define do + FactoryBot.define do skip_create trait :override_to_create do @@ -101,22 +101,22 @@ end it 'does not persist any record' do - expect(FactoryGirl.create(:user)).to be_new_record - expect(FactoryGirl.create(:post)).to be_new_record + expect(FactoryBot.create(:user)).to be_new_record + expect(FactoryBot.create(:post)).to be_new_record end it 'does not persist child records' do - expect(FactoryGirl.create(:child_user)).to be_new_record - expect(FactoryGirl.create(:child_post)).to be_new_record + expect(FactoryBot.create(:child_user)).to be_new_record + expect(FactoryBot.create(:child_post)).to be_new_record end it 'honors overridden to_create' do - expect(FactoryGirl.create(:child_user_with_trait).name).to eq 'override' - expect(FactoryGirl.create(:child_post_with_trait).name).to eq 'override' + expect(FactoryBot.create(:child_user_with_trait).name).to eq 'override' + expect(FactoryBot.create(:child_post_with_trait).name).to eq 'override' end it 'honors inline trait to_create' do - expect(FactoryGirl.create(:child_user, :override_to_create).name).to eq 'override' - expect(FactoryGirl.create(:child_post, :override_to_create).name).to eq 'override' + expect(FactoryBot.create(:child_user, :override_to_create).name).to eq 'override' + expect(FactoryBot.create(:child_post, :override_to_create).name).to eq 'override' end end diff --git a/spec/acceptance/initialize_with_spec.rb b/spec/acceptance/initialize_with_spec.rb index ab106d095..684e64887 100644 --- a/spec/acceptance/initialize_with_spec.rb +++ b/spec/acceptance/initialize_with_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" describe "initialize_with with non-FG attributes" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model("User", name: :string, age: :integer) do @@ -10,7 +10,7 @@ def self.construct(name, age) end end - FactoryGirl.define do + FactoryBot.define do factory :user do initialize_with { User.construct("John Doe", 21) } end @@ -23,7 +23,7 @@ def self.construct(name, age) end describe "initialize_with with FG attributes that are transient" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model("User", name: :string) do @@ -32,7 +32,7 @@ def self.construct(name) end end - FactoryGirl.define do + FactoryBot.define do factory :user do transient do name { "Handsome Chap" } @@ -48,7 +48,7 @@ def self.construct(name) end describe "initialize_with non-ORM-backed objects" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_class("ReportGenerator") do @@ -60,7 +60,7 @@ def initialize(name, data) end end - FactoryGirl.define do + FactoryBot.define do sequence(:random_data) { 5.times.map { Kernel.rand(200) } } factory :report_generator do @@ -68,7 +68,7 @@ def initialize(name, data) name "My Awesome Report" end - initialize_with { ReportGenerator.new(name, FactoryGirl.generate(:random_data)) } + initialize_with { ReportGenerator.new(name, FactoryBot.generate(:random_data)) } end end end @@ -92,7 +92,7 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do factory :awesome do transient do name "Great" @@ -114,11 +114,11 @@ def initialize(name) end it "uses the parent's constructor when the child factory doesn't assign it" do - expect(FactoryGirl.build(:sub_awesome).name).to eq "Sub" + expect(FactoryBot.build(:sub_awesome).name).to eq "Sub" end it "allows child factories to override initialize_with" do - expect(FactoryGirl.build(:super_awesome).name).to eq "Super" + expect(FactoryBot.build(:super_awesome).name).to eq "Super" end end @@ -132,7 +132,7 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do factory :awesome do transient do name "Great" @@ -144,7 +144,7 @@ def initialize(name) end it "instantiates the correct object" do - expect(FactoryGirl.build(:awesome, name: "Awesome name").name).to eq "Awesome name" + expect(FactoryBot.build(:awesome, name: "Awesome name").name).to eq "Awesome name" end end @@ -159,7 +159,7 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do sequence(:email) { |n| "person#{n}@example.com" } factory :user do @@ -173,7 +173,7 @@ def initialize(name) end it "instantiates the correct object" do - built_user = FactoryGirl.build(:user) + built_user = FactoryBot.build(:user) expect(built_user.name).to eq "person1" expect(built_user.email).to eq "person1@example.com" end @@ -191,7 +191,7 @@ def initialize(attributes = {}) end end - FactoryGirl.define do + FactoryBot.define do sequence(:email) { |n| "person#{n}@example.com" } factory :user do @@ -209,7 +209,7 @@ def initialize(attributes = {}) end it "assigns attributes correctly" do - user_with_attributes = FactoryGirl.build(:user) + user_with_attributes = FactoryBot.build(:user) expect(user_with_attributes.email).to eq "person1@example.com" expect(user_with_attributes.name).to eq "person1" expect(user_with_attributes.ignored).to be_nil @@ -226,12 +226,12 @@ def initialize(&block) end end - FactoryGirl.define do + FactoryBot.define do factory :awesome do initialize_with { new { "Output" } } end end - expect(FactoryGirl.build(:awesome).output).to eq "Output" + expect(FactoryBot.build(:awesome).output).to eq "Output" end end diff --git a/spec/acceptance/keyed_by_class_spec.rb b/spec/acceptance/keyed_by_class_spec.rb index d9cd4b054..df10f8d54 100644 --- a/spec/acceptance/keyed_by_class_spec.rb +++ b/spec/acceptance/keyed_by_class_spec.rb @@ -6,7 +6,7 @@ attr_accessor :name, :email end - FactoryGirl.define do + FactoryBot.define do factory :user do name "John Doe" sequence(:email) { |n| "person#{n}@example.com" } @@ -16,7 +16,7 @@ context "when deprecated class lookup if allowed", :silence_deprecation do it "allows interaction based on class name" do - user = FactoryGirl.create User, email: "person@example.com" + user = FactoryBot.create User, email: "person@example.com" expect(user.email).to eq "person@example.com" expect(user.name).to eq "John Doe" end @@ -24,8 +24,8 @@ context "when class lookup is disallowed" do it "doesn't find the factory" do - FactoryGirl.allow_class_lookup = false - expect { FactoryGirl.create(User) }.to( + FactoryBot.allow_class_lookup = false + expect { FactoryBot.create(User) }.to( raise_error(ArgumentError, "Factory not registered: User"), ) end diff --git a/spec/acceptance/lint_spec.rb b/spec/acceptance/lint_spec.rb index 061fb71d0..642a2f52b 100644 --- a/spec/acceptance/lint_spec.rb +++ b/spec/acceptance/lint_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'FactoryGirl.lint' do +describe 'FactoryBot.lint' do it 'raises when a factory is invalid' do define_model 'User', name: :string do validates :name, presence: true @@ -8,7 +8,7 @@ define_model 'AlwaysValid' - FactoryGirl.define do + FactoryBot.define do factory :user do factory :admin_user end @@ -24,8 +24,8 @@ ERROR_MESSAGE expect do - FactoryGirl.lint - end.to raise_error FactoryGirl::InvalidFactoryError, error_message + FactoryBot.lint + end.to raise_error FactoryBot::InvalidFactoryError, error_message end it 'does not raise when all factories are valid' do @@ -33,13 +33,13 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name 'assigned' end end - expect { FactoryGirl.lint }.not_to raise_error + expect { FactoryBot.lint }.not_to raise_error end it 'allows for selective linting' do @@ -49,17 +49,17 @@ define_model 'ValidThing', name: :string - FactoryGirl.define do + FactoryBot.define do factory :valid_thing factory :invalid_thing end expect do - only_valid_factories = FactoryGirl.factories.reject do |factory| + only_valid_factories = FactoryBot.factories.reject do |factory| factory.name =~ /invalid/ end - FactoryGirl.lint only_valid_factories + FactoryBot.lint only_valid_factories end.not_to raise_error end @@ -68,13 +68,13 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name "assigned" end end - FactoryGirl.lint FactoryGirl.factories, validate_traits: true + FactoryBot.lint FactoryBot.factories, validate_traits: true end it "allows for additional options without explicit factories" do @@ -82,13 +82,13 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name "assigned" end end - FactoryGirl.lint validate_traits: true + FactoryBot.lint validate_traits: true end describe "trait validation" do @@ -98,7 +98,7 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name "Yep" trait :unnamed do @@ -114,8 +114,8 @@ ERROR_MESSAGE expect do - FactoryGirl.lint traits: true - end.to raise_error FactoryGirl::InvalidFactoryError, error_message + FactoryBot.lint traits: true + end.to raise_error FactoryBot::InvalidFactoryError, error_message end it "does not raise if a trait produces a valid object" do @@ -123,7 +123,7 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name "Yep" trait :renamed do @@ -133,7 +133,7 @@ end expect do - FactoryGirl.lint traits: true + FactoryBot.lint traits: true end.not_to raise_error end end @@ -144,7 +144,7 @@ validates :name, presence: true end - FactoryGirl.define do + FactoryBot.define do factory :user do name "Yep" trait :unnamed do @@ -154,8 +154,8 @@ end expect do - FactoryGirl.lint traits: false - FactoryGirl.lint + FactoryBot.lint traits: false + FactoryBot.lint end.not_to raise_error end end @@ -171,14 +171,14 @@ def save! end end - FactoryGirl.define do + FactoryBot.define do factory :user do name "Barbara" end end expect do - FactoryGirl.lint strategy: :build + FactoryBot.lint strategy: :build end.not_to raise_error end @@ -191,7 +191,7 @@ def save! end end - FactoryGirl.define do + FactoryBot.define do factory :user do name "Barbara" @@ -202,7 +202,7 @@ def save! end expect do - FactoryGirl.lint traits: true, strategy: :build + FactoryBot.lint traits: true, strategy: :build end.not_to raise_error end end diff --git a/spec/acceptance/modify_factories_spec.rb b/spec/acceptance/modify_factories_spec.rb index 8c26f6b9e..87222e729 100644 --- a/spec/acceptance/modify_factories_spec.rb +++ b/spec/acceptance/modify_factories_spec.rb @@ -1,12 +1,12 @@ require "spec_helper" describe "modifying factories" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User', name: :string, admin: :boolean, email: :string, login: :string) - FactoryGirl.define do + FactoryBot.define do sequence(:email) { |n| "user#{n}@example.com" } factory :user do @@ -25,7 +25,7 @@ context "simple modification" do before do - FactoryGirl.modify do + FactoryBot.modify do factory :user do name "Great User" end @@ -38,12 +38,12 @@ it "doesn't allow the factory to be subsequently defined" do expect do - FactoryGirl.define { factory :user } - end.to raise_error(FactoryGirl::DuplicateDefinitionError, "Factory already registered: user") + FactoryBot.define { factory :user } + end.to raise_error(FactoryBot::DuplicateDefinitionError, "Factory already registered: user") end it "does allow the factory to be subsequently modified" do - FactoryGirl.modify do + FactoryBot.modify do factory :user do name "Overridden again!" end @@ -55,7 +55,7 @@ context "adding callbacks" do before do - FactoryGirl.modify do + FactoryBot.modify do factory :user do name "Great User" after(:create) do |user| @@ -74,13 +74,13 @@ context "reusing traits" do before do - FactoryGirl.define do + FactoryBot.define do trait :rockstar do name "Johnny Rockstar!!!" end end - FactoryGirl.modify do + FactoryBot.modify do factory :user do rockstar email { "#{name}@example.com" } @@ -97,7 +97,7 @@ context "redefining attributes" do before do - FactoryGirl.modify do + FactoryBot.modify do factory :user do email { "#{name}-modified@example.com" } name "Great User" @@ -156,7 +156,7 @@ end it "doesn't overwrite already defined child's attributes" do - FactoryGirl.modify do + FactoryBot.modify do factory :user do admin false end @@ -165,7 +165,7 @@ end it "allows for overriding child classes" do - FactoryGirl.modify do + FactoryBot.modify do factory :admin do admin false end @@ -176,7 +176,7 @@ it "raises an exception if the factory was not defined before" do expect { - FactoryGirl.modify do + FactoryBot.modify do factory :unknown_factory end }.to raise_error(ArgumentError) diff --git a/spec/acceptance/modify_inherited_spec.rb b/spec/acceptance/modify_inherited_spec.rb index f2117072d..1f1126013 100644 --- a/spec/acceptance/modify_inherited_spec.rb +++ b/spec/acceptance/modify_inherited_spec.rb @@ -3,7 +3,7 @@ describe "modifying inherited factories with traits" do before do define_model('User', gender: :string, admin: :boolean, age: :integer) - FactoryGirl.define do + FactoryBot.define do factory :user do trait(:female) { gender "Female" } trait(:male) { gender "Male" } @@ -29,24 +29,24 @@ end it "returns the correct value for overridden attributes from traits" do - expect(FactoryGirl.build(:male_user).gender).to eq "Male" + expect(FactoryBot.build(:male_user).gender).to eq "Male" end it "returns the correct value for overridden attributes from traits defining multiple attributes" do - expect(FactoryGirl.build(:female_user).gender).to eq "Female" - expect(FactoryGirl.build(:female_user).age).to eq 25 - expect(FactoryGirl.build(:female_user).admin).to eq true + expect(FactoryBot.build(:female_user).gender).to eq "Female" + expect(FactoryBot.build(:female_user).age).to eq 25 + expect(FactoryBot.build(:female_user).admin).to eq true end it "allows modification of attributes created via traits" do - FactoryGirl.modify do + FactoryBot.modify do factory :male_user do age 20 end end - expect(FactoryGirl.build(:male_user).gender).to eq "Male" - expect(FactoryGirl.build(:male_user).age).to eq 20 - expect(FactoryGirl.build(:male_user).admin).to eq true + expect(FactoryBot.build(:male_user).gender).to eq "Male" + expect(FactoryBot.build(:male_user).age).to eq 20 + expect(FactoryBot.build(:male_user).admin).to eq true end end diff --git a/spec/acceptance/nested_attributes_spec.rb b/spec/acceptance/nested_attributes_spec.rb index 44dea1069..69e61106b 100644 --- a/spec/acceptance/nested_attributes_spec.rb +++ b/spec/acceptance/nested_attributes_spec.rb @@ -11,9 +11,9 @@ belongs_to :post end - FactoryGirl.define do + FactoryBot.define do factory :post do - comments_attributes { [FactoryGirl.attributes_for(:comment), FactoryGirl.attributes_for(:comment)] } + comments_attributes { [FactoryBot.attributes_for(:comment), FactoryBot.attributes_for(:comment)] } end factory :comment do @@ -23,10 +23,10 @@ end it "assigns the correct amount of comments" do - expect(FactoryGirl.create(:post).comments.count).to eq 2 + expect(FactoryBot.create(:post).comments.count).to eq 2 end it "assigns the correct amount of comments when overridden" do - expect(FactoryGirl.create(:post, :comments_attributes => [FactoryGirl.attributes_for(:comment)]).comments.count).to eq 1 + expect(FactoryBot.create(:post, :comments_attributes => [FactoryBot.attributes_for(:comment)]).comments.count).to eq 1 end end diff --git a/spec/acceptance/overrides_spec.rb b/spec/acceptance/overrides_spec.rb index 288aba57c..9c0e8e3e0 100644 --- a/spec/acceptance/overrides_spec.rb +++ b/spec/acceptance/overrides_spec.rb @@ -14,7 +14,7 @@ def secure=(value) end end - FactoryGirl.define do + FactoryBot.define do factory :user do factory :admin do admin true @@ -28,14 +28,14 @@ def secure=(value) end end - let(:admin) { FactoryGirl.create(:admin) } + let(:admin) { FactoryBot.create(:admin) } let(:post_attributes) do { secure: false } end let(:non_admin_post_attributes) do - post_attributes[:user] = FactoryGirl.create(:user) + post_attributes[:user] = FactoryBot.create(:user) post_attributes end @@ -45,17 +45,17 @@ def secure=(value) end context "with an admin posting" do - subject { FactoryGirl.create(:post, admin_post_attributes) } + subject { FactoryBot.create(:post, admin_post_attributes) } its(:secure) { should eq false } end context "with a non-admin posting" do - subject { FactoryGirl.create(:post, non_admin_post_attributes) } + subject { FactoryBot.create(:post, non_admin_post_attributes) } its(:secure) { should be_nil } end context "with no user posting" do - subject { FactoryGirl.create(:post, post_attributes) } + subject { FactoryBot.create(:post, post_attributes) } its(:secure) { should be_nil } end end diff --git a/spec/acceptance/parent_spec.rb b/spec/acceptance/parent_spec.rb index 4629f9d3d..94e9040bd 100644 --- a/spec/acceptance/parent_spec.rb +++ b/spec/acceptance/parent_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do name "John" email { "#{name.downcase}@example.com" } @@ -32,13 +32,13 @@ end describe "the parent class" do - subject { FactoryGirl.create(:user) } + 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 { FactoryGirl.create(:admin) } + subject { FactoryBot.create(:admin) } it { should be_kind_of(User) } it { should be_admin } its(:name) { should eq "admin" } @@ -47,7 +47,7 @@ end describe "the child class redefining parent's dynamic method" do - subject { FactoryGirl.create(:guest) } + subject { FactoryBot.create(:guest) } it { should_not be_admin } its(:name) { should eq "John" } its(:email) { should eql "John-guest@example.com" } @@ -55,12 +55,12 @@ end describe "the child class redefining parent's dynamic attribute with static attribute" do - subject { FactoryGirl.create(:no_email) } + subject { FactoryBot.create(:no_email) } its(:email) { should eq "" } end describe "the child class redefining parent's static attribute with dynamic attribute" do - subject { FactoryGirl.create(:bill) } + subject { FactoryBot.create(:bill) } its(:name) { should eq "Bill" } end end @@ -69,7 +69,7 @@ before do define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do name "Basic User" @@ -85,6 +85,6 @@ end it "honors :parent over the factory block nesting" do - expect(FactoryGirl.build(:uppercase_male_user).name).to eq "JOHN DOE" + expect(FactoryBot.build(:uppercase_male_user).name).to eq "JOHN DOE" end end diff --git a/spec/acceptance/private_attributes_spec.rb b/spec/acceptance/private_attributes_spec.rb index b2fae36e4..a551413f7 100644 --- a/spec/acceptance/private_attributes_spec.rb +++ b/spec/acceptance/private_attributes_spec.rb @@ -7,14 +7,14 @@ attr_accessor :foo end - FactoryGirl.define do + FactoryBot.define do factory :user do foo 123 end end expect do - FactoryGirl.build(:user) + FactoryBot.build(:user) end.to raise_error NoMethodError, /foo=/ end end diff --git a/spec/acceptance/register_strategies_spec.rb b/spec/acceptance/register_strategies_spec.rb index e833dd58b..11b26a420 100644 --- a/spec/acceptance/register_strategies_spec.rb +++ b/spec/acceptance/register_strategies_spec.rb @@ -22,7 +22,7 @@ def result(evaluation) include_context "registering custom strategies" before do - FactoryGirl.define do + FactoryBot.define do factory :named_object do name "Great" end @@ -30,42 +30,42 @@ def result(evaluation) end it "allows overriding default strategies" do - expect(FactoryGirl.build(:named_object).name).to eq "Great" - FactoryGirl.register_strategy(:build, custom_strategy) - expect(FactoryGirl.build(:named_object).name).to eq "Custom strategy" + expect(FactoryBot.build(:named_object).name).to eq "Great" + FactoryBot.register_strategy(:build, custom_strategy) + expect(FactoryBot.build(:named_object).name).to eq "Custom strategy" end it "allows adding additional strategies" do - FactoryGirl.register_strategy(:insert, custom_strategy) + FactoryBot.register_strategy(:insert, custom_strategy) - expect(FactoryGirl.build(:named_object).name).to eq "Great" - expect(FactoryGirl.insert(:named_object).name).to eq "Custom strategy" + expect(FactoryBot.build(:named_object).name).to eq "Great" + expect(FactoryBot.insert(:named_object).name).to eq "Custom strategy" end it "allows using the *_list method to build a list using a custom strategy" do - FactoryGirl.register_strategy(:insert, custom_strategy) + FactoryBot.register_strategy(:insert, custom_strategy) - inserted_items = FactoryGirl.insert_list(:named_object, 2) + inserted_items = FactoryBot.insert_list(:named_object, 2) expect(inserted_items.length).to eq 2 expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"] end it "allows using the *_pair method to build a list using a custom strategy" do - FactoryGirl.register_strategy(:insert, custom_strategy) + FactoryBot.register_strategy(:insert, custom_strategy) - inserted_items = FactoryGirl.insert_pair(:named_object) + inserted_items = FactoryBot.insert_pair(:named_object) expect(inserted_items.length).to eq 2 expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"] end end -describe "including FactoryGirl::Syntax::Methods when custom strategies have been declared" do - include FactoryGirl::Syntax::Methods +describe "including FactoryBot::Syntax::Methods when custom strategies have been declared" do + include FactoryBot::Syntax::Methods include_context "registering custom strategies" before do - FactoryGirl.define do + FactoryBot.define do factory :named_object do name "Great" end @@ -73,7 +73,7 @@ def result(evaluation) end it "allows adding additional strategies" do - FactoryGirl.register_strategy(:insert, custom_strategy) + FactoryBot.register_strategy(:insert, custom_strategy) expect(insert(:named_object).name).to eq "Custom strategy" end @@ -89,7 +89,7 @@ def result(evaluation) define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :post do user end @@ -101,23 +101,23 @@ def result(evaluation) end context "when the :use_parent_strategy config option has not been enabled" do - before { FactoryGirl.use_parent_strategy = nil } + before { FactoryBot.use_parent_strategy = nil } it "uses the overridden strategy on the association" do - FactoryGirl.register_strategy(:create, custom_strategy) + FactoryBot.register_strategy(:create, custom_strategy) - post = FactoryGirl.build(:post) + post = FactoryBot.build(:post) expect(post.user.name).to eq "Custom strategy" end end context "when the :use_parent_strategy config option has been enabled" do - before { FactoryGirl.use_parent_strategy = true } + before { FactoryBot.use_parent_strategy = true } it "uses the parent strategy on the association" do - FactoryGirl.register_strategy(:create, custom_strategy) + FactoryBot.register_strategy(:create, custom_strategy) - post = FactoryGirl.build(:post) + post = FactoryBot.build(:post) expect(post.user.name).to eq "John Doe" end end @@ -133,7 +133,7 @@ def result(evaluation) define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :post do association :user, strategy: :insert end @@ -145,8 +145,8 @@ def result(evaluation) end it "uses the overridden create strategy to create the association" do - FactoryGirl.register_strategy(:insert, custom_strategy) - post = FactoryGirl.build(:post) + FactoryBot.register_strategy(:insert, custom_strategy) + post = FactoryBot.build(:post) expect(post.user.name).to eq "Custom strategy" end end diff --git a/spec/acceptance/sequence_context_spec.rb b/spec/acceptance/sequence_context_spec.rb index 907bb63fb..95da07a7e 100644 --- a/spec/acceptance/sequence_context_spec.rb +++ b/spec/acceptance/sequence_context_spec.rb @@ -12,41 +12,41 @@ def awesome end it 'builds a sequence calling sprintf correctly' do - FactoryGirl.define do + FactoryBot.define do factory :sequence_with_sprintf, class: User do sequence(:id) { |n| sprintf("foo%d", n) } end end - expect(FactoryGirl.build(:sequence_with_sprintf).id).to eq 'foo1' + expect(FactoryBot.build(:sequence_with_sprintf).id).to eq 'foo1' end it 'invokes the correct method on the instance' do - FactoryGirl.define do + FactoryBot.define do factory :sequence_with_public_method, class: User do sequence(:id) { |n| public_method(:awesome).call } end end - expect(FactoryGirl.build(:sequence_with_public_method).id).to eq 'aw yeah' + expect(FactoryBot.build(:sequence_with_public_method).id).to eq 'aw yeah' end it 'invokes a method with no arguments on the instance' do - FactoryGirl.define do + FactoryBot.define do factory :sequence_with_frozen, class: User do sequence(:id) { |n| frozen? } end end - expect(FactoryGirl.build(:sequence_with_frozen).id).to be false + expect(FactoryBot.build(:sequence_with_frozen).id).to be false end it 'allows direct reference of a method in a sequence' do - FactoryGirl.define do + FactoryBot.define do factory :sequence_referencing_attribute_directly, class: User do sequence(:id) { |n| "#{awesome}#{n}" } end end - expect(FactoryGirl.build(:sequence_referencing_attribute_directly).id).to eq 'aw yeah1' + expect(FactoryBot.build(:sequence_referencing_attribute_directly).id).to eq 'aw yeah1' end end diff --git a/spec/acceptance/sequence_spec.rb b/spec/acceptance/sequence_spec.rb index d6716601b..799c126f9 100644 --- a/spec/acceptance/sequence_spec.rb +++ b/spec/acceptance/sequence_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' describe "sequences" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods it "generates several values in the correct format" do - FactoryGirl.define do + FactoryBot.define do sequence :email do |n| "somebody#{n}@example.com" end @@ -19,7 +19,7 @@ end it "generates sequential numbers if no block is given" do - FactoryGirl.define do + FactoryBot.define do sequence :order end @@ -32,7 +32,7 @@ end it "generates aliases for the sequence that reference the same block" do - FactoryGirl.define do + FactoryBot.define do sequence(:size, aliases: [:count, :length]) { |n| "called-#{n}" } end @@ -46,7 +46,7 @@ end it "generates aliases for the sequence that reference the same block and retains value" do - FactoryGirl.define do + FactoryBot.define do sequence(:size, "a", aliases: [:count, :length]) { |n| "called-#{n}" } end @@ -60,7 +60,7 @@ end it "generates few values of the sequence" do - FactoryGirl.define do + FactoryBot.define do sequence :email do |n| "somebody#{n}@example.com" end diff --git a/spec/acceptance/skip_create_spec.rb b/spec/acceptance/skip_create_spec.rb index 360e3ed99..f1f1c2271 100644 --- a/spec/acceptance/skip_create_spec.rb +++ b/spec/acceptance/skip_create_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", email: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do skip_create @@ -14,6 +14,6 @@ end it "doesn't execute anything when creating the instance" do - expect(FactoryGirl.create(:user)).not_to be_persisted + expect(FactoryBot.create(:user)).not_to be_persisted end end diff --git a/spec/acceptance/stub_spec.rb b/spec/acceptance/stub_spec.rb index 1a9508be4..bc7f269ee 100644 --- a/spec/acceptance/stub_spec.rb +++ b/spec/acceptance/stub_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe "a stubbed instance" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -10,7 +10,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do @@ -32,7 +32,7 @@ end describe "a stubbed instance overriding strategy" do - include FactoryGirl::Syntax::Methods + include FactoryBot::Syntax::Methods before do define_model('User') @@ -40,7 +40,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user factory :post do diff --git a/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb b/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb index 9face3093..638d56e2e 100644 --- a/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb +++ b/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb @@ -11,7 +11,7 @@ def generate end define_model("User", email: :string) - FactoryGirl.define do + FactoryBot.define do sequence(:email_address) { |n| "person-#{n}@example.com" } factory :user do @@ -26,26 +26,26 @@ def generate end it "can access syntax methods from dynamic attributes" do - expect(FactoryGirl.build(:user).email).to eq "person-1@example.com" - expect(FactoryGirl.attributes_for(:user)[:email]).to eq "person-2@example.com" + expect(FactoryBot.build(:user).email).to eq "person-1@example.com" + expect(FactoryBot.attributes_for(:user)[:email]).to eq "person-2@example.com" end it "can access syntax methods from dynamic attributes" do - expect(FactoryGirl.build(:post).user).to be_instance_of(User) + expect(FactoryBot.build(:post).user).to be_instance_of(User) end it "can access methods already existing on the class" do - expect(FactoryGirl.build(:post).title).to eq "generate result" - expect(FactoryGirl.attributes_for(:post)[:title]).to be_nil + expect(FactoryBot.build(:post).title).to eq "generate result" + expect(FactoryBot.attributes_for(:post)[:title]).to be_nil end it 'allows syntax methods to be used when the block has an arity of 1' do - FactoryGirl.define do + FactoryBot.define do factory :post_using_block_with_variable, parent: :post do user { |_| build(:user) } end end - expect(FactoryGirl.build(:post_using_block_with_variable).user).to be_instance_of(User) + expect(FactoryBot.build(:post_using_block_with_variable).user).to be_instance_of(User) end end diff --git a/spec/acceptance/traits_spec.rb b/spec/acceptance/traits_spec.rb index b39e9329f..98e831910 100644 --- a/spec/acceptance/traits_spec.rb +++ b/spec/acceptance/traits_spec.rb @@ -10,7 +10,7 @@ date_of_birth: :date, great: :string) - FactoryGirl.define do + FactoryBot.define do factory :user_without_admin_scoping, class: User do admin_trait end @@ -83,35 +83,35 @@ end context "the parent class" do - subject { FactoryGirl.create(:user) } + subject { FactoryBot.create(:user) } its(:name) { should eq "John" } its(:gender) { should be_nil } it { should_not be_admin } end context "the child class with one trait" do - subject { FactoryGirl.create(:admin) } + subject { FactoryBot.create(:admin) } its(:name) { should eq "John" } its(:gender) { should be_nil } it { should be_admin } end context "the other child class with one trait" do - subject { FactoryGirl.create(:female) } + subject { FactoryBot.create(:female) } its(:name) { should eq "Jane" } its(:gender) { should eq "Female" } it { should_not be_admin } end context "the child with multiple traits" do - subject { FactoryGirl.create(:female_admin) } + subject { FactoryBot.create(:female_admin) } its(:name) { should eq "Jane" } its(:gender) { should eq "Female" } it { should be_admin } end context "the child with multiple traits and overridden attributes" do - subject { FactoryGirl.create(:female_admin, name: "Jill", gender: nil) } + subject { FactoryBot.create(:female_admin, name: "Jill", gender: nil) } its(:name) { should eq "Jill" } its(:gender) { should be_nil } it { should be_admin } @@ -119,14 +119,14 @@ context "the child with multiple traits who override the same attribute" do context "when the male assigns name after female" do - subject { FactoryGirl.create(:male_after_female_admin) } + subject { FactoryBot.create(:male_after_female_admin) } its(:name) { should eq "Joe" } its(:gender) { should eq "Male" } it { should be_admin } end context "when the female assigns name after male" do - subject { FactoryGirl.create(:female_after_male_admin) } + subject { FactoryBot.create(:female_after_male_admin) } its(:name) { should eq "Jane" } its(:gender) { should eq "Female" } it { should be_admin } @@ -134,46 +134,46 @@ end context "child class with scoped trait and inherited trait" do - subject { FactoryGirl.create(:female_admin_judy) } + subject { FactoryBot.create(:female_admin_judy) } its(:name) { should eq "Judy" } its(:gender) { should eq "Female" } it { should be_admin } end context "factory using global trait" do - subject { FactoryGirl.create(:user_with_email) } + subject { FactoryBot.create(:user_with_email) } its(:name) { should eq "Bill" } its(:email) { should eq "Bill@example.com"} end context "factory created with alternate syntax for specifying trait" do - subject { FactoryGirl.create(:male_user) } + subject { FactoryBot.create(:male_user) } its(:gender) { should eq "Male" } end context "factory created with alternate syntax where trait name and attribute are the same" do - subject { FactoryGirl.create(:great_user) } + subject { FactoryBot.create(:great_user) } its(:great) { should eq "GREAT!!!" } end context "factory created with alternate syntax where trait name and attribute are the same and attribute is overridden" do - subject { FactoryGirl.create(:great_user, great: "SORT OF!!!") } + subject { FactoryBot.create(:great_user, great: "SORT OF!!!") } its(:great) { should eq "SORT OF!!!" } end context "child factory created where trait attributes are inherited" do - subject { FactoryGirl.create(:child_male_user) } + subject { FactoryBot.create(:child_male_user) } its(:gender) { should eq "Male" } its(:date_of_birth) { should eq Date.parse("1/1/2000") } end context "factory outside of scope" do - subject { FactoryGirl.create(:user_without_admin_scoping) } + subject { FactoryBot.create(:user_without_admin_scoping) } it { expect { subject }.to raise_error(ArgumentError, "Trait not registered: admin_trait") } end context "child factory using grandparents' trait" do - subject { FactoryGirl.create(:female_great_user) } + subject { FactoryBot.create(:female_great_user) } its(:great) { should eq "GREAT!!!" } end end @@ -182,7 +182,7 @@ before do define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do name "John" @@ -205,17 +205,17 @@ end context "when the factory has a trait passed via arguments" do - subject { FactoryGirl.create(:caps_user) } + subject { FactoryBot.create(:caps_user) } its(:name) { should eq "JOHN" } end context "when the factory has an implicit trait" do - subject { FactoryGirl.create(:caps_user_implicit_trait) } + subject { FactoryBot.create(:caps_user_implicit_trait) } its(:name) { should eq "JOHN" } end it "executes callbacks in the order assigned" do - expect(FactoryGirl.create(:awesome_user).name).to eq "awesome" + expect(FactoryBot.create(:awesome_user).name).to eq "awesome" end end @@ -223,7 +223,7 @@ before do define_model("User", name: :string, admin: :boolean) - FactoryGirl.define do + FactoryBot.define do factory :user do name "John" @@ -239,41 +239,41 @@ end context "adding traits in create" do - subject { FactoryGirl.create(:user, :admin, :great, name: "Joe") } + subject { FactoryBot.create(:user, :admin, :great, name: "Joe") } its(:admin) { should be true } its(:name) { should eq "JOE" } it "doesn't modify the user factory" do subject - expect(FactoryGirl.create(:user)).not_to be_admin - expect(FactoryGirl.create(:user).name).to eq "John" + expect(FactoryBot.create(:user)).not_to be_admin + expect(FactoryBot.create(:user).name).to eq "John" end end context "adding traits in build" do - subject { FactoryGirl.build(:user, :admin, :great, name: "Joe") } + subject { FactoryBot.build(:user, :admin, :great, name: "Joe") } its(:admin) { should be true } its(:name) { should eq "Joe" } end context "adding traits in attributes_for" do - subject { FactoryGirl.attributes_for(:user, :admin, :great) } + subject { FactoryBot.attributes_for(:user, :admin, :great) } its([:admin]) { should be true } its([:name]) { should eq "John" } end context "adding traits in build_stubbed" do - subject { FactoryGirl.build_stubbed(:user, :admin, :great, name: "Jack") } + subject { FactoryBot.build_stubbed(:user, :admin, :great, name: "Jack") } its(:admin) { should be true } its(:name) { should eq "Jack" } end context "adding traits in create_list" do - subject { FactoryGirl.create_list(:user, 2, :admin, :great, name: "Joe") } + subject { FactoryBot.create_list(:user, 2, :admin, :great, name: "Joe") } its(:length) { should eq 2 } @@ -286,7 +286,7 @@ end context "adding traits in build_list" do - subject { FactoryGirl.build_list(:user, 2, :admin, :great, name: "Joe") } + subject { FactoryBot.build_list(:user, 2, :admin, :great, name: "Joe") } its(:length) { should eq 2 } @@ -303,7 +303,7 @@ before do define_model("User", name: :string, email: :string, combined: :string) - FactoryGirl.define do + FactoryBot.define do trait :email do email { "#{name}@example.com" } end @@ -316,7 +316,7 @@ end end - subject { FactoryGirl.build(:user) } + subject { FactoryBot.build(:user) } its(:name) { should eq "John" } its(:email) { should eq "John@example.com" } its(:combined) { should eq "John " } @@ -332,7 +332,7 @@ belongs_to :user end - FactoryGirl.define do + FactoryBot.define do factory :user do trait :with_post do posts { [ Post.new ] } @@ -342,9 +342,9 @@ end it "applies traits only to the instance generated for that call" do - expect(FactoryGirl.create(:user, :with_post).posts).not_to be_empty - expect(FactoryGirl.create(:user).posts).to be_empty - expect(FactoryGirl.create(:user, :with_post).posts).not_to be_empty + expect(FactoryBot.create(:user, :with_post).posts).not_to be_empty + expect(FactoryBot.create(:user).posts).to be_empty + expect(FactoryBot.create(:user, :with_post).posts).not_to be_empty end end @@ -352,7 +352,7 @@ before do define_model("User", status: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do status "pending" @@ -368,31 +368,31 @@ end it "returns the default status" do - expect(FactoryGirl.build(:user).status).to eq "pending" + expect(FactoryBot.build(:user).status).to eq "pending" end it "prefers inline trait attributes over default attributes" do - expect(FactoryGirl.build(:user, :accepted).status).to eq "accepted" + expect(FactoryBot.build(:user, :accepted).status).to eq "accepted" end it "prefers traits on a factory over default attributes" do - expect(FactoryGirl.build(:declined_user).status).to eq "declined" + expect(FactoryBot.build(:declined_user).status).to eq "declined" end it "prefers inline trait attributes over traits on a factory" do - expect(FactoryGirl.build(:declined_user, :accepted).status).to eq "accepted" + expect(FactoryBot.build(:declined_user, :accepted).status).to eq "accepted" end it "prefers attributes on factories over attributes from non-inline traits" do - expect(FactoryGirl.build(:extended_declined_user).status).to eq "extended_declined" + expect(FactoryBot.build(:extended_declined_user).status).to eq "extended_declined" end it "prefers inline traits over attributes on factories" do - expect(FactoryGirl.build(:extended_declined_user, :accepted).status).to eq "accepted" + expect(FactoryBot.build(:extended_declined_user, :accepted).status).to eq "accepted" end it "prefers overridden attributes over attributes from traits, inline traits, or attributes on factories" do - expect(FactoryGirl.build(:extended_declined_user, :accepted, status: "completely overridden").status).to eq "completely overridden" + expect(FactoryBot.build(:extended_declined_user, :accepted, status: "completely overridden").status).to eq "completely overridden" end end @@ -400,7 +400,7 @@ before do define_model("User", role: :string, gender: :string, age: :integer) - FactoryGirl.define do + FactoryBot.define do factory :user do trait(:female) { gender "female" } trait(:admin) { role "admin" } @@ -413,7 +413,7 @@ end it "can honor traits on the very first call" do - user = FactoryGirl.build(:female_user, :admin, age: 30) + user = FactoryBot.build(:female_user, :admin, age: 30) expect(user.gender).to eq 'female' expect(user.age).to eq 30 expect(user.role).to eq 'admin' @@ -424,7 +424,7 @@ before do define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do trait :with_to_create do to_create { |instance| instance.name = "to_create" } @@ -453,38 +453,38 @@ end it "can apply to_create from traits" do - expect(FactoryGirl.create(:user, :with_to_create).name).to eq "to_create" + expect(FactoryBot.create(:user, :with_to_create).name).to eq "to_create" end it "can apply to_create from the definition" do - expect(FactoryGirl.create(:sub_user).name).to eq "sub" - expect(FactoryGirl.create(:child_user).name).to eq "sub" + expect(FactoryBot.create(:sub_user).name).to eq "sub" + expect(FactoryBot.create(:child_user).name).to eq "sub" end it "gives additional traits higher priority than to_create from the definition" do - expect(FactoryGirl.create(:sub_user, :with_to_create).name).to eq "to_create" - expect(FactoryGirl.create(:child_user, :with_to_create).name).to eq "to_create" + expect(FactoryBot.create(:sub_user, :with_to_create).name).to eq "to_create" + expect(FactoryBot.create(:child_user, :with_to_create).name).to eq "to_create" end it "gives base traits normal priority" do - expect(FactoryGirl.create(:sub_user_with_trait).name).to eq "to_create" - expect(FactoryGirl.create(:child_user_with_trait).name).to eq "to_create" + expect(FactoryBot.create(:sub_user_with_trait).name).to eq "to_create" + expect(FactoryBot.create(:child_user_with_trait).name).to eq "to_create" end it "gives base traits lower priority than overrides" do - expect(FactoryGirl.create(:sub_user_with_trait_and_override).name).to eq "sub with trait and override" - expect(FactoryGirl.create(:child_user_with_trait_and_override).name).to eq "sub with trait and override" + expect(FactoryBot.create(:sub_user_with_trait_and_override).name).to eq "sub with trait and override" + expect(FactoryBot.create(:child_user_with_trait_and_override).name).to eq "sub with trait and override" end it "gives additional traits higher priority than base traits and factory definition" do - FactoryGirl.define do + FactoryBot.define do trait :overridden do to_create { |instance| instance.name = "completely overridden" } end end - expect(FactoryGirl.create(:sub_user_with_trait_and_override, :overridden).name).to eq "completely overridden" - expect(FactoryGirl.create(:child_user_with_trait_and_override, :overridden).name).to eq "completely overridden" + expect(FactoryBot.create(:sub_user_with_trait_and_override, :overridden).name).to eq "completely overridden" + expect(FactoryBot.create(:child_user_with_trait_and_override, :overridden).name).to eq "completely overridden" end end @@ -498,7 +498,7 @@ def initialize(name) end end - FactoryGirl.define do + FactoryBot.define do factory :user do trait :with_initialize_with do initialize_with { new("initialize_with") } @@ -527,38 +527,38 @@ def initialize(name) end it "can apply initialize_with from traits" do - expect(FactoryGirl.build(:user, :with_initialize_with).name).to eq "initialize_with" + expect(FactoryBot.build(:user, :with_initialize_with).name).to eq "initialize_with" end it "can apply initialize_with from the definition" do - expect(FactoryGirl.build(:sub_user).name).to eq "sub" - expect(FactoryGirl.build(:child_user).name).to eq "sub" + expect(FactoryBot.build(:sub_user).name).to eq "sub" + expect(FactoryBot.build(:child_user).name).to eq "sub" end it "gives additional traits higher priority than initialize_with from the definition" do - expect(FactoryGirl.build(:sub_user, :with_initialize_with).name).to eq "initialize_with" - expect(FactoryGirl.build(:child_user, :with_initialize_with).name).to eq "initialize_with" + expect(FactoryBot.build(:sub_user, :with_initialize_with).name).to eq "initialize_with" + expect(FactoryBot.build(:child_user, :with_initialize_with).name).to eq "initialize_with" end it "gives base traits normal priority" do - expect(FactoryGirl.build(:sub_user_with_trait).name).to eq "initialize_with" - expect(FactoryGirl.build(:child_user_with_trait).name).to eq "initialize_with" + expect(FactoryBot.build(:sub_user_with_trait).name).to eq "initialize_with" + expect(FactoryBot.build(:child_user_with_trait).name).to eq "initialize_with" end it "gives base traits lower priority than overrides" do - expect(FactoryGirl.build(:sub_user_with_trait_and_override).name).to eq "sub with trait and override" - expect(FactoryGirl.build(:child_user_with_trait_and_override).name).to eq "sub with trait and override" + expect(FactoryBot.build(:sub_user_with_trait_and_override).name).to eq "sub with trait and override" + expect(FactoryBot.build(:child_user_with_trait_and_override).name).to eq "sub with trait and override" end it "gives additional traits higher priority than base traits and factory definition" do - FactoryGirl.define do + FactoryBot.define do trait :overridden do initialize_with { new("completely overridden") } end end - expect(FactoryGirl.build(:sub_user_with_trait_and_override, :overridden).name).to eq "completely overridden" - expect(FactoryGirl.build(:child_user_with_trait_and_override, :overridden).name).to eq "completely overridden" + expect(FactoryBot.build(:sub_user_with_trait_and_override, :overridden).name).to eq "completely overridden" + expect(FactoryBot.build(:child_user_with_trait_and_override, :overridden).name).to eq "completely overridden" end end @@ -576,7 +576,7 @@ def initialize(name) shared_examples_for "assigning data from traits" do it "assigns the correct values" do - user = FactoryGirl.create(:user, :female_admin) + user = FactoryBot.create(:user, :female_admin) expect(user.gender).to eq "FEMALE" expect(user.role).to eq "ADMIN" expect(user.name).to eq "Jane Doe" @@ -585,7 +585,7 @@ def initialize(name) context "defined outside the factory" do before do - FactoryGirl.define do + FactoryBot.define do trait :female do gender "female" to_create { |instance| instance.gender = instance.gender.upcase } @@ -615,7 +615,7 @@ def initialize(name) context "defined inside the factory" do before do - FactoryGirl.define do + FactoryBot.define do factory :user do trait :female do gender "female" @@ -648,7 +648,7 @@ def initialize(name) before do define_model("User", value: :integer) - FactoryGirl.define do + FactoryBot.define do factory :user do value 0 @@ -664,7 +664,7 @@ def initialize(name) end it "only runs the callback once" do - expect(FactoryGirl.build(:user_with_trait_with_callback).value).to eq 1 + expect(FactoryBot.build(:user_with_trait_with_callback).value).to eq 1 end end @@ -684,7 +684,7 @@ def initialize(name) belongs_to :author, class_name: 'User' end - FactoryGirl.define do + FactoryBot.define do factory :user do admin false @@ -708,19 +708,19 @@ def initialize(name) end it "allows assigning traits for the factory of an association" do - author = FactoryGirl.create(:post).author + author = FactoryBot.create(:post).author expect(author).to be_admin expect(author.name).to eq 'John Doe' end it "allows inline traits with the default association" do - user = FactoryGirl.create(:comment).user + user = FactoryBot.create(:comment).user expect(user).to be_admin expect(user.name).to eq 'Joe Slick' end it "allows inline traits with a specific factory for an association" do - creator = FactoryGirl.create(:order).creator + creator = FactoryBot.create(:order).creator expect(creator).to be_admin expect(creator.name).to eq 'Joe Creator' end diff --git a/spec/acceptance/transient_attributes_spec.rb b/spec/acceptance/transient_attributes_spec.rb index 461ddf875..8f590a3bb 100644 --- a/spec/acceptance/transient_attributes_spec.rb +++ b/spec/acceptance/transient_attributes_spec.rb @@ -4,7 +4,7 @@ before do define_model("User", name: :string, email: :string) - FactoryGirl.define do + FactoryBot.define do sequence(:name) { |n| "John #{n}" } factory :user do @@ -14,7 +14,7 @@ upcased false end - name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" } + name { "#{FactoryBot.generate(:name)}#{" - Rockstar" if rockstar}" } email { "#{name.downcase}#{four}@example.com" } after(:create) do |user, evaluator| @@ -25,7 +25,7 @@ end context "returning attributes for a factory" do - subject { FactoryGirl.attributes_for(:user, rockstar: true) } + subject { FactoryBot.attributes_for(:user, rockstar: true) } it { should_not have_key(:four) } it { should_not have_key(:rockstar) } it { should_not have_key(:upcased) } @@ -34,10 +34,10 @@ end context "with a transient variable assigned" do - let(:rockstar) { FactoryGirl.create(:user, rockstar: true, four: "1234") } - let(:rockstar_with_name) { FactoryGirl.create(:user, name: "Jane Doe", rockstar: true) } - let(:upcased_rockstar) { FactoryGirl.create(:user, rockstar: true, upcased: true) } - let(:groupie) { FactoryGirl.create(:user, rockstar: false) } + let(:rockstar) { FactoryBot.create(:user, rockstar: true, four: "1234") } + let(:rockstar_with_name) { FactoryBot.create(:user, name: "Jane Doe", rockstar: true) } + let(:upcased_rockstar) { FactoryBot.create(:user, rockstar: true, upcased: true) } + let(:groupie) { FactoryBot.create(:user, rockstar: false) } it "generates the correct attributes on a rockstar" do expect(rockstar.name).to eq "John 1 - Rockstar" @@ -61,7 +61,7 @@ end context "without transient variables assigned" do - let(:rockstar) { FactoryGirl.create(:user) } + let(:rockstar) { FactoryBot.create(:user) } it "uses the default value of the attribute" do expect(rockstar.name).to eq "John 1 - Rockstar" @@ -70,7 +70,7 @@ context "using aliased 'ignore' method name", :silence_deprecation do before do - FactoryGirl.define do + FactoryBot.define do factory :user_using_ignore, class: User do ignore do honorific "Esteemed" @@ -81,7 +81,7 @@ end end - let(:esteemed) { FactoryGirl.create(:user_using_ignore) } + let(:esteemed) { FactoryBot.create(:user_using_ignore) } it "uses the default value of the attribute" do expect(esteemed.name).to eq "Esteemed Jane Doe" @@ -93,7 +93,7 @@ before do define_model("User", name: :string) - FactoryGirl.define do + FactoryBot.define do factory :user do transient do sequence(:counter) @@ -105,8 +105,8 @@ end it "increments sequences correctly" do - expect(FactoryGirl.build(:user).name).to eq "John Doe 1" - expect(FactoryGirl.build(:user).name).to eq "John Doe 2" + expect(FactoryBot.build(:user).name).to eq "John Doe 1" + expect(FactoryBot.build(:user).name).to eq "John Doe 2" end end @@ -124,7 +124,7 @@ def initialize(id, name) end end - FactoryGirl.define do + FactoryBot.define do factory :user do transient do foo { Foo.new('id-of-foo', 'name-of-foo')} @@ -137,7 +137,7 @@ def initialize(id, name) end it "does not ignore an _id attribute that is an alias for a transient attribute" do - user = FactoryGirl.build(:user, :foo => Foo.new('passed-in-id-of-foo', 'passed-in-name-of-foo')) + user = FactoryBot.build(:user, :foo => Foo.new('passed-in-id-of-foo', 'passed-in-name-of-foo')) expect(user.foo_id).to eq 'passed-in-id-of-foo' expect(user.foo_name).to eq 'passed-in-name-of-foo' end diff --git a/spec/factory_girl/aliases_spec.rb b/spec/factory_bot/aliases_spec.rb similarity index 59% rename from spec/factory_girl/aliases_spec.rb rename to spec/factory_bot/aliases_spec.rb index ed64c6ec0..06f17207b 100644 --- a/spec/factory_girl/aliases_spec.rb +++ b/spec/factory_bot/aliases_spec.rb @@ -1,30 +1,30 @@ require 'spec_helper' -describe FactoryGirl, "aliases" do +describe FactoryBot, "aliases" do context "aliases for an attribute" do - subject { FactoryGirl.aliases_for(:test) } + subject { FactoryBot.aliases_for(:test) } it { should include(:test) } it { should include(:test_id) } end context "aliases for a foreign key" do - subject { FactoryGirl.aliases_for(:test_id) } + subject { FactoryBot.aliases_for(:test_id) } it { should include(:test) } it { should include(:test_id) } end context "aliases for an attribute starting with an underscore" do - subject { FactoryGirl.aliases_for(:_id) } + subject { FactoryBot.aliases_for(:_id) } it { should_not include(:id) } end end -describe FactoryGirl, "after defining an alias" do +describe FactoryBot, "after defining an alias" do before do - FactoryGirl.aliases << [/(.*)_suffix/, '\1'] + FactoryBot.aliases << [/(.*)_suffix/, '\1'] end - subject { FactoryGirl.aliases_for(:test_suffix) } + subject { FactoryBot.aliases_for(:test_suffix) } it { should include(:test) } it { should include(:test_suffix_id) } diff --git a/spec/factory_girl/attribute/association_spec.rb b/spec/factory_bot/attribute/association_spec.rb similarity index 76% rename from spec/factory_girl/attribute/association_spec.rb rename to spec/factory_bot/attribute/association_spec.rb index 900b7a0f4..ade49d469 100644 --- a/spec/factory_girl/attribute/association_spec.rb +++ b/spec/factory_bot/attribute/association_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' -describe FactoryGirl::Attribute::Association do +describe FactoryBot::Attribute::Association do let(:name) { :author } let(:factory) { :user } let(:overrides) { { first_name: "John" } } let(:association) { double("association") } - subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) } + subject { FactoryBot::Attribute::Association.new(name, factory, overrides) } module MissingMethods def association(*args); end @@ -34,7 +34,7 @@ def association(*args); end end end -describe FactoryGirl::Attribute::Association, "with a string name" do - subject { FactoryGirl::Attribute::Association.new("name", :user, {}) } +describe FactoryBot::Attribute::Association, "with a string name" do + subject { FactoryBot::Attribute::Association.new("name", :user, {}) } its(:name) { should eq :name } end diff --git a/spec/factory_girl/attribute/dynamic_spec.rb b/spec/factory_bot/attribute/dynamic_spec.rb similarity index 75% rename from spec/factory_girl/attribute/dynamic_spec.rb rename to spec/factory_bot/attribute/dynamic_spec.rb index 931a8c087..19f24700d 100644 --- a/spec/factory_girl/attribute/dynamic_spec.rb +++ b/spec/factory_bot/attribute/dynamic_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' -describe FactoryGirl::Attribute::Dynamic do +describe FactoryBot::Attribute::Dynamic do let(:name) { :first_name } let(:block) { -> { } } - subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) } + subject { FactoryBot::Attribute::Dynamic.new(name, false, block) } its(:name) { should eq name } @@ -47,15 +47,15 @@ def attribute_defined_on_attribute(*args); end end context "with a block returning a sequence" do - let(:block) { -> { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) { |n| "foo#{n}" }) } } + let(:block) { -> { FactoryBot.register_sequence(FactoryBot::Sequence.new(:email, 1) { |n| "foo#{n}" }) } } it "raises a sequence abuse error" do - expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError) + expect { subject.to_proc.call }.to raise_error(FactoryBot::SequenceAbuseError) end end end -describe FactoryGirl::Attribute::Dynamic, "with a string name" do - subject { FactoryGirl::Attribute::Dynamic.new("name", false, -> { } ) } +describe FactoryBot::Attribute::Dynamic, "with a string name" do + subject { FactoryBot::Attribute::Dynamic.new("name", false, -> { } ) } its(:name) { should eq :name } end diff --git a/spec/factory_bot/attribute/sequence_spec.rb b/spec/factory_bot/attribute/sequence_spec.rb new file mode 100644 index 000000000..cee8262d7 --- /dev/null +++ b/spec/factory_bot/attribute/sequence_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe FactoryBot::Attribute::Sequence do + let(:sequence_name) { :name } + let(:name) { :first_name } + let(:sequence) { FactoryBot::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } } + + subject { FactoryBot::Attribute::Sequence.new(name, sequence_name, false) } + before { FactoryBot.register_sequence(sequence) } + + its(:name) { should eq name } + + it "assigns the next value in the sequence" do + expect(subject.to_proc.call).to eq "Name 5" + end +end diff --git a/spec/factory_girl/attribute/static_spec.rb b/spec/factory_bot/attribute/static_spec.rb similarity index 50% rename from spec/factory_girl/attribute/static_spec.rb rename to spec/factory_bot/attribute/static_spec.rb index c65fc4f61..88bcd821f 100644 --- a/spec/factory_girl/attribute/static_spec.rb +++ b/spec/factory_bot/attribute/static_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' -describe FactoryGirl::Attribute::Static do +describe FactoryBot::Attribute::Static do let(:name) { :first_name } let(:value) { "John" } - subject { FactoryGirl::Attribute::Static.new(name, value, false) } + subject { FactoryBot::Attribute::Static.new(name, value, false) } its(:name) { should eq name } @@ -13,7 +13,7 @@ end end -describe FactoryGirl::Attribute::Static, "with a string name" do - subject { FactoryGirl::Attribute::Static.new("name", nil, false) } +describe FactoryBot::Attribute::Static, "with a string name" do + subject { FactoryBot::Attribute::Static.new("name", nil, false) } its(:name) { should eq :name } end diff --git a/spec/factory_girl/attribute_list_spec.rb b/spec/factory_bot/attribute_list_spec.rb similarity index 60% rename from spec/factory_girl/attribute_list_spec.rb rename to spec/factory_bot/attribute_list_spec.rb index 35ef0b6d7..5c2d86628 100644 --- a/spec/factory_girl/attribute_list_spec.rb +++ b/spec/factory_bot/attribute_list_spec.rb @@ -1,8 +1,8 @@ require "spec_helper" -describe FactoryGirl::AttributeList, "#define_attribute" do - let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) } - let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) } +describe FactoryBot::AttributeList, "#define_attribute" do + let(:static_attribute) { FactoryBot::Attribute::Static.new(:full_name, "value", false) } + let(:dynamic_attribute) { FactoryBot::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) } it "maintains a list of attributes" do subject.define_attribute(static_attribute) @@ -20,18 +20,18 @@ it "raises if an attribute has already been defined" do expect { 2.times { subject.define_attribute(static_attribute) } - }.to raise_error(FactoryGirl::AttributeDefinitionError, "Attribute already defined: full_name") + }.to raise_error(FactoryBot::AttributeDefinitionError, "Attribute already defined: full_name") end end -describe FactoryGirl::AttributeList, "#define_attribute with a named attribute list" do - subject { FactoryGirl::AttributeList.new(:author) } +describe FactoryBot::AttributeList, "#define_attribute with a named attribute list" do + subject { FactoryBot::AttributeList.new(:author) } - let(:association_with_same_name) { FactoryGirl::Attribute::Association.new(:author, :author, {}) } - let(:association_with_different_name) { FactoryGirl::Attribute::Association.new(:author, :post, {}) } + let(:association_with_same_name) { FactoryBot::Attribute::Association.new(:author, :author, {}) } + let(:association_with_different_name) { FactoryBot::Attribute::Association.new(:author, :post, {}) } it "raises when the attribute is a self-referencing association" do - expect { subject.define_attribute(association_with_same_name) }.to raise_error(FactoryGirl::AssociationDefinitionError, "Self-referencing association 'author' in 'author'") + expect { subject.define_attribute(association_with_same_name) }.to raise_error(FactoryBot::AssociationDefinitionError, "Self-referencing association 'author' in 'author'") end it "does not raise when the attribute is not a self-referencing association" do @@ -39,14 +39,14 @@ end end -describe FactoryGirl::AttributeList, "#apply_attributes" do - let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "John Adams", false) } - let(:city_attribute) { FactoryGirl::Attribute::Static.new(:city, "Boston", false) } - let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(model) { "#{model.full_name}@example.com" }) } - let(:login_attribute) { FactoryGirl::Attribute::Dynamic.new(:login, false, ->(model) { "username-#{model.full_name}" }) } +describe FactoryBot::AttributeList, "#apply_attributes" do + let(:full_name_attribute) { FactoryBot::Attribute::Static.new(:full_name, "John Adams", false) } + let(:city_attribute) { FactoryBot::Attribute::Static.new(:city, "Boston", false) } + let(:email_attribute) { FactoryBot::Attribute::Dynamic.new(:email, false, ->(model) { "#{model.full_name}@example.com" }) } + let(:login_attribute) { FactoryBot::Attribute::Dynamic.new(:login, false, ->(model) { "username-#{model.full_name}" }) } def list(*attributes) - FactoryGirl::AttributeList.new.tap do |list| + FactoryBot::AttributeList.new.tap do |list| attributes.each { |attribute| list.define_attribute(attribute) } end end @@ -59,11 +59,11 @@ def list(*attributes) end end -describe FactoryGirl::AttributeList, "#associations" do - let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) } - let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) } - let(:author_attribute) { FactoryGirl::Attribute::Association.new(:author, :user, {}) } - let(:profile_attribute) { FactoryGirl::Attribute::Association.new(:profile, :profile, {}) } +describe FactoryBot::AttributeList, "#associations" do + let(:full_name_attribute) { FactoryBot::Attribute::Static.new(:full_name, "value", false) } + let(:email_attribute) { FactoryBot::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) } + let(:author_attribute) { FactoryBot::Attribute::Association.new(:author, :user, {}) } + let(:profile_attribute) { FactoryBot::Attribute::Association.new(:profile, :profile, {}) } before do subject.define_attribute(full_name_attribute) @@ -77,13 +77,13 @@ def list(*attributes) end end -describe FactoryGirl::AttributeList, "filter based on ignored attributes" do +describe FactoryBot::AttributeList, "filter based on ignored attributes" do def build_ignored_attribute(name) - FactoryGirl::Attribute::Static.new(name, "value", true) + FactoryBot::Attribute::Static.new(name, "value", true) end def build_non_ignored_attribute(name) - FactoryGirl::Attribute::Static.new(name, "value", false) + FactoryBot::Attribute::Static.new(name, "value", false) end before do @@ -103,17 +103,17 @@ def build_non_ignored_attribute(name) end end -describe FactoryGirl::AttributeList, "generating names" do +describe FactoryBot::AttributeList, "generating names" do def build_ignored_attribute(name) - FactoryGirl::Attribute::Static.new(name, "value", true) + FactoryBot::Attribute::Static.new(name, "value", true) end def build_non_ignored_attribute(name) - FactoryGirl::Attribute::Static.new(name, "value", false) + FactoryBot::Attribute::Static.new(name, "value", false) end def build_association(name) - FactoryGirl::Attribute::Association.new(name, :user, {}) + FactoryBot::Attribute::Association.new(name, :user, {}) end before do diff --git a/spec/factory_bot/attribute_spec.rb b/spec/factory_bot/attribute_spec.rb new file mode 100644 index 000000000..76b3f23a6 --- /dev/null +++ b/spec/factory_bot/attribute_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe FactoryBot::Attribute do + let(:name) { "user" } + subject { FactoryBot::Attribute.new(name, false) } + + its(:name) { should eq name.to_sym } + it { should_not be_association } + + it "raises an error when defining an attribute writer" do + error_message = %{factory_bot uses 'test value' syntax rather than 'test = value'} + expect { + FactoryBot::Attribute.new('test=', false) + }.to raise_error(FactoryBot::AttributeDefinitionError, error_message) + end +end diff --git a/spec/factory_bot/callback_spec.rb b/spec/factory_bot/callback_spec.rb new file mode 100644 index 000000000..813215b45 --- /dev/null +++ b/spec/factory_bot/callback_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe FactoryBot::Callback do + it "has a name" do + expect(FactoryBot::Callback.new(:after_create, -> {}).name).to eq :after_create + end + + it "converts strings to symbols" do + expect(FactoryBot::Callback.new("after_create", -> {}).name).to eq :after_create + end + + it "runs its block with no parameters" do + ran_with = nil + FactoryBot::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two) + expect(ran_with).to eq [] + end + + it "runs its block with one parameter" do + ran_with = nil + FactoryBot::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two) + expect(ran_with).to eq [:one] + end + + it "runs its block with two parameters" do + ran_with = nil + FactoryBot::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two) + expect(ran_with).to eq [:one, :two] + end + + it "allows valid callback names to be assigned" do + FactoryBot.callback_names.each do |callback_name| + expect { FactoryBot::Callback.new(callback_name, -> {}) }. + to_not raise_error + end + end + + it "raises if an invalid callback name is assigned" do + expect { FactoryBot::Callback.new(:magic_fairies, -> {}) }. + to raise_error(FactoryBot::InvalidCallbackNameError, /magic_fairies is not a valid callback name/) + end +end diff --git a/spec/factory_girl/declaration/implicit_spec.rb b/spec/factory_bot/declaration/implicit_spec.rb similarity index 52% rename from spec/factory_girl/declaration/implicit_spec.rb rename to spec/factory_bot/declaration/implicit_spec.rb index 80e340b45..9d7734f08 100644 --- a/spec/factory_girl/declaration/implicit_spec.rb +++ b/spec/factory_bot/declaration/implicit_spec.rb @@ -1,13 +1,13 @@ require 'spec_helper' -describe FactoryGirl::Declaration::Implicit do +describe FactoryBot::Declaration::Implicit do let(:name) { :author } - let(:declaration) { FactoryGirl::Declaration::Implicit.new(name) } + let(:declaration) { FactoryBot::Declaration::Implicit.new(name) } subject { declaration.to_attributes.first } context "with a known factory" do before do - allow(FactoryGirl.factories).to receive(:registered?).and_return true + allow(FactoryBot.factories).to receive(:registered?).and_return true end it { should be_association } @@ -16,10 +16,10 @@ context "with a known sequence" do before do - allow(FactoryGirl.sequences).to receive(:registered?).and_return true + allow(FactoryBot.sequences).to receive(:registered?).and_return true end it { should_not be_association } - it { should be_a(FactoryGirl::Attribute::Sequence) } + it { should be_a(FactoryBot::Attribute::Sequence) } end end diff --git a/spec/factory_girl/declaration_list_spec.rb b/spec/factory_bot/declaration_list_spec.rb similarity index 89% rename from spec/factory_girl/declaration_list_spec.rb rename to spec/factory_bot/declaration_list_spec.rb index a36784e9a..a03def7d3 100644 --- a/spec/factory_girl/declaration_list_spec.rb +++ b/spec/factory_bot/declaration_list_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -describe FactoryGirl::DeclarationList, "#attributes" do +describe FactoryBot::DeclarationList, "#attributes" do let(:static_attribute_1) { double("static attribute 1") } let(:static_attribute_2) { double("static attribute 2") } let(:dynamic_attribute_1) { double("dynamic attribute 1") } @@ -15,13 +15,13 @@ end it "returns an AttributeList" do - expect(subject.attributes).to be_a(FactoryGirl::AttributeList) + expect(subject.attributes).to be_a(FactoryBot::AttributeList) end let(:attribute_list) { double("attribute list", define_attribute: true) } it "defines each attribute on the attribute list" do - allow(FactoryGirl::AttributeList).to receive(:new).and_return attribute_list + allow(FactoryBot::AttributeList).to receive(:new).and_return attribute_list subject.declare_attribute(static_declaration) subject.declare_attribute(dynamic_declaration) @@ -34,7 +34,7 @@ end end -describe FactoryGirl::DeclarationList, "#declare_attribute" do +describe FactoryBot::DeclarationList, "#declare_attribute" do let(:declaration_1) { double("declaration", name: "declaration 1") } let(:declaration_2) { double("declaration", name: "declaration 2") } let(:declaration_with_same_name) do diff --git a/spec/factory_girl/definition_proxy_spec.rb b/spec/factory_bot/definition_proxy_spec.rb similarity index 69% rename from spec/factory_girl/definition_proxy_spec.rb rename to spec/factory_bot/definition_proxy_spec.rb index e7f4a2527..45cc41b6e 100644 --- a/spec/factory_girl/definition_proxy_spec.rb +++ b/spec/factory_bot/definition_proxy_spec.rb @@ -1,13 +1,13 @@ require 'spec_helper' -describe FactoryGirl::DefinitionProxy, "#add_attribute" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#add_attribute" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "raises if both a block and value are given" do expect { proxy.add_attribute(:something, "great") { "will raise!" } - }.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given") + }.to raise_error(FactoryBot::AttributeDefinitionError, "Both value and block given") end it "declares a static attribute on the factory" do @@ -22,14 +22,14 @@ end end -describe FactoryGirl::DefinitionProxy, "#add_attribute when the proxy ignores attributes" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject, true) } +describe FactoryBot::DefinitionProxy, "#add_attribute when the proxy ignores attributes" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject, true) } it "raises if both a block and value are given" do expect { proxy.add_attribute(:something, "great") { "will raise!" } - }.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given") + }.to raise_error(FactoryBot::AttributeDefinitionError, "Both value and block given") end it "declares a static attribute on the factory" do @@ -44,9 +44,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#transient" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#transient" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "makes all attributes added ignored" do proxy.transient do @@ -57,9 +57,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#method_missing" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#method_missing" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "declares an implicit declaration without args or a block" do proxy.bogus @@ -83,32 +83,32 @@ end end -describe FactoryGirl::DefinitionProxy, "#sequence" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#sequence" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } - before { allow(FactoryGirl::Sequence).to receive(:new) } + before { allow(FactoryBot::Sequence).to receive(:new) } it "creates a new sequence starting at 1" do proxy.sequence(:great) - expect(FactoryGirl::Sequence).to have_received(:new).with(:great) + expect(FactoryBot::Sequence).to have_received(:new).with(:great) end it "creates a new sequence with an overridden starting vaue" do proxy.sequence(:great, "C") - expect(FactoryGirl::Sequence).to have_received(:new).with(:great, "C") + expect(FactoryBot::Sequence).to have_received(:new).with(:great, "C") end it "creates a new sequence with a block" do sequence_block = Proc.new { |n| "user+#{n}@example.com" } proxy.sequence(:great, 1, &sequence_block) - expect(FactoryGirl::Sequence).to have_received(:new).with(:great, 1, &sequence_block) + expect(FactoryBot::Sequence).to have_received(:new).with(:great, 1, &sequence_block) end end -describe FactoryGirl::DefinitionProxy, "#association" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#association" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "declares an association" do proxy.association(:association_name) @@ -121,9 +121,9 @@ end end -describe FactoryGirl::DefinitionProxy, "adding callbacks" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "adding callbacks" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } let(:callback) { -> { "my awesome callback!" } } context "#after(:build)" do @@ -160,9 +160,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#to_create" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#to_create" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "accepts a block to run in place of #save!" do to_create_block = ->(record) { record.persist } @@ -171,9 +171,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#factory" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#factory" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "without options" do proxy.factory(:child) @@ -192,9 +192,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#trait" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#trait" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "declares a trait" do male_trait = Proc.new { gender("Male") } @@ -203,9 +203,9 @@ end end -describe FactoryGirl::DefinitionProxy, "#initialize_with" do - subject { FactoryGirl::Definition.new } - let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } +describe FactoryBot::DefinitionProxy, "#initialize_with" do + subject { FactoryBot::Definition.new } + let(:proxy) { FactoryBot::DefinitionProxy.new(subject) } it "defines the constructor on the definition" do constructor = Proc.new { Array.new } diff --git a/spec/factory_girl/definition_spec.rb b/spec/factory_bot/definition_spec.rb similarity index 70% rename from spec/factory_girl/definition_spec.rb rename to spec/factory_bot/definition_spec.rb index 7ed2d607a..3e8c1dac2 100644 --- a/spec/factory_girl/definition_spec.rb +++ b/spec/factory_bot/definition_spec.rb @@ -1,24 +1,24 @@ require "spec_helper" -describe FactoryGirl::Definition do +describe FactoryBot::Definition do it { should delegate(:declare_attribute).to(:declarations) } end -describe FactoryGirl::Definition, "with a name" do +describe FactoryBot::Definition, "with a name" do let(:name) { :"great name" } - subject { FactoryGirl::Definition.new(name) } + subject { FactoryBot::Definition.new(name) } it "creates a new attribute list with the name passed" do - allow(FactoryGirl::DeclarationList).to receive(:new) + allow(FactoryBot::DeclarationList).to receive(:new) subject - expect(FactoryGirl::DeclarationList).to have_received(:new).with(name) + expect(FactoryBot::DeclarationList).to have_received(:new).with(name) end end -describe FactoryGirl::Definition, "#overridable" do +describe FactoryBot::Definition, "#overridable" do let(:list) { double("declaration list", overridable: true) } before do - allow(FactoryGirl::DeclarationList).to receive(:new).and_return list + allow(FactoryBot::DeclarationList).to receive(:new).and_return list end it "sets the declaration list as overridable" do @@ -27,7 +27,7 @@ end end -describe FactoryGirl::Definition, "defining traits" do +describe FactoryBot::Definition, "defining traits" do let(:trait_1) { double("trait") } let(:trait_2) { double("trait") } @@ -44,7 +44,7 @@ end end -describe FactoryGirl::Definition, "adding callbacks" do +describe FactoryBot::Definition, "adding callbacks" do let(:callback_1) { "callback1" } let(:callback_2) { "callback2" } @@ -55,7 +55,7 @@ end end -describe FactoryGirl::Definition, "#to_create" do +describe FactoryBot::Definition, "#to_create" do its(:to_create) { should be_nil } it "returns the assigned value when given a block" do diff --git a/spec/factory_girl/disallows_duplicates_registry_spec.rb b/spec/factory_bot/disallows_duplicates_registry_spec.rb similarity index 77% rename from spec/factory_girl/disallows_duplicates_registry_spec.rb rename to spec/factory_bot/disallows_duplicates_registry_spec.rb index e05036bd5..87895425c 100644 --- a/spec/factory_girl/disallows_duplicates_registry_spec.rb +++ b/spec/factory_bot/disallows_duplicates_registry_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do +describe FactoryBot::Decorator::DisallowsDuplicatesRegistry do let(:registry) { double("registry", name: "Great thing", register: true) } subject { described_class.new(registry) } @@ -14,6 +14,6 @@ it "raises when attempting to #register a previously registered strategy" do allow(registry).to receive(:registered?).and_return true expect { subject.register(:same_name, {}) }. - to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name") + to raise_error(FactoryBot::DuplicateDefinitionError, "Great thing already registered: same_name") end end diff --git a/spec/factory_girl/evaluator_class_definer_spec.rb b/spec/factory_bot/evaluator_class_definer_spec.rb similarity index 86% rename from spec/factory_girl/evaluator_class_definer_spec.rb rename to spec/factory_bot/evaluator_class_definer_spec.rb index aa661422b..1287498b7 100644 --- a/spec/factory_girl/evaluator_class_definer_spec.rb +++ b/spec/factory_bot/evaluator_class_definer_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -describe FactoryGirl::EvaluatorClassDefiner do +describe FactoryBot::EvaluatorClassDefiner do it "returns an evaluator when accessing the evaluator class" do - evaluator = define_evaluator(parent_class: FactoryGirl::Evaluator) + evaluator = define_evaluator(parent_class: FactoryBot::Evaluator) - expect(evaluator).to be_a(FactoryGirl::Evaluator) + expect(evaluator).to be_a(FactoryBot::Evaluator) end it "adds each attribute to the evaluator" do @@ -63,13 +63,13 @@ def define_evaluator(arguments = {}) evaluator_class = define_evaluator_class(arguments) - evaluator_class.new(FactoryGirl::Strategy::Null) + evaluator_class.new(FactoryBot::Strategy::Null) end def define_evaluator_class(arguments = {}) - evaluator_class_definer = FactoryGirl::EvaluatorClassDefiner.new( + evaluator_class_definer = FactoryBot::EvaluatorClassDefiner.new( arguments[:attributes] || [], - arguments[:parent_class] || FactoryGirl::Evaluator + arguments[:parent_class] || FactoryBot::Evaluator ) evaluator_class_definer.evaluator_class end diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_bot/factory_spec.rb similarity index 56% rename from spec/factory_girl/factory_spec.rb rename to spec/factory_bot/factory_spec.rb index 2b6a8be2d..5278af034 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_bot/factory_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' -describe FactoryGirl::Factory do +describe FactoryBot::Factory do before do @name = :user @class = define_class('User') - @factory = FactoryGirl::Factory.new(@name) - FactoryGirl.register_factory(@factory) + @factory = FactoryBot::Factory.new(@name) + FactoryBot.register_factory(@factory) end it "has a factory name" do @@ -18,22 +18,22 @@ it "passes a custom creation block" do strategy = double("strategy", result: nil, add_observer: true) - allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy + allow(FactoryBot::Strategy::Build).to receive(:new).and_return strategy block = -> {} - factory = FactoryGirl::Factory.new(:object) + factory = FactoryBot::Factory.new(:object) factory.to_create(&block) - factory.run(FactoryGirl::Strategy::Build, {}) + factory.run(FactoryBot::Strategy::Build, {}) - expect(strategy).to have_received(:result).with(instance_of(FactoryGirl::Evaluation)) + expect(strategy).to have_received(:result).with(instance_of(FactoryBot::Evaluation)) end it "returns associations" do - factory = FactoryGirl::Factory.new(:post) - FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin)) - factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {})) - factory.declare_attribute(FactoryGirl::Declaration::Association.new(:editor, {})) - factory.declare_attribute(FactoryGirl::Declaration::Implicit.new(:admin, factory)) + factory = FactoryBot::Factory.new(:post) + FactoryBot.register_factory(FactoryBot::Factory.new(:admin)) + factory.declare_attribute(FactoryBot::Declaration::Association.new(:author, {})) + factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {})) + factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory)) factory.associations.each do |association| expect(association).to be_association end @@ -41,14 +41,14 @@ end it "includes associations from the parent factory" do - association_on_parent = FactoryGirl::Declaration::Association.new(:association_on_parent, {}) - association_on_child = FactoryGirl::Declaration::Association.new(:association_on_child, {}) + association_on_parent = FactoryBot::Declaration::Association.new(:association_on_parent, {}) + association_on_child = FactoryBot::Declaration::Association.new(:association_on_child, {}) - factory = FactoryGirl::Factory.new(:post) + factory = FactoryBot::Factory.new(:post) factory.declare_attribute(association_on_parent) - FactoryGirl.register_factory(factory) + FactoryBot.register_factory(factory) - child_factory = FactoryGirl::Factory.new(:child_post, parent: :post) + child_factory = FactoryBot::Factory.new(:child_post, parent: :post) child_factory.declare_attribute(association_on_child) expect(child_factory.associations.map(&:name)).to eq [:association_on_parent, :association_on_child] @@ -62,32 +62,32 @@ end it "returns the overridden value in the generated attributes" do - declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!') + declaration = FactoryBot::Declaration::Static.new(@name, 'The price is wrong, Bob!') @factory.declare_attribute(declaration) - result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash) + result = @factory.run(FactoryBot::Strategy::AttributesFor, @hash) expect(result[@name]).to eq @value end it "does not call a lazy attribute block for an overridden attribute" do - declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, -> { flunk }) + declaration = FactoryBot::Declaration::Dynamic.new(@name, false, -> { flunk }) @factory.declare_attribute(declaration) - @factory.run(FactoryGirl::Strategy::AttributesFor, @hash) + @factory.run(FactoryBot::Strategy::AttributesFor, @hash) end it "overrides a symbol parameter with a string parameter" do - declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!') + declaration = FactoryBot::Declaration::Static.new(@name, 'The price is wrong, Bob!') @factory.declare_attribute(declaration) @hash = { @name.to_s => @value } - result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash) + result = @factory.run(FactoryBot::Strategy::AttributesFor, @hash) expect(result[@name]).to eq @value end end describe "overriding an attribute with an alias" do before do - @factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original')) - FactoryGirl.aliases << [/(.*)_alias/, '\1'] - @result = @factory.run(FactoryGirl::Strategy::AttributesFor, + @factory.declare_attribute(FactoryBot::Declaration::Static.new(:test, 'original')) + FactoryBot.aliases << [/(.*)_alias/, '\1'] + @result = @factory.run(FactoryBot::Strategy::AttributesFor, test_alias: 'new') end @@ -105,24 +105,24 @@ end it "creates a new factory using the class of the parent" do - child = FactoryGirl::Factory.new(:child, parent: @factory.name) + child = FactoryBot::Factory.new(:child, parent: @factory.name) child.compile expect(child.build_class).to eq @factory.build_class end it "creates a new factory while overriding the parent class" do - child = FactoryGirl::Factory.new(:child, class: String, parent: @factory.name) + child = FactoryBot::Factory.new(:child, class: String, parent: @factory.name) child.compile expect(child.build_class).to eq String end end -describe FactoryGirl::Factory, "when defined with a custom class" do - subject { FactoryGirl::Factory.new(:author, class: Float) } +describe FactoryBot::Factory, "when defined with a custom class" do + subject { FactoryBot::Factory.new(:author, class: Float) } its(:build_class) { should eq Float } end -describe FactoryGirl::Factory, "when given a class that overrides #to_s" do +describe FactoryBot::Factory, "when given a class that overrides #to_s" do let(:overriding_class) { Overriding::Class } before do @@ -134,46 +134,46 @@ def self.to_s end end - subject { FactoryGirl::Factory.new(:overriding_class, class: Overriding::Class) } + subject { FactoryBot::Factory.new(:overriding_class, class: Overriding::Class) } it "sets build_class correctly" do expect(subject.build_class).to eq overriding_class end end -describe FactoryGirl::Factory, "when defined with a class instead of a name" do +describe FactoryBot::Factory, "when defined with a class instead of a name" do let(:factory_class) { ArgumentError } let(:name) { :argument_error } - subject { FactoryGirl::Factory.new(factory_class) } + subject { FactoryBot::Factory.new(factory_class) } its(:name) { should eq name } its(:build_class) { should eq factory_class } end -describe FactoryGirl::Factory, "when defined with a custom class name" do - subject { FactoryGirl::Factory.new(:author, class: :argument_error) } +describe FactoryBot::Factory, "when defined with a custom class name" do + subject { FactoryBot::Factory.new(:author, class: :argument_error) } its(:build_class) { should eq ArgumentError } end -describe FactoryGirl::Factory, "with a name ending in s" do +describe FactoryBot::Factory, "with a name ending in s" do let(:name) { :business } let(:business_class) { Business } before { define_class('Business') } - subject { FactoryGirl::Factory.new(name) } + subject { FactoryBot::Factory.new(name) } its(:name) { should eq name } its(:build_class) { should eq business_class } end -describe FactoryGirl::Factory, "with a string for a name" do +describe FactoryBot::Factory, "with a string for a name" do let(:name) { :string } - subject { FactoryGirl::Factory.new(name.to_s) } + subject { FactoryBot::Factory.new(name.to_s) } its(:name) { should eq name } end -describe FactoryGirl::Factory, "for namespaced class" do +describe FactoryBot::Factory, "for namespaced class" do let(:name) { :settings } let(:settings_class) { Admin::Settings } @@ -183,7 +183,7 @@ def self.to_s end context "with a namespaced class with Namespace::Class syntax" do - subject { FactoryGirl::Factory.new(name, class: "Admin::Settings") } + subject { FactoryBot::Factory.new(name, class: "Admin::Settings") } it "sets build_class correctly" do expect(subject.build_class).to eq settings_class @@ -191,7 +191,7 @@ def self.to_s end context "with a namespaced class with namespace/class syntax" do - subject { FactoryGirl::Factory.new(name, class: "admin/settings") } + subject { FactoryBot::Factory.new(name, class: "admin/settings") } it "sets build_class correctly" do expect(subject.build_class).to eq settings_class @@ -199,36 +199,36 @@ def self.to_s end end -describe FactoryGirl::Factory, "human names" do +describe FactoryBot::Factory, "human names" do context "factory name without underscores" do - subject { FactoryGirl::Factory.new(:user) } + subject { FactoryBot::Factory.new(:user) } its(:names) { should eq [:user] } its(:human_names) { should eq ["user"] } end context "factory name with underscores" do - subject { FactoryGirl::Factory.new(:happy_user) } + subject { FactoryBot::Factory.new(:happy_user) } its(:names) { should eq [:happy_user] } its(:human_names) { should eq ["happy user"] } end context "factory name with big letters" do - subject { FactoryGirl::Factory.new(:LoL) } + subject { FactoryBot::Factory.new(:LoL) } its(:names) { should eq [:LoL] } its(:human_names) { should eq ["lol"] } end context "factory name with aliases" do - subject { FactoryGirl::Factory.new(:happy_user, aliases: [:gleeful_user, :person]) } + subject { FactoryBot::Factory.new(:happy_user, aliases: [:gleeful_user, :person]) } its(:names) { should eq [:happy_user, :gleeful_user, :person] } its(:human_names) { should eq ["happy user", "gleeful user", "person"] } end end -describe FactoryGirl::Factory, "running a factory" do - subject { FactoryGirl::Factory.new(:user) } - let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) } - let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) } +describe FactoryBot::Factory, "running a factory" do + subject { FactoryBot::Factory.new(:user) } + let(:attribute) { FactoryBot::Attribute::Static.new(:name, "value", false) } + let(:declaration) { FactoryBot::Declaration::Static.new(:name, "value", false) } let(:strategy) { double("strategy", result: "result", add_observer: true) } let(:attributes) { [attribute] } let(:attribute_list) do @@ -237,26 +237,26 @@ def self.to_s before do define_model("User", name: :string) - allow(FactoryGirl::Declaration::Static).to receive(:new). + allow(FactoryBot::Declaration::Static).to receive(:new). and_return declaration allow(declaration).to receive(:to_attributes).and_return attributes - allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy + allow(FactoryBot::Strategy::Build).to receive(:new).and_return strategy subject.declare_attribute(declaration) end it "creates the right strategy using the build class when running" do - subject.run(FactoryGirl::Strategy::Build, {}) - expect(FactoryGirl::Strategy::Build).to have_received(:new).once + subject.run(FactoryBot::Strategy::Build, {}) + expect(FactoryBot::Strategy::Build).to have_received(:new).once end it "returns the result from the strategy when running" do - expect(subject.run(FactoryGirl::Strategy::Build, {})).to eq "result" + expect(subject.run(FactoryBot::Strategy::Build, {})).to eq "result" end it "calls the block and returns the result" do block_run = nil block = ->(result) { block_run = "changed" } - subject.run(FactoryGirl::Strategy::Build, { }, &block) + subject.run(FactoryBot::Strategy::Build, { }, &block) expect(block_run).to eq "changed" end end diff --git a/spec/factory_girl/find_definitions_spec.rb b/spec/factory_bot/find_definitions_spec.rb similarity index 91% rename from spec/factory_girl/find_definitions_spec.rb rename to spec/factory_bot/find_definitions_spec.rb index b07e8979f..7eff04ca4 100644 --- a/spec/factory_girl/find_definitions_spec.rb +++ b/spec/factory_bot/find_definitions_spec.rb @@ -2,11 +2,11 @@ shared_examples_for "finds definitions" do before do - allow(FactoryGirl).to receive(:load) - FactoryGirl.find_definitions + allow(FactoryBot).to receive(:load) + FactoryBot.find_definitions end - subject { FactoryGirl } + subject { FactoryBot } end RSpec::Matchers.define :load_definitions_from do |file| @@ -79,15 +79,15 @@ def self.in_directory_with_files(*files) in_directory_with_files File.join(dir, 'factories', 'b.rb'), File.join(dir, 'factories', 'a.rb') it "loads the files in the right order" do - allow(FactoryGirl).to receive(:load) + allow(FactoryBot).to receive(:load) wd = File.dirname(__FILE__) file_b = File.join(wd, "tmp", dir, "factories", "b.rb") file_a = File.join(wd, "tmp", dir, "factories", "a.rb") - FactoryGirl.find_definitions + FactoryBot.find_definitions - expect(FactoryGirl).to have_received(:load).with(file_a).ordered - expect(FactoryGirl).to have_received(:load).with(file_b).ordered + expect(FactoryBot).to have_received(:load).with(file_a).ordered + expect(FactoryBot).to have_received(:load).with(file_b).ordered end end diff --git a/spec/factory_girl/null_factory_spec.rb b/spec/factory_bot/null_factory_spec.rb similarity index 64% rename from spec/factory_girl/null_factory_spec.rb rename to spec/factory_bot/null_factory_spec.rb index 8dfa5af56..317a50a52 100644 --- a/spec/factory_girl/null_factory_spec.rb +++ b/spec/factory_bot/null_factory_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -describe FactoryGirl::NullFactory do +describe FactoryBot::NullFactory do it { should delegate(:defined_traits).to(:definition) } it { should delegate(:callbacks).to(:definition) } it { should delegate(:attributes).to(:definition) } @@ -8,6 +8,6 @@ its(:compile) { should be_nil } its(:class_name) { should be_nil } - its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) } - its(:evaluator_class) { should eq FactoryGirl::Evaluator } + its(:attributes) { should be_an_instance_of(FactoryBot::AttributeList) } + its(:evaluator_class) { should eq FactoryBot::Evaluator } end diff --git a/spec/factory_girl/null_object_spec.rb b/spec/factory_bot/null_object_spec.rb similarity index 85% rename from spec/factory_girl/null_object_spec.rb rename to spec/factory_bot/null_object_spec.rb index cf31c6b18..da832b951 100644 --- a/spec/factory_girl/null_object_spec.rb +++ b/spec/factory_bot/null_object_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -describe FactoryGirl::NullObject do +describe FactoryBot::NullObject do let(:methods_to_respond_to) { %w[id age name admin?] } let(:methods_to_not_respond_to) { %w[email date_of_birth title] } - subject { FactoryGirl::NullObject.new(methods_to_respond_to) } + subject { FactoryBot::NullObject.new(methods_to_respond_to) } it "responds to the given methods" do methods_to_respond_to.each do |method_name| diff --git a/spec/factory_girl/registry_spec.rb b/spec/factory_bot/registry_spec.rb similarity index 95% rename from spec/factory_girl/registry_spec.rb rename to spec/factory_bot/registry_spec.rb index e3b3541af..c0aafae3d 100644 --- a/spec/factory_girl/registry_spec.rb +++ b/spec/factory_bot/registry_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' -describe FactoryGirl::Registry do +describe FactoryBot::Registry do let(:registered_object) { double("registered object") } let(:second_registered_object) { double("second registered object") } - subject { FactoryGirl::Registry.new("Great thing") } + subject { FactoryBot::Registry.new("Great thing") } it { should be_kind_of(Enumerable) } diff --git a/spec/factory_girl/sequence_spec.rb b/spec/factory_bot/sequence_spec.rb similarity index 76% rename from spec/factory_girl/sequence_spec.rb rename to spec/factory_bot/sequence_spec.rb index 82a90c62a..9af34c36a 100644 --- a/spec/factory_girl/sequence_spec.rb +++ b/spec/factory_bot/sequence_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe FactoryGirl::Sequence do +describe FactoryBot::Sequence do describe "a basic sequence" do let(:name) { :test } - subject { FactoryGirl::Sequence.new(name) { |n| "=#{n}" } } + subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } } its(:name) { should eq name } its(:names) { should eq [name] } @@ -16,7 +16,7 @@ end describe "a custom sequence" do - subject { FactoryGirl::Sequence.new(:name, "A") { |n| "=#{n}" } } + subject { FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}" } } its(:next) { should eq "=A" } describe "when incrementing" do @@ -26,7 +26,7 @@ end describe "a sequence with aliases using default value" do - subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } } + subject { FactoryBot::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } } its(:next) { should eq "=1" } its(:names) { should eq [:test, :alias, :other] } @@ -37,7 +37,7 @@ end describe "a sequence with custom value and aliases" do - subject { FactoryGirl::Sequence.new(:test, 3, aliases: [:alias, :other]) { |n| "=#{n}" } } + subject { FactoryBot::Sequence.new(:test, 3, aliases: [:alias, :other]) { |n| "=#{n}" } } its(:next) { should eq "=3" } describe "when incrementing" do @@ -47,7 +47,7 @@ end describe "a basic sequence without a block" do - subject { FactoryGirl::Sequence.new(:name) } + subject { FactoryBot::Sequence.new(:name) } its(:next) { should eq 1 } describe "when incrementing" do @@ -57,7 +57,7 @@ end describe "a custom sequence without a block" do - subject { FactoryGirl::Sequence.new(:name, "A") } + subject { FactoryBot::Sequence.new(:name, "A") } its(:next) { should eq "A" } describe "when incrementing" do @@ -67,7 +67,7 @@ end describe "iterating over items in an enumerator" do - subject { FactoryGirl::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" } } + subject { FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" } } it "navigates to the next items until no items remain" do expect(subject.next).to eq "=foo" @@ -77,7 +77,7 @@ end describe "a custom sequence and scope" do - subject { FactoryGirl::Sequence.new(:name, 'A') { |n| "=#{n}#{foo}" } } + subject { FactoryBot::Sequence.new(:name, 'A') { |n| "=#{n}#{foo}" } } let(:scope) { double("scope", foo: "attribute") } it 'increments within the correct scope' do diff --git a/spec/factory_girl/strategy/attributes_for_spec.rb b/spec/factory_bot/strategy/attributes_for_spec.rb similarity index 90% rename from spec/factory_girl/strategy/attributes_for_spec.rb rename to spec/factory_bot/strategy/attributes_for_spec.rb index 3a2f012a0..f670ffcca 100644 --- a/spec/factory_girl/strategy/attributes_for_spec.rb +++ b/spec/factory_bot/strategy/attributes_for_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FactoryGirl::Strategy::AttributesFor do +describe FactoryBot::Strategy::AttributesFor do let(:result) { { name: "John Doe", gender: "Male", admin: false } } let(:evaluation) { double("evaluation", hash: result) } diff --git a/spec/factory_girl/strategy/build_spec.rb b/spec/factory_bot/strategy/build_spec.rb similarity index 84% rename from spec/factory_girl/strategy/build_spec.rb rename to spec/factory_bot/strategy/build_spec.rb index 1b37b6ea6..19095211f 100644 --- a/spec/factory_girl/strategy/build_spec.rb +++ b/spec/factory_bot/strategy/build_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -describe FactoryGirl::Strategy::Build do +describe FactoryBot::Strategy::Build do it_should_behave_like "strategy with association support", :create it_should_behave_like "strategy with callbacks", :after_build it_should_behave_like "strategy with strategy: :build", :build diff --git a/spec/factory_girl/strategy/create_spec.rb b/spec/factory_bot/strategy/create_spec.rb similarity index 91% rename from spec/factory_girl/strategy/create_spec.rb rename to spec/factory_bot/strategy/create_spec.rb index 2ae1d4daa..b643378f5 100644 --- a/spec/factory_girl/strategy/create_spec.rb +++ b/spec/factory_bot/strategy/create_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FactoryGirl::Strategy::Create do +describe FactoryBot::Strategy::Create do it_should_behave_like "strategy with association support", :create it_should_behave_like "strategy with callbacks", :after_build, :before_create, :after_create diff --git a/spec/factory_girl/strategy/stub_spec.rb b/spec/factory_bot/strategy/stub_spec.rb similarity index 98% rename from spec/factory_girl/strategy/stub_spec.rb rename to spec/factory_bot/strategy/stub_spec.rb index 8d9c44d05..85b5584ff 100644 --- a/spec/factory_girl/strategy/stub_spec.rb +++ b/spec/factory_bot/strategy/stub_spec.rb @@ -19,7 +19,7 @@ end end -describe FactoryGirl::Strategy::Stub do +describe FactoryBot::Strategy::Stub do it_should_behave_like "strategy with association support", :build_stubbed it_should_behave_like "strategy with callbacks", :after_stub it_should_behave_like "strategy with strategy: :build", :build_stubbed diff --git a/spec/factory_girl/strategy_calculator_spec.rb b/spec/factory_bot/strategy_calculator_spec.rb similarity index 55% rename from spec/factory_girl/strategy_calculator_spec.rb rename to spec/factory_bot/strategy_calculator_spec.rb index f1ce62d82..1ee3bd8fb 100644 --- a/spec/factory_girl/strategy_calculator_spec.rb +++ b/spec/factory_bot/strategy_calculator_spec.rb @@ -1,12 +1,12 @@ require "spec_helper" -describe FactoryGirl::StrategyCalculator do +describe FactoryBot::StrategyCalculator do let(:strategy) do define_class("MyAwesomeClass") end context "when a class" do - subject { FactoryGirl::StrategyCalculator.new(strategy).strategy } + subject { FactoryBot::StrategyCalculator.new(strategy).strategy } it "returns the class passed" do expect(subject).to eq strategy @@ -15,14 +15,14 @@ context "when a symbol" do before do - allow(FactoryGirl).to receive(:strategy_by_name).and_return(strategy) + allow(FactoryBot).to receive(:strategy_by_name).and_return(strategy) end - subject { FactoryGirl::StrategyCalculator.new(:build).strategy } + subject { FactoryBot::StrategyCalculator.new(:build).strategy } it "finds the strategy by name" do subject - expect(FactoryGirl).to have_received(:strategy_by_name).with(:build) + expect(FactoryBot).to have_received(:strategy_by_name).with(:build) end it "returns the strategy found" do diff --git a/spec/factory_bot_spec.rb b/spec/factory_bot_spec.rb new file mode 100644 index 000000000..c9a3a2273 --- /dev/null +++ b/spec/factory_bot_spec.rb @@ -0,0 +1,22 @@ +require "spec_helper" + +describe FactoryBot do + let(:factory) { FactoryBot::Factory.new(:object) } + let(:sequence) { FactoryBot::Sequence.new(:email) } + let(:trait) { FactoryBot::Trait.new(:admin) } + + it "finds a registered factory" do + FactoryBot.register_factory(factory) + expect(FactoryBot.factory_by_name(factory.name)).to eq factory + end + + it "finds a registered sequence" do + FactoryBot.register_sequence(sequence) + expect(FactoryBot.sequence_by_name(sequence.name)).to eq sequence + end + + it "finds a registered trait" do + FactoryBot.register_trait(trait) + expect(FactoryBot.trait_by_name(trait.name)).to eq trait + end +end diff --git a/spec/factory_girl/attribute/sequence_spec.rb b/spec/factory_girl/attribute/sequence_spec.rb deleted file mode 100644 index 2af42e1cc..000000000 --- a/spec/factory_girl/attribute/sequence_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe FactoryGirl::Attribute::Sequence do - let(:sequence_name) { :name } - let(:name) { :first_name } - let(:sequence) { FactoryGirl::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } } - - subject { FactoryGirl::Attribute::Sequence.new(name, sequence_name, false) } - before { FactoryGirl.register_sequence(sequence) } - - its(:name) { should eq name } - - it "assigns the next value in the sequence" do - expect(subject.to_proc.call).to eq "Name 5" - end -end diff --git a/spec/factory_girl/attribute_spec.rb b/spec/factory_girl/attribute_spec.rb deleted file mode 100644 index 3b78c51bc..000000000 --- a/spec/factory_girl/attribute_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe FactoryGirl::Attribute do - let(:name) { "user" } - subject { FactoryGirl::Attribute.new(name, false) } - - its(:name) { should eq name.to_sym } - it { should_not be_association } - - it "raises an error when defining an attribute writer" do - error_message = %{factory_girl uses 'test value' syntax rather than 'test = value'} - expect { - FactoryGirl::Attribute.new('test=', false) - }.to raise_error(FactoryGirl::AttributeDefinitionError, error_message) - end -end diff --git a/spec/factory_girl/callback_spec.rb b/spec/factory_girl/callback_spec.rb deleted file mode 100644 index 37664bc25..000000000 --- a/spec/factory_girl/callback_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'spec_helper' - -describe FactoryGirl::Callback do - it "has a name" do - expect(FactoryGirl::Callback.new(:after_create, -> {}).name).to eq :after_create - end - - it "converts strings to symbols" do - expect(FactoryGirl::Callback.new("after_create", -> {}).name).to eq :after_create - end - - it "runs its block with no parameters" do - ran_with = nil - FactoryGirl::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two) - expect(ran_with).to eq [] - end - - it "runs its block with one parameter" do - ran_with = nil - FactoryGirl::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two) - expect(ran_with).to eq [:one] - end - - it "runs its block with two parameters" do - ran_with = nil - FactoryGirl::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two) - expect(ran_with).to eq [:one, :two] - end - - it "allows valid callback names to be assigned" do - FactoryGirl.callback_names.each do |callback_name| - expect { FactoryGirl::Callback.new(callback_name, -> {}) }. - to_not raise_error - end - end - - it "raises if an invalid callback name is assigned" do - expect { FactoryGirl::Callback.new(:magic_fairies, -> {}) }. - to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/) - end -end diff --git a/spec/factory_girl_spec.rb b/spec/factory_girl_spec.rb deleted file mode 100644 index 18498da7d..000000000 --- a/spec/factory_girl_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require "spec_helper" - -describe FactoryGirl do - let(:factory) { FactoryGirl::Factory.new(:object) } - let(:sequence) { FactoryGirl::Sequence.new(:email) } - let(:trait) { FactoryGirl::Trait.new(:admin) } - - it "finds a registered factory" do - FactoryGirl.register_factory(factory) - expect(FactoryGirl.factory_by_name(factory.name)).to eq factory - end - - it "finds a registered sequence" do - FactoryGirl.register_sequence(sequence) - expect(FactoryGirl.sequence_by_name(sequence.name)).to eq sequence - end - - it "finds a registered trait" do - FactoryGirl.register_trait(trait) - expect(FactoryGirl.trait_by_name(trait.name)).to eq trait - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ded547930..ba5c25617 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,7 @@ require "simplecov" -require 'factory_girl' +require 'factory_bot' require "timecop" Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) } @@ -23,7 +23,7 @@ config.include DeclarationMatchers config.before do - FactoryGirl.reload + FactoryBot.reload end config.after do diff --git a/spec/support/matchers/callback.rb b/spec/support/matchers/callback.rb index e7fdd6d81..214a823ed 100644 --- a/spec/support/matchers/callback.rb +++ b/spec/support/matchers/callback.rb @@ -1,6 +1,6 @@ RSpec::Matchers.define :have_callback do |callback_name| match do |instance| - instance.callbacks.include?(FactoryGirl::Callback.new(callback_name, @block)) + instance.callbacks.include?(FactoryBot::Callback.new(callback_name, @block)) end chain :with_block do |block| diff --git a/spec/support/matchers/declaration.rb b/spec/support/matchers/declaration.rb index 328117aa3..d9d9ad356 100644 --- a/spec/support/matchers/declaration.rb +++ b/spec/support/matchers/declaration.rb @@ -60,14 +60,14 @@ def failure_message def expected_declaration case @declaration_type - when :static then FactoryGirl::Declaration::Static.new(@name, @value, ignored?) - when :dynamic then FactoryGirl::Declaration::Dynamic.new(@name, ignored?, @value) - when :implicit then FactoryGirl::Declaration::Implicit.new(@name, @factory, ignored?) + when :static then FactoryBot::Declaration::Static.new(@name, @value, ignored?) + when :dynamic then FactoryBot::Declaration::Dynamic.new(@name, ignored?, @value) + when :implicit then FactoryBot::Declaration::Implicit.new(@name, @factory, ignored?) when :association if @options - FactoryGirl::Declaration::Association.new(@name, options) + FactoryBot::Declaration::Association.new(@name, options) else - FactoryGirl::Declaration::Association.new(@name) + FactoryBot::Declaration::Association.new(@name) end end end diff --git a/spec/support/shared_examples/strategy.rb b/spec/support/shared_examples/strategy.rb index 3260938ab..8c34882ad 100644 --- a/spec/support/shared_examples/strategy.rb +++ b/spec/support/shared_examples/strategy.rb @@ -1,14 +1,14 @@ shared_examples_for "strategy without association support" do let(:factory) { double("associate_factory") } - let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) } + let(:attribute) { FactoryBot::Attribute::Association.new(:user, :user, {}) } def association_named(name, overrides) - runner = FactoryGirl::FactoryRunner.new(name, :build, [overrides]) + runner = FactoryBot::FactoryRunner.new(name, :build, [overrides]) subject.association(runner) end before do - allow(FactoryGirl).to receive(:factory_by_name).and_return factory + allow(FactoryBot).to receive(:factory_by_name).and_return factory allow(factory).to receive(:compile) allow(factory).to receive(:run) end @@ -18,53 +18,53 @@ def association_named(name, overrides) end end -shared_examples_for "strategy with association support" do |factory_girl_strategy_name| +shared_examples_for "strategy with association support" do |factory_bot_strategy_name| let(:factory) { double("associate_factory") } def association_named(name, strategy, overrides) - runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides]) + runner = FactoryBot::FactoryRunner.new(name, strategy, [overrides]) subject.association(runner) end before do - allow(FactoryGirl).to receive(:factory_by_name).and_return factory + allow(FactoryBot).to receive(:factory_by_name).and_return factory allow(factory).to receive(:compile) allow(factory).to receive(:run) end it "runs the factory with the correct overrides" do - association_named(:author, factory_girl_strategy_name, great: "value") - expect(factory).to have_received(:run).with(factory_girl_strategy_name, great: "value") + association_named(:author, factory_bot_strategy_name, great: "value") + expect(factory).to have_received(:run).with(factory_bot_strategy_name, great: "value") end it "finds the factory with the correct factory name" do - association_named(:author, factory_girl_strategy_name, great: "value") - expect(FactoryGirl).to have_received(:factory_by_name).with(:author) + association_named(:author, factory_bot_strategy_name, great: "value") + expect(FactoryBot).to have_received(:factory_by_name).with(:author) end end -shared_examples_for "strategy with strategy: :build" do |factory_girl_strategy_name| +shared_examples_for "strategy with strategy: :build" do |factory_bot_strategy_name| let(:factory) { double("associate_factory") } def association_named(name, overrides) - runner = FactoryGirl::FactoryRunner.new(name, overrides[:strategy], [overrides.except(:strategy)]) + runner = FactoryBot::FactoryRunner.new(name, overrides[:strategy], [overrides.except(:strategy)]) subject.association(runner) end before do - allow(FactoryGirl).to receive(:factory_by_name).and_return factory + allow(FactoryBot).to receive(:factory_by_name).and_return factory allow(factory).to receive(:compile) allow(factory).to receive(:run) end it "runs the factory with the correct overrides" do association_named(:author, strategy: :build, great: "value") - expect(factory).to have_received(:run).with(factory_girl_strategy_name, { great: "value" }) + expect(factory).to have_received(:run).with(factory_bot_strategy_name, { great: "value" }) end it "finds the factory with the correct factory name" do association_named(:author, strategy: :build, great: "value") - expect(FactoryGirl).to have_received(:factory_by_name).with(:author) + expect(FactoryBot).to have_received(:factory_by_name).with(:author) end end