2011-07-11 23:11:06 -04:00
|
|
|
Feature:
|
|
|
|
In order to easily generate factory files instead of fixture files when generating models
|
2016-12-02 10:25:09 -05:00
|
|
|
As a user of Rails and Factory Bot
|
|
|
|
I would like to use factory_bot_rails generators.
|
2011-07-11 23:11:06 -04:00
|
|
|
|
2012-12-19 14:46:21 -05:00
|
|
|
Background:
|
2019-02-06 05:00:06 -05:00
|
|
|
Given I successfully run `bundle exec rails new testapp -m ../../features/support/rails_template`
|
2011-07-11 23:11:06 -04:00
|
|
|
And I cd to "testapp"
|
2016-12-02 10:25:09 -05:00
|
|
|
And I add "factory_bot_rails" from this project as a dependency
|
2018-11-30 22:10:14 -05:00
|
|
|
And I run `bundle install` with a clean environment
|
2012-12-19 14:46:21 -05:00
|
|
|
|
2016-12-02 10:25:09 -05:00
|
|
|
Scenario: The factory_bot_rails generators create a factory file for each model if there is not a factories.rb file
|
2018-11-30 22:10:14 -05:00
|
|
|
When I run `bundle exec rails generate model User name:string age:integer` with a clean environment
|
2014-08-07 19:15:39 -04:00
|
|
|
And I run `bundle exec rails generate model Namespaced::User name:string` with a clean environment
|
2012-02-26 13:58:48 -05:00
|
|
|
Then the output should contain "test/factories/users.rb"
|
2018-12-03 22:11:11 -05:00
|
|
|
And the output should contain "test/factories/namespaced/users.rb"
|
2014-10-30 08:58:52 -04:00
|
|
|
And the file "test/factories/users.rb" should contain exactly:
|
|
|
|
"""
|
2016-12-02 10:25:09 -05:00
|
|
|
FactoryBot.define do
|
2014-10-30 08:58:52 -04:00
|
|
|
factory :user do
|
2018-08-24 16:48:00 -04:00
|
|
|
name { "MyString" }
|
|
|
|
age { 1 }
|
2014-10-30 08:58:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
"""
|
2018-12-03 22:11:11 -05:00
|
|
|
And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do"
|
2012-11-29 05:46:29 -05:00
|
|
|
|
2016-12-02 10:25:09 -05:00
|
|
|
Scenario: The factory_bot_rails generators add a factory in the correct spot
|
2018-11-30 22:10:14 -05:00
|
|
|
When I write to "test/factories.rb" with:
|
2015-03-14 21:59:59 -04:00
|
|
|
"""
|
2016-12-02 10:25:09 -05:00
|
|
|
FactoryBot.define do
|
2015-03-14 21:59:59 -04:00
|
|
|
end
|
|
|
|
"""
|
|
|
|
And I run `bundle exec rails generate model User name:string` with a clean environment
|
2018-11-30 22:07:38 -05:00
|
|
|
And I run `bundle exec rails generate model Robot name:string` with a clean environment
|
2015-03-14 21:59:59 -04:00
|
|
|
Then the file "test/factories.rb" should contain exactly:
|
|
|
|
"""
|
2016-12-02 10:25:09 -05:00
|
|
|
FactoryBot.define do
|
2018-11-30 22:07:38 -05:00
|
|
|
factory :robot do
|
|
|
|
name { "MyString" }
|
|
|
|
end
|
|
|
|
|
2015-03-14 21:59:59 -04:00
|
|
|
factory :user do
|
2018-08-24 16:48:00 -04:00
|
|
|
name { "MyString" }
|
2015-03-14 21:59:59 -04:00
|
|
|
end
|
2018-11-30 22:07:38 -05:00
|
|
|
|
2015-03-14 21:59:59 -04:00
|
|
|
end
|
|
|
|
"""
|
|
|
|
|
2016-12-02 10:25:09 -05:00
|
|
|
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
|
2018-11-30 22:10:14 -05:00
|
|
|
When I write to "test/factories.rb" with:
|
2014-07-25 19:10:08 -04:00
|
|
|
"""
|
2016-12-02 10:25:09 -05:00
|
|
|
FactoryBot.define do
|
2014-07-25 19:10:08 -04:00
|
|
|
end
|
|
|
|
"""
|
2014-08-07 19:15:39 -04:00
|
|
|
And I run `bundle exec rails generate model User name:string` with a clean environment
|
2018-11-30 22:07:38 -05:00
|
|
|
Then the file "test/factories/users.rb" should not contain "factory :user do"
|
2018-11-30 22:10:14 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|