pax_global_header00006660000000000000000000000064135452366500014523gustar00rootroot0000000000000052 comment=c03aeb7e2993d820ab6024150461942767923860 factory_bot_rails-5.1.1/000077500000000000000000000000001354523665000152345ustar00rootroot00000000000000factory_bot_rails-5.1.1/.gitignore000066400000000000000000000001751354523665000172270ustar00rootroot00000000000000*.swp .bundle gemfiles/*.lock coverage factory_girl_rails-*.gem pkg rdoc test.db tmp .rubocop-https* factory_bot_rails-*.gem factory_bot_rails-5.1.1/.hound.yml000066400000000000000000000000451354523665000171510ustar00rootroot00000000000000rubocop: config_file: .rubocop.yml factory_bot_rails-5.1.1/.rspec000066400000000000000000000000601354523665000163450ustar00rootroot00000000000000--color --format progress --require spec_helper factory_bot_rails-5.1.1/.rubocop.yml000066400000000000000000000003451354523665000175100ustar00rootroot00000000000000inherit_from: - https://raw.githubusercontent.com/thoughtbot/guides/master/style/ruby/.rubocop.yml AllCops: TargetRubyVersion: 2.3 Exclude: - "tmp/**/*" - "gemfiles/*" Style/EmptyMethod: EnforcedStyle: expanded factory_bot_rails-5.1.1/.travis.yml000066400000000000000000000011771354523665000173530ustar00rootroot00000000000000language: ruby env: - ARUBA_TIMEOUT=240 before_install: - gem update --system - rvm @default,@global do gem uninstall bundler -v 2.0.2 -x cache: bundler rvm: - 2.3 - 2.4 - 2.5 - 2.6 - ruby-head branches: only: - master install: - "bundle install --retry 3 --jobs 8" gemfile: - gemfiles/rails4.2.gemfile - gemfiles/rails5.0.gemfile - gemfiles/rails5.1.gemfile - gemfiles/rails5.2.gemfile - gemfiles/rails6.0.gemfile matrix: fast_finish: true allow_failures: - rvm: ruby-head exclude: - rvm: 2.3 gemfile: gemfiles/rails6.0.gemfile - rvm: 2.4 gemfile: gemfiles/rails6.0.gemfile factory_bot_rails-5.1.1/Appraisals000066400000000000000000000017651354523665000172670ustar00rootroot00000000000000# These are the versions of Rails we want to test against. appraise "rails4.2" do gem "rails", "~> 4.2.11" gem "sqlite3", "~> 1.3.6" end appraise "rails5.0" do gem "activerecord", "~> 5.0.7" gem "railties", "~> 5.0.7" gem "sqlite3", "~> 1.3.6" gem "actionmailer", "~> 5.0.7" gem "sass-rails" end appraise "rails5.1" do gem "activerecord", "~> 5.1.7" gem "railties", "~> 5.1.7" gem "actionmailer", "~> 5.1.7" gem "sass-rails" end appraise "rails5.2" do gem "activerecord", "~> 5.2.3" gem "railties", "~> 5.2.3" gem "actionmailer", "~> 5.2.3" gem "bootsnap" gem "activestorage", "~> 5.2.3" gem "sass-rails" end appraise "rails6.0" do gem "rails", "~> 6.0.0" gem "bootsnap" gem "byebug" gem "capybara" gem "jbuilder" gem "listen", "~> 3.1.5" gem "puma", "~> 3.11" gem "sass-rails", "~> 5" gem "selenium-webdriver" gem "spring" gem "spring-watcher-listen" gem "turbolinks" gem "web-console", group: :development gem "webdrivers" gem "webpacker" end factory_bot_rails-5.1.1/CODE_OF_CONDUCT.md000066400000000000000000000002511354523665000200310ustar00rootroot00000000000000# Code of conduct By participating in this project, you agree to abide by the [thoughtbot code of conduct][1]. [1]: https://thoughtbot.com/open-source-code-of-conduct factory_bot_rails-5.1.1/CONTRIBUTING.md000066400000000000000000000021121354523665000174610ustar00rootroot00000000000000# Contributing We love pull requests from everyone. By participating in this project, you agree to abide by the thoughtbot [code of conduct]. [code of conduct]: https://thoughtbot.com/open-source-code-of-conduct Fork, then clone the repo: git clone git@github.com:your-username/factory_bot_rails.git Set up your machine: ./bin/setup Make sure the tests pass: rake Make your change. Add tests for your change. Make the tests pass: rake Push to your fork and [submit a pull request][pr]. [pr]: https://github.com/thoughtbot/factory_bot_rails/compare/ At this point you're waiting on us. We like to at least comment on pull requests within three business days (and, typically, one business day). We may suggest some changes or improvements or alternatives. Some things that will increase the chance that your pull request is accepted: * Write tests. * Follow our [style guide][style]. * Write a [good commit message][commit]. [style]: https://github.com/thoughtbot/guides/tree/master/style [commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html factory_bot_rails-5.1.1/Gemfile000066400000000000000000000007171354523665000165340ustar00rootroot00000000000000source "https://rubygems.org" gemspec name: "factory_bot_rails" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", platforms: :ruby gem "rubocop", "0.54", require: false factory_bot_rails-5.1.1/Gemfile.lock000066400000000000000000000105201354523665000174540ustar00rootroot00000000000000PATH remote: . specs: factory_bot_rails (5.1.1) factory_bot (~> 5.1.0) railties (>= 4.2.0) GEM remote: https://rubygems.org/ specs: actionpack (5.2.3) actionview (= 5.2.3) activesupport (= 5.2.3) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (5.2.3) activesupport (= 5.2.3) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) activemodel (5.2.3) activesupport (= 5.2.3) activerecord (5.2.3) activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) activerecord-jdbc-adapter (52.2-java) activerecord (~> 5.2.0) activerecord-jdbcsqlite3-adapter (52.2-java) activerecord-jdbc-adapter (= 52.2) jdbc-sqlite3 (~> 3.8, < 3.30) activesupport (5.2.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) appraisal (2.2.0) bundler rake thor (>= 0.14.0) arel (9.0.0) aruba (0.14.9) childprocess (>= 0.6.3, < 1.1.0) contracts (~> 0.9) cucumber (>= 1.3.19) ffi (~> 1.9) rspec-expectations (>= 2.99) thor (~> 0.19) ast (2.4.0) builder (3.2.3) childprocess (1.0.1) rake (< 13.0) coffee-rails (4.2.2) coffee-script (>= 2.2.0) railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.12.2) concurrent-ruby (1.1.5) contracts (0.16.0) crass (1.0.4) cucumber (1.3.19) 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.3) erubi (1.8.0) execjs (2.7.0) factory_bot (5.1.1) activesupport (>= 4.2.0) ffi (1.10.0) ffi (1.10.0-java) gherkin (2.12.2) multi_json (~> 1.3) gherkin (2.12.2-java) multi_json (~> 1.3) i18n (1.6.0) concurrent-ruby (~> 1.0) jdbc-sqlite3 (3.20.1) jquery-rails (4.3.3) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) jruby-openssl (0.10.2-java) loofah (2.2.3) crass (~> 1.0.2) nokogiri (>= 1.5.9) method_source (0.9.2) mini_portile2 (2.4.0) minitest (5.11.3) multi_json (1.13.1) multi_test (0.1.2) nokogiri (1.10.2) mini_portile2 (~> 2.4.0) nokogiri (1.10.2-java) parallel (1.17.0) parser (2.6.2.1) ast (~> 2.4.0) power_assert (1.1.4) powerpack (0.1.2) rack (2.0.7) rack-test (1.1.0) rack (>= 1.0, < 3) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) railties (5.2.3) actionpack (= 5.2.3) activesupport (= 5.2.3) method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rainbow (3.0.0) rake (12.3.2) rspec-core (3.8.0) rspec-support (~> 3.8.0) rspec-expectations (3.8.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-mocks (3.8.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-rails (3.8.2) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) rspec-core (~> 3.8.0) rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-support (~> 3.8.0) rspec-support (3.8.0) rubocop (0.54.0) parallel (~> 1.10) parser (>= 2.5) powerpack (~> 0.1) rainbow (>= 2.2.2, < 4.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.10.0) sqlite3 (1.4.0) test-unit (3.3.2) power_assert therubyrhino (2.0.4) therubyrhino_jar (>= 1.7.3) therubyrhino_jar (1.7.6) thor (0.20.3) thread_safe (0.3.6) thread_safe (0.3.6-java) tzinfo (1.2.5) thread_safe (~> 0.1) uglifier (4.1.20) execjs (>= 0.3.0, < 3) unicode-display_width (1.5.0) PLATFORMS java ruby DEPENDENCIES activerecord-jdbcsqlite3-adapter appraisal aruba coffee-rails cucumber (= 1.3.19) factory_bot_rails! jdbc-sqlite3 jquery-rails jruby-openssl rake rspec-rails rubocop (= 0.54) sqlite3 test-unit therubyrhino uglifier BUNDLED WITH 2.0.2 factory_bot_rails-5.1.1/LICENSE000066400000000000000000000020701354523665000162400ustar00rootroot00000000000000Copyright (c) 2008-2019 Joe Ferris and thoughtbot, inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. factory_bot_rails-5.1.1/NEWS.md000066400000000000000000000040031354523665000163270ustar00rootroot00000000000000factory_bot_rails versioning is synced with factory_bot releases. For this reason there might not be any notable changes in new versions of this project. # NEWS ## 5.1.1 (September 24, 2019) * Fixed: Ensure definitions do not load before I18n is initialized ## 5.1.0 (September 24, 2019) * Changed: factory_bot dependency to ~> 5.1.0 ## 5.0.2 (April 14, 2019) * Bugfix: Reload factory\_bot whenever the application changes to avoid holding onto stale object references * Bugfix: Avoid watching project root when no factory definitions exist ## 5.0.1 (February 9, 2019) * Bugfix: Avoid watching files and directories that don't exist (to avoid a file watching bug in Rails https://github.com/rails/rails/issues/32700) ## 5.0.0 (February 1, 2019) * Added: calling reload! in the Rails console will reload any factory definition files that have changed * Added: support for custom generator templates * Added: `definition_file_paths` configuration option, making it easier to place factories in custom locations * Changed: namespaced models are now generated inside a directory matching the namespace * Changed: added newline between factories generated into the same file * Removed: support for EOL version of Ruby and Rails ## 4.11.1 (September 7, 2018) * Update generator to use dynamic attributes instead of deprecated static attributes ## 4.11.0 (August 16, 2018) * No notable changes ## 4.10.0 (May 25, 2018) * No notable changes ## 4.8.2 (October 20, 2017) * Rename factory\_girl\_rails to factory\_bot\_rails ## 4.7.0 (April 1, 2016) * No notable changes ## 4.6.0 (February 1, 2016) * No notable changes ## 4.5.0 (October 17, 2014) * Improved README ## 4.4.1 (February 26, 2014) * Support Spring ## 4.2.1 (February 8, 2013) * Fix bug when configuring FG and RSpec fixture directory * Remove debugging * Require factory\_girl\_rails explicitly in generator ## 4.2.0 (January 25, 2013) * Add appraisal and get test suite working reliably with turn gem * Support MiniTest * Allow a custom directory for factories to be specified factory_bot_rails-5.1.1/README.md000066400000000000000000000137121354523665000165170ustar00rootroot00000000000000# factory_bot_rails [![Build Status][ci-image]][ci] [![Code Climate][grade-image]][grade] [![Gem Version][version-image]][version] [![Reviewed by Hound][hound-image]][hound] [factory_bot][fb] 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. ### Transitioning from factory\_girl\_rails? Check out the [guide](https://github.com/thoughtbot/factory_bot/blob/4-9-0-stable/UPGRADE_FROM_FACTORY_GIRL.md). ## Rails factory\_bot\_rails provides Rails integration for [factory_bot][fb]. Supported Rails versions are listed in [`Appraisals`](Appraisals). Supported Ruby versions are listed in [`.travis.yml`](.travis.yml). ## Download Github: http://github.com/thoughtbot/factory_bot_rails Gem: $ gem install factory_bot_rails ## Configuration Add `factory_bot_rails` to your Gemfile in both the test and development groups: ```ruby group :development, :test do gem 'factory_bot_rails' end ``` You may want to configure your test suite to include factory\_bot methods; see [configuration](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#configure-your-test-suite). ### Automatic Factory Definition Loading By default, factory\_bot\_rails will automatically load factories defined in the following locations, relative to the root of the Rails project: ``` factories.rb test/factories.rb spec/factories.rb factories/*.rb test/factories/*.rb spec/factories/*.rb ``` You can configure by adding the following to `config/application.rb` or the appropriate environment configuration in `config/environments`: ```ruby config.factory_bot.definition_file_paths = ["custom/factories"] ``` This will cause factory\_bot\_rails to automatically load factories in `custom/factories.rb` and `custom/factories/*.rb`. It is possible to use this setting to share factories from a gem: ```rb begin require 'factory_bot_rails' rescue LoadError end class MyEngine < ::Rails::Engine config.factory_bot.definition_file_paths += [File.expand_path('../factories', __FILE__)] if defined?(FactoryBotRails) end ``` You can also disable automatic factory definition loading entirely by using an empty array: ```rb config.factory_bot.definition_file_paths = [] ``` ### Generators Including factory\_bot\_rails in the development group of your Gemfile will cause Rails to generate factories instead of fixtures. If you want to disable this feature, you can either move factory\_bot\_rails out of the development group of your Gemfile, or add the following configuration: ```ruby config.generators do |g| g.factory_bot false end ``` If fixture replacement is enabled and you already have a `test/factories.rb` file (or `spec/factories.rb` if using rspec_rails), generated factories will be inserted at the top of the existing file. Otherwise, factories will be generated in the `test/factories` directory (`spec/factories` if using rspec_rails), in a file matching the name of the table (e.g. `test/factories/users.rb`). To generate factories in a different directory, you can use the following configuration: ```ruby config.generators do |g| g.factory_bot dir: 'custom/dir/for/factories' end ``` Note that factory\_bot\_rails will not automatically load files in custom locations unless you add them to `config.factory_bot.definition_file_paths` as well. The suffix option allows you to customize the name of the generated file with a suffix: ```ruby config.generators do |g| g.factory_bot suffix: "factory" end ``` This will generate `test/factories/users_factory.rb` instead of `test/factories/users.rb`. For even more customization, use the `filename_proc` option: ```ruby config.generators do |g| g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" } end ``` To override the [default factory template][], define your own template in `lib/templates/factory_bot/model/factories.erb`. This template will have access to any methods available in `FactoryBot::Generators::ModelGenerator`. Note that factory\_bot\_rails will only use this custom template if you are generating each factory in a separate file; it will have no effect if you are generating all of your factories in `test/factories.rb` or `spec/factories.rb`. [default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/master/lib/generators/factory_bot/model/templates/factories.erb ## Contributing Please see [CONTRIBUTING.md](CONTRIBUTING.md). factory_bot_rails 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_bot_rails/graphs/contributors). ## License factory_bot_rails is Copyright © 2008-2019 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the [LICENSE](LICENSE) file. ## About thoughtbot ![thoughtbot](https://thoughtbot.com/logo.png) factory_bot_rails is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc. We are passionate about open source software. See [our other projects][community]. We are [available for hire][hire]. [fb]: https://github.com/thoughtbot/factory_bot [ci]: http://travis-ci.org/thoughtbot/factory_bot_rails?branch=master [ci-image]: https://secure.travis-ci.org/thoughtbot/factory_bot_rails.svg [grade]: https://codeclimate.com/github/thoughtbot/factory_bot_rails [grade-image]: https://codeclimate.com/github/thoughtbot/factory_bot_rails.svg [community]: https://thoughtbot.com/community?utm_source=github [hire]: https://thoughtbot.com/hire-us?utm_source=github [version-image]: https://badge.fury.io/rb/factory_bot_rails.svg [version]: https://badge.fury.io/rb/factory_bot_rails [hound-image]: https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg [hound]: https://houndci.com factory_bot_rails-5.1.1/RELEASING.md000066400000000000000000000017571354523665000171010ustar00rootroot00000000000000# Releasing 1. Update the version in the gemspec (and the factory\_bot version, if necessary) and run `bundle install` 1. Update `NEWS.md` to reflect the changes since last release. 1. Commit changes. There shouldn't be code changes, and thus CI doesn't need to run, so you can add "[ci skip]" to the commit message. 1. Tag the release: `git tag -s vVERSION` - We recommend the [_quick guide on how to sign a release_] from git ready. 1. Push changes: `git push && git push --tags` 1. Build and publish: ```bash gem build factory_bot_rails.gemspec gem push factory_bot_rails-VERSION.gem ``` 1. Add a new GitHub release using the recent `NEWS.md` as the content. Sample URL: https://github.com/thoughtbot/factory_bot_rails/releases/new?tag=vVERSION 1. Announce the new release, making sure to say "thank you" to the contributors who helped shape this version! [_quick guide on how to sign a release_]: http://gitready.com/advanced/2014/11/02/gpg-sign-releases.html factory_bot_rails-5.1.1/Rakefile000066400000000000000000000005661354523665000167100ustar00rootroot00000000000000require "bundler/setup" require "cucumber/rake/task" require "rspec/core/rake_task" Bundler::GemHelper.install_tasks name: "factory_bot_rails" Cucumber::Rake::Task.new(:cucumber) do |t| t.fork = true t.cucumber_opts = ["--format", (ENV["CUCUMBER_FORMAT"] || "progress")] end RSpec::Core::RakeTask.new(:spec) desc "Run the test suite" task default: %w(spec cucumber) factory_bot_rails-5.1.1/bin/000077500000000000000000000000001354523665000160045ustar00rootroot00000000000000factory_bot_rails-5.1.1/bin/setup000077500000000000000000000005741354523665000171000ustar00rootroot00000000000000#!/usr/bin/env sh # Run this script immediately after cloning the codebase. # Exit if any subcommand fails set -e # Set up Ruby dependencies via Bundler bundle install # Add binstubs to PATH in ~/.zshenv like this: # export PATH=".git/safe/../../bin:$PATH" mkdir -p .git/safe # Set up Appraisal to help us test against multiple Rails versions bundle exec appraisal install factory_bot_rails-5.1.1/factory_bot_rails.gemspec000066400000000000000000000013561354523665000223130ustar00rootroot00000000000000Gem::Specification.new do |s| s.name = "factory_bot_rails" s.version = "5.1.1" s.authors = ["Joe Ferris"] s.email = "jferris@thoughtbot.com" s.homepage = "https://github.com/thoughtbot/factory_bot_rails" s.summary = "factory_bot_rails provides integration between "\ "factory_bot and rails 4.2 or newer" s.description = "factory_bot_rails provides integration between "\ "factory_bot and rails 4.2 or newer" s.files = Dir["lib/**/*"] + %w[CONTRIBUTING.md LICENSE NEWS.md README.md] s.require_paths = ["lib"] s.executables = [] s.license = "MIT" s.add_runtime_dependency("factory_bot", "~> 5.1.0") s.add_runtime_dependency("railties", ">= 4.2.0") end factory_bot_rails-5.1.1/features/000077500000000000000000000000001354523665000170525ustar00rootroot00000000000000factory_bot_rails-5.1.1/features/fixture_replacement_config.feature000066400000000000000000000134411354523665000260240ustar00rootroot00000000000000Feature: In order to not have to manually configure Factory Bot as the Rails testing fixture replacement by using the --fixture-replacement=factory_bot option I would like the Factory Bot Rails gem to configure Factory Bot as the fixture replacement. Background: Given I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template` And I cd to "testapp" And I add "factory_bot_rails" from this project as a dependency Scenario: Using Factory Bot and Factory Bot Rails with Test Unit generates a factory file and does not generate a fixture file And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | test/factories/users.rb | And the following files should not exist: | test/fixtures/users.yml | Scenario: Using Factory Bot and Factory Bot Rails with RSpec should generate a factory file When I add "rspec-rails" as a dependency And I configure the factories as: """ config.generators do |g| g.test_framework :rspec, fixture: true g.fixture_replacement :factory_bot end """ And I run `bundle install` with a clean environment Then the output should contain "rspec-rails" And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | spec/factories/users.rb | And the following files should not exist: | spec/fixtures/users.yml | Scenario: Using Factory Bot and Factory Bot Rails with RSpec and suffix configuration should generate a factory file with suffix When I add "rspec-rails" as a dependency And I configure the factories as: """ config.generators do |g| g.test_framework :rspec, fixture: true g.fixture_replacement :factory_bot, suffix: 'factory' end """ And I run `bundle install` with a clean environment Then the output should contain "rspec-rails" And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | spec/factories/users_factory.rb | And the following files should not exist: | spec/fixtures/users.yml | Scenario: Using Factory Bot and Factory Bot Rails does not override a manually-configured factories directory using RSpec When I add "rspec-rails" as a dependency And I configure the factories directory as "custom/dir" And I run `bundle install` with a clean environment Then the output should contain "rspec-rails" And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should not exist: | test/factories/users.rb | | spec/factories/users.rb | But the following files should exist: | custom/dir/users.rb | Scenario: Using Factory Bot and Factory Bot Rails does not override a manually-configured factories directory using Test::Unit When I configure the factories directory as "custom/dir" And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should not exist: | test/factories/users.rb | | spec/factories/users.rb | But the following files should exist: | custom/dir/users.rb | Scenario: Using Factory Bot Rails with MiniTest should generate a factory file When I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | test/factories/users.rb | But the following files should not exist: | spec/fixtures/users.yml | Scenario: Using Factory Bot Rails with MiniTest and a custom directory should generate a factory file When I configure the factories directory as "custom/dir" And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | custom/dir/users.rb | But the following files should not exist: | spec/fixtures/users.yml | Scenario: Disable Factory Bot generator When I configure the factories as: """ config.generators do |g| g.factory_bot false end """ And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should not exist: | test/factories/users.rb | | spec/factories/users.rb | Scenario: Use a suffix with the Factory Bot generator When I add "rspec-rails" as a dependency When I configure the factories as: """ config.generators do |g| g.factory_bot suffix: 'suffix' end """ And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | spec/factories/users_suffix.rb | Then the following files should not exist: | spec/factories/users.rb | Scenario: Use a filename_proc with the Factory Bot generator When I add "rspec-rails" as a dependency When I configure the factories as: """ config.generators do |g| g.factory_bot filename_proc: Proc.new { |tb| "prefix_#{tb.singularize}_suffix" } end """ And I run `bundle install` with a clean environment And I run `bundle exec rails generate model User name:string` with a clean environment Then the following files should exist: | spec/factories/prefix_user_suffix.rb | Then the following files should not exist: | spec/factories/users.rb | factory_bot_rails-5.1.1/features/generators.feature000066400000000000000000000052121354523665000226000ustar00rootroot00000000000000Feature: In order to easily generate factory files instead of fixture files when generating models As a user of Rails and Factory Bot I would like to use factory_bot_rails generators. Background: Given I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template` And I cd to "testapp" And I add "factory_bot_rails" from this project as a dependency And I run `bundle install` with a clean environment Scenario: The factory_bot_rails generators create a factory file for each model if there is not a factories.rb file When I run `bundle exec rails generate model User name:string age:integer` with a clean environment And I run `bundle exec rails generate model Namespaced::User name:string` with a clean environment Then the output should contain "test/factories/users.rb" And the output should contain "test/factories/namespaced/users.rb" And the file "test/factories/users.rb" should contain exactly: """ FactoryBot.define do factory :user do name { "MyString" } age { 1 } end end """ And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do" Scenario: The factory_bot_rails generators add a factory in the correct spot When I write to "test/factories.rb" with: """ FactoryBot.define do end """ And I run `bundle exec rails generate model User name:string` with a clean environment And I run `bundle exec rails generate model Robot name:string` with a clean environment Then the file "test/factories.rb" should contain exactly: """ FactoryBot.define do factory :robot do name { "MyString" } end factory :user do name { "MyString" } end end """ Scenario: The factory_bot_rails generators does not create a factory file for each model if there is a factories.rb file in the test directory When I write to "test/factories.rb" with: """ FactoryBot.define do end """ And I run `bundle exec rails generate model User name:string` with a clean environment Then the file "test/factories/users.rb" should not contain "factory :user do" Scenario: The factory_bot_rails generators use a custom template When I write to "lib/templates/factory_bot/model/factories.erb" with: """ <%= "Custom factory definition" %> """ And I run `bundle exec rails generate model User` with a clean environment Then the file "test/factories/users.rb" should contain exactly: """ Custom factory definition """ factory_bot_rails-5.1.1/features/load_definitions.feature000066400000000000000000000143611354523665000237460ustar00rootroot00000000000000Feature: automatically load factory definitions Background: When I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template` And I cd to "testapp" And I add "factory_bot_rails" from this project as a dependency And I add "test-unit" as a dependency And I run `bundle install` with a clean environment And I write to "db/migrate/1_create_users.rb" with: """ migration_class = if ActiveRecord::Migration.respond_to?(:[]) ActiveRecord::Migration[4.2] else ActiveRecord::Migration end class CreateUsers < migration_class def self.up create_table :users do |t| t.string :name end end end """ When I run `bundle exec rake db:migrate` with a clean environment And I write to "app/models/user.rb" with: """ class User < ActiveRecord::Base end """ Scenario: generate a Rails application and use factory definitions When I write to "test/factories.rb" with: """ FactoryBot.define do factory :user do name { "Frank" } end end """ When I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory" do user = FactoryBot.create(:user) assert_equal 'Frank', user.name end end """ When I run `bundle exec rake test` with a clean environment Then the output should contain "1 assertions, 0 failures, 0 errors" Scenario: use custom definition file paths When I configure the factories as: """ config.factory_bot.definition_file_paths = ["custom_factories_path"] """ When I write to "custom_factories_path.rb" with: """ FactoryBot.define do factory :user do name { "Frank" } end end """ When I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory" do user = FactoryBot.create(:user) assert_equal 'Frank', user.name end end """ When I run `bundle exec rake test` with a clean environment Then the output should contain "1 assertions, 0 failures, 0 errors" Scenario: use 3rd-party factories with configured definition file paths When I append to "config/application.rb" with: """ require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) """ When I write to "lib/some_railtie/railties.rb" with: """ module SomeRailtie class Railtie < ::Rails::Engine config.factory_bot.definition_file_paths << File.expand_path('../factories', __FILE__) end end """ When I write to "lib/some_railtie/factories.rb" with: """ FactoryBot.define do factory :factory_from_some_railtie, class: 'User' do name { 'Artem' } end end """ When I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory of some_railtie" do user = FactoryBot.create(:factory_from_some_railtie) assert_equal 'Artem', user.name end end """ When I run `bundle exec rake test` with a clean environment Then the output should contain "1 assertions, 0 failures, 0 errors" Scenario: use 3rd-party factories with an initializer and without any user-defined factories When I append to "config/application.rb" with: """ require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) """ When I write to "lib/some_railtie/railties.rb" with: """ module SomeRailtie class Railtie < ::Rails::Engine initializer "some_railtie.factories", :after => "factory_bot.set_factory_paths" do FactoryBot.definition_file_paths << File.expand_path('../factories', __FILE__) end end end """ When I write to "lib/some_railtie/factories.rb" with: """ FactoryBot.define do factory :factory_from_some_railtie, class: 'User' do name { 'Artem' } end end """ When I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory of some_railtie" do railtie_user = FactoryBot.create(:factory_from_some_railtie) assert_equal 'Artem', railtie_user.name end end """ When I run `bundle exec rake test` with a clean environment Then the output should contain "1 assertions, 0 failures, 0 errors" Scenario: use 3rd-party factories with an initializer together with a user-defined factory When I append to "config/application.rb" with: """ require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) """ When I write to "lib/some_railtie/railties.rb" with: """ module SomeRailtie class Railtie < ::Rails::Engine initializer "some_railtie.factories", :after => "factory_bot.set_factory_paths" do FactoryBot.definition_file_paths << File.expand_path('../factories', __FILE__) end end end """ When I write to "lib/some_railtie/factories.rb" with: """ FactoryBot.define do factory :factory_from_some_railtie, class: 'User' do name { 'Artem' } end end """ When I write to "test/factories.rb" with: """ FactoryBot.define do factory :user do name { "Frank" } end end """ When I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory of some_railtie" do railtie_user = FactoryBot.create(:factory_from_some_railtie) assert_equal 'Artem', railtie_user.name user = FactoryBot.create(:user) assert_equal 'Frank', user.name end end """ When I run `bundle exec rake test` with a clean environment Then the output should contain "2 assertions, 0 failures, 0 errors" factory_bot_rails-5.1.1/features/reloading.feature000066400000000000000000000066261354523665000224050ustar00rootroot00000000000000Feature: When using factory_bot_rails together with Spring I want changes to my application to trigger the factory_bot_rails reloader So that factory_bot_rails doesn't hold onto stale class references Scenario: Editing a model without editing the factory When I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template` And I cd to "testapp" And I add "factory_bot_rails" from this project as a dependency And I add "test-unit" as a dependency And I run `bundle install` with a clean environment And I write to "db/migrate/1_create_users.rb" with: """ migration_class = if ActiveRecord::Migration.respond_to?(:[]) ActiveRecord::Migration[4.2] else ActiveRecord::Migration end class CreateUsers < migration_class def self.up create_table :users do |t| t.string :name end end end """ And I run `bundle exec rake db:migrate` with a clean environment And I write to "app/models/user.rb" with: """ class User < ActiveRecord::Base end """ And I write to "test/factories.rb" with: """ FactoryBot.define do factory :author, class: User do name { "Frank" } end end """ And I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory" do author = FactoryBot.create(:author) assert_equal author.class.object_id, User.object_id end end """ And I run `bundle binstubs bundler rake spring --force` with a clean environment And I run `bin/spring binstub --all` with a clean environment And I run `bin/rake test` with Spring enabled And I append to "app/models/user.rb" with: """ # User model edited """ And I run `bin/rake test` with Spring enabled And I run `spring stop` with a clean environment Then the output should contain "1 runs, 1 assertions" And the output should not contain "Failure:" Scenario: Initializing the reloader with I18n support When I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template` And I cd to "testapp" And I add "factory_bot_rails" from this project as a dependency And I add "test-unit" as a dependency And I run `bundle install` with a clean environment And I run `bundle exec rake db:migrate` with a clean environment And I write to "app/models/user.rb" with: """ class User TRANSLATION = I18n.translate("translation_key") end """ And I write to "config/locales/en.yml" with: """ en: translation_key: "translation_value" """ And I write to "test/factories.rb" with: """ FactoryBot.define do factory :user do User::TRANSLATION end end """ And I write to "test/unit/user_test.rb" with: """ require 'test_helper' class UserTest < ActiveSupport::TestCase test "use factory" do user = FactoryBot.build(:user) assert_equal "translation_value", User::TRANSLATION end end """ And I run `bundle exec rake test` with a clean environment Then the output should contain "1 runs, 1 assertions" And the output should not contain "Failure:" factory_bot_rails-5.1.1/features/step_definitions/000077500000000000000000000000001354523665000224205ustar00rootroot00000000000000factory_bot_rails-5.1.1/features/step_definitions/appraisal.rb000066400000000000000000000005601354523665000247220ustar00rootroot00000000000000When /^I run `([^"]+)` with a clean environment$/ do |command| step <<~STEP I successfully run `ruby -e 'system({"BUNDLE_GEMFILE" => nil, "DISABLE_SPRING" => "true"}, "#{command}")'` STEP end When /^I run `([^"]+)` with Spring enabled$/ do |command| step <<~STEP I successfully run `ruby -e 'system({"BUNDLE_GEMFILE" => nil}, "#{command}")'` STEP end factory_bot_rails-5.1.1/features/step_definitions/rails_steps.rb000066400000000000000000000021361354523665000252770ustar00rootroot00000000000000When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name| append_to_file("Gemfile", %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n}) end When /^I add "([^"]+)" as a dependency$/ do |gem_name| append_to_file("Gemfile", %{gem "#{gem_name}"\n}) end When /^I print out "([^"]*)"$/ do |path| in_current_dir do File.open(path, "r") do |f| puts f.inspect end end end When /^I configure the factories as:$/ do |string| append_to_file File.join("config", "application.rb"), <<~RUBY class Testapp::Application #{string} end RUBY end When /^I configure the factories directory as "([^"]+)"$/ do |factory_dir| append_to_file File.join("config", "application.rb"), <<~RUBY class Testapp::Application config.generators do |g| g.fixture_replacement :factory_bot, :dir => "#{factory_dir}" end end RUBY end When /^I comment out gem "([^"]*)" from my Gemfile$/ do |gem_name| in_current_dir do content = File.read("Gemfile") File.open("Gemfile", "w") do |f| f.write content.sub(/gem ['"]#{gem_name}/, '#\1') end end end factory_bot_rails-5.1.1/features/support/000077500000000000000000000000001354523665000205665ustar00rootroot00000000000000factory_bot_rails-5.1.1/features/support/env.rb000066400000000000000000000013361354523665000217060ustar00rootroot00000000000000require "aruba/cucumber" PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..", "..")).freeze Aruba.configure do |config| config.exit_timeout = Integer ENV.fetch("ARUBA_TIMEOUT") { 120 } end if RUBY_PLATFORM == "java" Aruba.configure do |config| config.before_cmd do # disable JIT since these processes are so short lived set_env("JRUBY_OPTS", "-X-C #{ENV['JRUBY_OPTS']}") java_options = ENV["JAVA_OPTS"] if 1.size == 4 # 4 for 32 bit java, 8 for 64 bit java. set_env("JAVA_OPTS", "-d32 #{java_options}") else set_env( "JAVA_OPTS", "-XX:+TieredCompilation -XX:TieredStopAtLevel=1 #{java_options}", ) end end end end factory_bot_rails-5.1.1/features/support/rails_template000066400000000000000000000001041354523665000235110ustar00rootroot00000000000000gsub_file "Gemfile", /^gem 'sqlite3'$/, 'gem "sqlite3", "~> 1.3.6"' factory_bot_rails-5.1.1/gemfiles/000077500000000000000000000000001354523665000170275ustar00rootroot00000000000000factory_bot_rails-5.1.1/gemfiles/rails4.2.gemfile000066400000000000000000000010241354523665000217140ustar00rootroot00000000000000# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", "~> 1.3.6" gem "rubocop", "0.54", require: false gem "rails", "~> 4.2.11" gemspec name: "factory_bot_rails", path: "../" factory_bot_rails-5.1.1/gemfiles/rails5.0.gemfile000066400000000000000000000011451354523665000217170ustar00rootroot00000000000000# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", "~> 1.3.6" gem "rubocop", "0.54", require: false gem "activerecord", "~> 5.0.7" gem "railties", "~> 5.0.7" gem "actionmailer", "~> 5.0.7" gem "sass-rails" gemspec name: "factory_bot_rails", path: "../" factory_bot_rails-5.1.1/gemfiles/rails5.1.gemfile000066400000000000000000000011531354523665000217170ustar00rootroot00000000000000# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", platforms: :ruby gem "rubocop", "0.54", require: false gem "activerecord", "~> 5.1.7" gem "railties", "~> 5.1.7" gem "actionmailer", "~> 5.1.7" gem "sass-rails" gemspec name: "factory_bot_rails", path: "../" factory_bot_rails-5.1.1/gemfiles/rails5.2.gemfile000066400000000000000000000012321354523665000217160ustar00rootroot00000000000000# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", platforms: :ruby gem "rubocop", "0.54", require: false gem "activerecord", "~> 5.2.3" gem "railties", "~> 5.2.3" gem "actionmailer", "~> 5.2.3" gem "bootsnap" gem "activestorage", "~> 5.2.3" gem "sass-rails" gemspec name: "factory_bot_rails", path: "../" factory_bot_rails-5.1.1/gemfiles/rails6.0.gemfile000066400000000000000000000014661354523665000217260ustar00rootroot00000000000000# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal" gem "aruba" gem "coffee-rails" gem "cucumber", "1.3.19" gem "jquery-rails" gem "rake" gem "rspec-rails" gem "test-unit" gem "uglifier" gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby gem "jdbc-sqlite3", platforms: :jruby gem "jruby-openssl", platforms: :jruby gem "therubyrhino", platforms: :jruby gem "sqlite3", platforms: :ruby gem "rubocop", "0.54", require: false gem "rails", "~> 6.0.0" gem "bootsnap" gem "byebug" gem "capybara" gem "jbuilder" gem "listen", "~> 3.1.5" gem "puma", "~> 3.11" gem "sass-rails", "~> 5" gem "selenium-webdriver" gem "spring" gem "spring-watcher-listen" gem "turbolinks" gem "web-console", group: :development gem "webdrivers" gem "webpacker" gemspec name: "factory_bot_rails", path: "../" factory_bot_rails-5.1.1/lib/000077500000000000000000000000001354523665000160025ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/factory_bot_rails.rb000066400000000000000000000001001354523665000220230ustar00rootroot00000000000000require "factory_bot_rails/railtie" module FactoryBotRails end factory_bot_rails-5.1.1/lib/factory_bot_rails/000077500000000000000000000000001354523665000215075ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/factory_bot_rails/definition_file_paths.rb000066400000000000000000000010131354523665000263550ustar00rootroot00000000000000# frozen_string_literal: true module FactoryBotRails class DefinitionFilePaths def initialize(definition_file_paths) @files = [] @directories = {} definition_file_paths.each do |path| @files << "#{path}.rb" @directories[path.to_s] = [:rb] end end def directories @directories.select { |path| Dir.exist?(path) } end def files @files.select { |file| File.exist?(file) } end def any? directories.any? || files.any? end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/generator.rb000066400000000000000000000014271354523665000240260ustar00rootroot00000000000000require "factory_bot_rails/generators/rspec_generator" require "factory_bot_rails/generators/non_rspec_generator" require "factory_bot_rails/generators/null_generator" module FactoryBotRails class Generator def initialize(config) @generators = config.app_generators end def run generator.new(@generators).run end def generator return Generators::NullGenerator if factory_bot_disabled? if test_framework == :rspec Generators::RSpecGenerator else Generators::NonRSpecGenerator end end def test_framework rails_options[:test_framework] end def factory_bot_disabled? rails_options[:factory_bot] == false end def rails_options @generators.options[:rails] end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/generators/000077500000000000000000000000001354523665000236605ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/factory_bot_rails/generators/non_rspec_generator.rb000066400000000000000000000006651354523665000302500ustar00rootroot00000000000000module FactoryBotRails module Generators class NonRSpecGenerator def initialize(generators) @generators = generators end def run @generators.test_framework( test_framework, fixture: false, fixture_replacement: :factory_bot, ) end private def test_framework @generators.options[:rails][:test_framework] end end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/generators/null_generator.rb000066400000000000000000000002311354523665000272210ustar00rootroot00000000000000module FactoryBotRails module Generators class NullGenerator def initialize(generators) end def run end end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/generators/rspec_generator.rb000066400000000000000000000012071354523665000273670ustar00rootroot00000000000000module FactoryBotRails module Generators class RSpecGenerator def initialize(generators) @generators = generators end def run @generators.fixture_replacement( fixture_replacement_setting, dir: factory_bot_directory, ) end private def fixture_replacement_setting @generators.options[:rails][:fixture_replacement] || :factory_bot end def factory_bot_directory factory_bot_options.fetch(:dir, "spec/factories") end def factory_bot_options @generators.options.fetch(:factory_bot, {}) end end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/railtie.rb000066400000000000000000000015721354523665000234720ustar00rootroot00000000000000# frozen_string_literal: true require "factory_bot" require "factory_bot_rails/generator" require "factory_bot_rails/reloader" require "rails" module FactoryBotRails class Railtie < Rails::Railtie config.factory_bot = ActiveSupport::OrderedOptions.new config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths initializer "factory_bot.set_fixture_replacement" do Generator.new(config).run end initializer "factory_bot.set_factory_paths" do FactoryBot.definition_file_paths = definition_file_paths end initializer "factory_bot.register_reloader" do |app| Reloader.new(app, config).run end config.after_initialize do FactoryBot.reload end private def definition_file_paths config.factory_bot.definition_file_paths.map do |path| Rails.root.join(path) end end end end factory_bot_rails-5.1.1/lib/factory_bot_rails/reloader.rb000066400000000000000000000015061354523665000236330ustar00rootroot00000000000000# frozen_string_literal: true require "factory_bot_rails/definition_file_paths" module FactoryBotRails class Reloader def initialize(app, config) @app = app @config = config @paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths) end def run return unless @paths.any? register_reloader(build_reloader) end private attr_reader :app, :config def build_reloader reloader_class.new(@paths.files, @paths.directories) do FactoryBot.reload end end def reloader_class app.config.file_watcher end def register_reloader(reloader) closed_over_app = app config.to_prepare do if closed_over_app.initialized? reloader.execute end end app.reloaders << reloader end end end factory_bot_rails-5.1.1/lib/generators/000077500000000000000000000000001354523665000201535ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/generators/factory_bot.rb000066400000000000000000000007671354523665000230250ustar00rootroot00000000000000require "rails/generators/named_base" module FactoryBot module Generators class Base < Rails::Generators::NamedBase #:nodoc: def self.source_root path = File.join( File.dirname(__FILE__), "factory_bot", generator_name, "templates", ) File.expand_path(path) end def explicit_class_option return if class_name == singular_table_name.camelize ", class: '#{class_name}'" end end end end factory_bot_rails-5.1.1/lib/generators/factory_bot/000077500000000000000000000000001354523665000224665ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/generators/factory_bot/model/000077500000000000000000000000001354523665000235665ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/generators/factory_bot/model/model_generator.rb000066400000000000000000000040501354523665000272600ustar00rootroot00000000000000require "generators/factory_bot" require "factory_bot_rails" module FactoryBot module Generators class ModelGenerator < Base argument( :attributes, type: :array, default: [], banner: "field:type field:type", ) class_option( :dir, type: :string, default: "test/factories", desc: "The directory or file root where factories belong", ) class_option( :suffix, type: :string, default: nil, desc: "Suffix to add factory file", ) def create_fixture_file if File.exist?(factories_file) insert_factory_into_existing_file else create_factory_file end end private def factories_file options[:dir] + ".rb" end def insert_factory_into_existing_file insert_into_file( factories_file, factory_definition, after: "FactoryBot.define do\n", ) end def create_factory_file file = File.join(options[:dir], "#{filename}.rb") template "factories.erb", file end def factory_definition <<~RUBY factory :#{singular_table_name}#{explicit_class_option} do #{factory_attributes.gsub(/^/, ' ')} end RUBY end def factory_attributes attributes.map do |attribute| "#{attribute.name} { #{attribute.default.inspect} }" end.join("\n") end def filename if factory_bot_options[:filename_proc].present? factory_bot_options[:filename_proc].call(table_name) else name = File.join(class_path, plural_name) [name, filename_suffix].compact.join("_") end end def filename_suffix factory_bot_options[:suffix] || options[:suffix] end def factory_bot_options generators.options[:factory_bot] || {} end def generators FactoryBotRails::Railtie.config.app_generators end end end end factory_bot_rails-5.1.1/lib/generators/factory_bot/model/templates/000077500000000000000000000000001354523665000255645ustar00rootroot00000000000000factory_bot_rails-5.1.1/lib/generators/factory_bot/model/templates/factories.erb000066400000000000000000000000721354523665000302340ustar00rootroot00000000000000FactoryBot.define do <%= factory_definition.rstrip %> end factory_bot_rails-5.1.1/spec/000077500000000000000000000000001354523665000161665ustar00rootroot00000000000000factory_bot_rails-5.1.1/spec/factory_bot_rails/000077500000000000000000000000001354523665000216735ustar00rootroot00000000000000factory_bot_rails-5.1.1/spec/factory_bot_rails/definition_file_paths_spec.rb000066400000000000000000000025671354523665000275720ustar00rootroot00000000000000# frozen_string_literal: true describe FactoryBotRails::DefinitionFilePaths do describe "#files" do it "returns a list of definition files that only exist" do definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] files = described_class.new(definition_file_paths).files expect(files).to eq ["spec/fixtures/factories.rb"] end end describe "#directories" do it "returns a hash of definition directories that only exist" do definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] directories = described_class.new(definition_file_paths).directories expect(directories).to eq( "spec/fixtures/factories" => [:rb], ) end it "converts Pathname objects to strings" do definition_file_paths = [Pathname.new("spec/fixtures/factories")] directories = described_class.new(definition_file_paths).directories expect(directories).to eq("spec/fixtures/factories" => [:rb]) end end describe "#any?" do it "returns true only if definition file paths exist" do definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] expect(described_class.new(definition_file_paths).any?).to eq true definition_file_paths = ["not_exist_directory"] expect(described_class.new(definition_file_paths).any?).to eq false end end end factory_bot_rails-5.1.1/spec/factory_bot_rails/railtie_spec.rb000066400000000000000000000026021354523665000246630ustar00rootroot00000000000000# frozen_string_literal: true describe FactoryBotRails::Railtie do describe "application reloading" do context "when a definition file has been updated" do it "reloads the factory definitions" do allow(FactoryBot).to receive(:reload) touch("factories.rb") reload_rails! expect(FactoryBot).to have_received(:reload).at_least(1).times end end context "when a file in a definition directory has been updated" do it "reloads the factory definitions" do allow(FactoryBot).to receive(:reload) touch("factories/definitions.rb") reload_rails! expect(FactoryBot).to have_received(:reload).at_least(1).times end end context "when the factory definitions have NOT been updated" do it "reloads the factory definitions" do allow(FactoryBot).to receive(:reload) reload_rails! expect(FactoryBot).to have_received(:reload).at_least(1).times end end def touch(file) FileUtils.touch(Rails.root.join(file)) end def reload_rails! if defined? ActiveSupport::Reloader Rails.application.reloader.reload! else # For Rails 4 ActionDispatch::Reloader.cleanup! ActionDispatch::Reloader.prepare! end wait_for_rails_to_reload end def wait_for_rails_to_reload sleep 0.01 end end end factory_bot_rails-5.1.1/spec/factory_bot_rails/reloader_spec.rb000066400000000000000000000025751354523665000250400ustar00rootroot00000000000000# frozen_string_literal: true describe FactoryBotRails::Reloader do describe "#run" do before do @original_definition_file_paths = FactoryBot.definition_file_paths end after do FactoryBot.definition_file_paths = @original_definition_file_paths end context "when a definition file paths exist" do it "registers a reloader" do allow(reloader_class).to receive(:new) run_reloader(["spec/fixtures/factories", "not_exist_directory"]) expect(reloader_class).to have_received(:new) end end context "when a file exists but not a directory" do it "registers a reloader" do allow(reloader_class).to receive(:new) run_reloader(["spec/fake_app", "not_exist_directory"]) expect(reloader_class).to have_received(:new) end end context "when a definition file paths NOT exist" do it "does NOT register a reloader" do allow(reloader_class).to receive(:new) run_reloader(["not_exist_directory"]) expect(reloader_class).not_to have_received(:new) end end def run_reloader(definition_file_paths) FactoryBot.definition_file_paths = definition_file_paths FactoryBotRails::Reloader. new(Rails.application, Rails.application.config).run end def reloader_class Rails.application.config.file_watcher end end end factory_bot_rails-5.1.1/spec/fake_app.rb000066400000000000000000000003461354523665000202640ustar00rootroot00000000000000# frozen_string_literal: true module Dummy class Application < Rails::Application config.eager_load = false config.root = "spec/fixtures" end end Rails.logger = Logger.new("/dev/null") Rails.application.initialize! factory_bot_rails-5.1.1/spec/fixtures/000077500000000000000000000000001354523665000200375ustar00rootroot00000000000000factory_bot_rails-5.1.1/spec/fixtures/factories.rb000066400000000000000000000000421354523665000223370ustar00rootroot00000000000000# Rails.root.join('factories.rb') factory_bot_rails-5.1.1/spec/fixtures/factories/000077500000000000000000000000001354523665000220165ustar00rootroot00000000000000factory_bot_rails-5.1.1/spec/fixtures/factories/definitions.rb000066400000000000000000000000611354523665000246530ustar00rootroot00000000000000# Rails.root.join('factories', 'definitions.rb') factory_bot_rails-5.1.1/spec/spec_helper.rb000066400000000000000000000003601354523665000210030ustar00rootroot00000000000000# frozen_string_literal: true ENV["RAILS_ENV"] = "test" require "factory_bot_rails" require "fake_app" RSpec.configure do |config| config.run_all_when_everything_filtered = true config.filter_run :focus config.order = "random" end