mirror of
https://github.com/thoughtbot/factory_bot_rails.git
synced 2022-11-09 11:49:18 -05:00
Fix for factory class naming (#366)
Changing the process of name selection for factory defining Example: rails g model UserMedia filename:string --force-plural ... invoke factory_bot create spec/factories/user_media.rb it generates the correct factory naming: FactoryBot.define do factory :user_media do filename { "MyString" } end end Fixes: #356
This commit is contained in:
parent
5c52981fcc
commit
7780065bd2
3 changed files with 18 additions and 2 deletions
|
@ -24,6 +24,18 @@ Feature:
|
||||||
"""
|
"""
|
||||||
And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do"
|
And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do"
|
||||||
|
|
||||||
|
Scenario: The factory_bot_rails generators create a factory file with correct naming when I use --force-plural
|
||||||
|
When I run `bundle exec rails generate model UserMedia filename:string --force-plural` with a clean environment
|
||||||
|
Then the output should contain "test/factories/user_media.rb"
|
||||||
|
And the file "test/factories/user_media.rb" should contain exactly:
|
||||||
|
"""
|
||||||
|
FactoryBot.define do
|
||||||
|
factory :user_media do
|
||||||
|
filename { "MyString" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
|
||||||
Scenario: The factory_bot_rails generators add a factory in the correct spot
|
Scenario: The factory_bot_rails generators add a factory in the correct spot
|
||||||
When I write to "test/factories.rb" with:
|
When I write to "test/factories.rb" with:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -14,8 +14,12 @@ module FactoryBot
|
||||||
File.expand_path(path)
|
File.expand_path(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def factory_name
|
||||||
|
class_name.gsub("::", "").underscore
|
||||||
|
end
|
||||||
|
|
||||||
def explicit_class_option
|
def explicit_class_option
|
||||||
return if class_name == singular_table_name.camelize
|
return if class_name.underscore == factory_name
|
||||||
|
|
||||||
", class: '#{class_name}'"
|
", class: '#{class_name}'"
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,7 +54,7 @@ module FactoryBot
|
||||||
|
|
||||||
def factory_definition
|
def factory_definition
|
||||||
<<~RUBY
|
<<~RUBY
|
||||||
factory :#{singular_table_name}#{explicit_class_option} do
|
factory :#{factory_name}#{explicit_class_option} do
|
||||||
#{factory_attributes.gsub(/^/, ' ')}
|
#{factory_attributes.gsub(/^/, ' ')}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue