mirror of
https://github.com/thoughtbot/factory_bot_rails.git
synced 2022-11-09 11:49:18 -05:00
Test more realistic scenario for 3rd-party engine
With factory_bot 5 we changed the official way to include factory_bot definitions in an engine from using an initializer to using the `config.factory_bot.definition_file_paths` configuration. We left these tests with the initializer around because we wanted to avoid breaking existing engines. With #331 if none of the paths in `config.factory_bot.definition_file_paths` exist we won't load any factory definitions at all. Before this PR this test was failing with #311 because none of the paths in `config.factory_bot.definition_file_paths` existed, and so the definition file in the engine never got loaded. I am changing this test because there scenario is unlike. If somebody is using factory_bot definitions from an engine, they will most likely have their own factory definitions as well, defined in one of the `config.factory_bot.definition_file_paths`. If it turns out somebody is using factory_bot definitions from an engine without also using definitions in their own project, there are a couple of workarounds: - Add an empty factory file (e.g. test/factories.rb) - Manually call `FactoryBot.reload` - Update the engine to use the official `config.factory_bot.definition_file_paths` instead of an initializer
This commit is contained in:
parent
dcfc9f6662
commit
2b7bca0094
1 changed files with 14 additions and 3 deletions
|
@ -138,16 +138,27 @@ Feature: automatically load factory definitions
|
||||||
end
|
end
|
||||||
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:
|
When I write to "test/unit/user_test.rb" with:
|
||||||
"""
|
"""
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class UserTest < ActiveSupport::TestCase
|
class UserTest < ActiveSupport::TestCase
|
||||||
test "use factory of some_railtie" do
|
test "use factory of some_railtie" do
|
||||||
user = FactoryBot.create(:factory_from_some_railtie)
|
railtie_user = FactoryBot.create(:factory_from_some_railtie)
|
||||||
assert_equal 'Artem', user.name
|
assert_equal 'Artem', railtie_user.name
|
||||||
|
|
||||||
|
user = FactoryBot.create(:user)
|
||||||
|
assert_equal 'Frank', user.name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
"""
|
"""
|
||||||
When I run `bundle exec rake test` with a clean environment
|
When I run `bundle exec rake test` with a clean environment
|
||||||
Then the output should contain "1 assertions, 0 failures, 0 errors"
|
Then the output should contain "2 assertions, 0 failures, 0 errors"
|
||||||
|
|
Loading…
Reference in a new issue