Skip to content

Commit

Permalink
Change Factory loading mechanism to FactoryBots supported mechanism
Browse files Browse the repository at this point in the history
We build these factories such that they could be independently loaded.
However, FactoryBot is built with the assumption in mind that it gets a
number of folder names and loads the factories when loading itself (via
FactoryBot.find_definitions).

The old behaviour is still supported, but deprecated.
  • Loading branch information
mamhoff committed Feb 17, 2021
1 parent fd250f0 commit 099e803
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 50 deletions.
11 changes: 11 additions & 0 deletions lib/alchemy/test_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Alchemy
module TestSupport
def self.factory_paths
Dir[
::Alchemy::Engine.root.join("lib", "alchemy", "test_support", "factories", "*_factory.rb")
].map { |path| path.sub(/.rb\z/, "") }
end
end
end
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/essence_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

require "shoulda-matchers"
require "alchemy/test_support/factories/page_factory"
require "alchemy/test_support/factories/element_factory"
require "alchemy/test_support/factories/content_factory"

RSpec.shared_examples_for "an essence" do
let(:element) { Alchemy::Element.new }
Expand Down
18 changes: 15 additions & 3 deletions lib/alchemy/test_support/factories.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# frozen_string_literal: true

Dir["#{File.dirname(__FILE__)}/factories/*.rb"].sort.each do |file|
require file
end
require 'alchemy/test_support'

Alchemy::Deprecation.warn <<~MSG
Please require factories using FactoryBots preferred approach:
# spec/rails_helper.rb
require 'alchemy/test_support'
FactoryBot.definition_file_paths.concat(Alchemy::TestSupport.factory_paths)
FactoryBot.reload
MSG

FactoryBot.definition_file_paths.concat(Alchemy::TestSupport.factory_paths)
FactoryBot.reload
2 changes: 0 additions & 2 deletions lib/alchemy/test_support/factories/attachment_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "factory_bot"

FactoryBot.define do
factory :alchemy_attachment, class: "Alchemy::Attachment" do
file do
Expand Down
6 changes: 0 additions & 6 deletions lib/alchemy/test_support/factories/content_factory.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/element_factory"
require "alchemy/test_support/factories/essence_file_factory"
require "alchemy/test_support/factories/essence_picture_factory"
require "alchemy/test_support/factories/essence_text_factory"

FactoryBot.define do
factory :alchemy_content, class: "Alchemy::Content" do
name { "text" }
Expand Down
2 changes: 0 additions & 2 deletions lib/alchemy/test_support/factories/dummy_user_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "factory_bot"

FactoryBot.define do
factory :alchemy_dummy_user, class: "DummyUser" do
sequence(:email) { |n| "john.#{n}@doe.com" }
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/element_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/page_factory"

FactoryBot.define do
factory :alchemy_element, class: "Alchemy::Element" do
name { "article" }
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/essence_file_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/attachment_factory"

FactoryBot.define do
factory :alchemy_essence_file, class: "Alchemy::EssenceFile" do
attachment factory: :alchemy_attachment
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/essence_page_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/page_factory"

FactoryBot.define do
factory :alchemy_essence_page, class: "Alchemy::EssencePage" do
page factory: :alchemy_page
Expand Down
4 changes: 0 additions & 4 deletions lib/alchemy/test_support/factories/essence_picture_factory.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/content_factory"
require "alchemy/test_support/factories/picture_factory"

FactoryBot.define do
factory :alchemy_essence_picture, class: "Alchemy::EssencePicture" do
picture factory: :alchemy_picture
Expand Down
2 changes: 0 additions & 2 deletions lib/alchemy/test_support/factories/essence_text_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "factory_bot"

FactoryBot.define do
factory :alchemy_essence_text, class: "Alchemy::EssenceText" do
body { "This is a headline" }
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/language_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/site_factory"

FactoryBot.define do
factory :alchemy_language, class: "Alchemy::Language" do
name { "Your Language" }
Expand Down
4 changes: 0 additions & 4 deletions lib/alchemy/test_support/factories/node_factory.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/language_factory"
require "alchemy/test_support/factories/page_factory"

FactoryBot.define do
factory :alchemy_node, class: "Alchemy::Node" do
language { Alchemy::Language.default || create(:alchemy_language) }
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/page_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "alchemy/test_support/factories/language_factory"

FactoryBot.define do
factory :alchemy_page, class: "Alchemy::Page" do
language do
Expand Down
2 changes: 0 additions & 2 deletions lib/alchemy/test_support/factories/picture_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "factory_bot"

FactoryBot.define do
factory :alchemy_picture, class: "Alchemy::Picture" do
image_file do
Expand Down
3 changes: 0 additions & 3 deletions lib/alchemy/test_support/factories/picture_thumb_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "factory_bot"
require "securerandom"

FactoryBot.define do
factory :alchemy_picture_thumb, class: "Alchemy::PictureThumb" do
picture { create(:alchemy_picture) }
Expand Down
2 changes: 0 additions & 2 deletions lib/alchemy/test_support/factories/site_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "factory_bot"

FactoryBot.define do
factory :alchemy_site, class: "Alchemy::Site" do
name { "A Site" }
Expand Down
7 changes: 5 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
require "rspec/rails"
require "webdrivers/chromedriver"
require "shoulda-matchers"
require "factory_bot"

require "alchemy/seeder"
require "alchemy/test_support"
require "alchemy/test_support/config_stubbing"
require "alchemy/test_support/essence_shared_examples"
require "alchemy/test_support/integration_helpers"
require "alchemy/test_support/factories"
require "alchemy/test_support/shared_contexts"
require "alchemy/test_support/shared_uploader_examples"

require_relative "factories"
require_relative "support/calculation_examples.rb"
require_relative "support/hint_examples.rb"
require_relative "support/transformation_examples.rb"
Expand All @@ -44,6 +44,9 @@
Capybara.default_selector = :css
Capybara.ignore_hidden_elements = false

FactoryBot.definition_file_paths.concat(Alchemy::TestSupport.factory_paths)
FactoryBot.reload

Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
Expand Down

0 comments on commit 099e803

Please sign in to comment.