Replace FactoryGirl with FactoryBot

This commit is contained in:
Georg Gadinger 2020-04-19 17:27:42 +02:00
parent 3b4dc9961e
commit 2d3e55a7c4
16 changed files with 47 additions and 48 deletions

View File

@ -102,7 +102,7 @@ group :development, :test do
gem 'rake' gem 'rake'
gem 'puma' gem 'puma'
gem 'rspec-rails', '~> 3.9' gem 'rspec-rails', '~> 3.9'
gem 'factory_girl_rails', require: false gem 'factory_bot_rails', require: false
gem 'faker' gem 'faker'
gem 'capybara' gem 'capybara'
gem 'poltergeist' gem 'poltergeist'

View File

@ -141,11 +141,11 @@ GEM
erubi (1.9.0) erubi (1.9.0)
excon (0.73.0) excon (0.73.0)
execjs (2.7.0) execjs (2.7.0)
factory_girl (4.9.0) factory_bot (5.1.2)
activesupport (>= 3.0.0) activesupport (>= 4.2.0)
factory_girl_rails (4.9.0) factory_bot_rails (5.1.1)
factory_girl (~> 4.9.0) factory_bot (~> 5.1.0)
railties (>= 3.0.0) railties (>= 4.2.0)
fake_email_validator (1.0.11) fake_email_validator (1.0.11)
activemodel activemodel
mail mail
@ -540,7 +540,7 @@ DEPENDENCIES
devise-async devise-async
devise-i18n devise-i18n
ed25519 ed25519
factory_girl_rails factory_bot_rails
fake_email_validator fake_email_validator
faker faker
fog-aws fog-aws

View File

@ -1,8 +1,8 @@
FactoryGirl.define do FactoryBot.define do
factory :user do |u| factory :user do |u|
u.sequence(:screen_name) { |n| "#{Faker::Internet.user_name 0..12, %w(_)}#{n}" } u.sequence(:screen_name) { |n| "#{Faker::Internet.user_name 0..12, %w(_)}#{n}" }
u.sequence(:email) { |n| "#{n}#{Faker::Internet.email}" } u.sequence(:email) { |n| "#{n}#{Faker::Internet.email}" }
u.password "P4s5w0rD" u.password { "P4s5w0rD" }
u.sequence(:confirmed_at) { Time.zone.now } u.sequence(:confirmed_at) { Time.zone.now }
u.display_name { Faker::Name.name } u.display_name { Faker::Name.name }
end end

View File

@ -1,6 +1,6 @@
FactoryGirl.define do FactoryBot.define do
factory :answer do |u| factory :answer do |u|
u.sequence(:content) { |n| "This is an answer. I'm number #{n}!" } u.sequence(:content) { |n| "This is an answer. I'm number #{n}!" }
u.user FactoryGirl.create(:user) u.user { FactoryBot.create(:user) }
end end
end end

View File

@ -1,9 +1,8 @@
FactoryGirl.define do FactoryBot.define do
factory :notification do factory :notification do
target_type "MyString" target_type { "MyString" }
target_id 1 target_id { 1 }
recipient_id 1 recipient_id { 1 }
new false new { false }
end end
end end

View File

@ -1,6 +1,6 @@
FactoryGirl.define do FactoryBot.define do
factory :question do |u| factory :question do |u|
u.sequence(:content) { |n| "#{QuestionGenerator.generate}#{n}" } u.sequence(:content) { |n| "#{QuestionGenerator.generate}#{n}" }
u.author_is_anonymous true u.author_is_anonymous { true }
end end
end end

View File

@ -17,7 +17,7 @@ feature "Ban users", :devise do
# And I am banned # And I am banned
# Then I see the sign in page # Then I see the sign in page
scenario "user gets banned", js: true do scenario "user gets banned", js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
login_as me, scope: :user login_as me, scope: :user
visit root_path visit root_path
@ -34,7 +34,7 @@ feature "Ban users", :devise do
end end
scenario 'user visits banned user profiles', js: true do scenario 'user visits banned user profiles', js: true do
evil_user = FactoryGirl.create :user evil_user = FactoryBot.create :user
evil_user.permanently_banned = true evil_user.permanently_banned = true
evil_user.save evil_user.save

View File

@ -8,8 +8,8 @@ feature "User profile page", :devise do
end end
scenario "user gets followed", js: true do scenario "user gets followed", js: true do
me = FactoryGirl.create(:user) me = FactoryBot.create(:user)
other = FactoryGirl.create(:user) other = FactoryBot.create(:user)
login_as me, scope: :user login_as me, scope: :user
visit show_user_profile_path(other.screen_name) visit show_user_profile_path(other.screen_name)

View File

@ -18,8 +18,8 @@ feature "Inbox", :devise do
# Then I can answer my question # Then I can answer my question
# And see the answer on my user profile # And see the answer on my user profile
scenario "user answers a question", js: true do scenario "user answers a question", js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
question = FactoryGirl.create :question question = FactoryBot.create :question
Inbox.create question: question, user: me, new: true Inbox.create question: question, user: me, new: true
login_as me, scope: :user login_as me, scope: :user
@ -46,7 +46,7 @@ feature "Inbox", :devise do
# And I click "Get new question" # And I click "Get new question"
# Then I get a new question # Then I get a new question
scenario 'user generates new question', js: true do scenario 'user generates new question', js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
login_as me, scope: :user login_as me, scope: :user
visit inbox_path visit inbox_path
@ -65,7 +65,7 @@ feature "Inbox", :devise do
# And I don't want to receive questions by anonymous users # And I don't want to receive questions by anonymous users
# Then I get a new question # Then I get a new question
scenario 'user with privacy options generates new question', js: true do scenario 'user with privacy options generates new question', js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
me.privacy_allow_anonymous_questions = false me.privacy_allow_anonymous_questions = false
me.save me.save
@ -87,8 +87,8 @@ feature "Inbox", :devise do
# And I delete the question # And I delete the question
# Then don't see it anymore in my inbox # Then don't see it anymore in my inbox
scenario "user deletes a question", js: true do scenario "user deletes a question", js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
question = FactoryGirl.create :question question = FactoryBot.create :question
Inbox.create question: question, user: me Inbox.create question: question, user: me
login_as me, scope: :user login_as me, scope: :user
@ -116,9 +116,9 @@ feature "Inbox", :devise do
# And I click on "Delete all questions" # And I click on "Delete all questions"
# Then don't see them anymore in my inbox # Then don't see them anymore in my inbox
scenario "user deletes all questions", js: true do scenario "user deletes all questions", js: true do
me = FactoryGirl.create :user me = FactoryBot.create :user
5.times do 5.times do
question = FactoryGirl.create :question question = FactoryBot.create :question
Inbox.create question: question, user: me Inbox.create question: question, user: me
end end

View File

@ -5,7 +5,7 @@
feature 'Sign in', :devise do feature 'Sign in', :devise do
scenario 'user cannot sign in if not registered', js: true do scenario 'user cannot sign in if not registered', js: true do
user = FactoryGirl.build(:user) user = FactoryBot.build(:user)
signin(user.screen_name, user.password) signin(user.screen_name, user.password)
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login' expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login'
end end
@ -16,7 +16,7 @@ feature 'Sign in', :devise do
# When I sign in with valid credentials # When I sign in with valid credentials
# Then I see a success message # Then I see a success message
scenario 'user can sign in with valid credentials', js: true do scenario 'user can sign in with valid credentials', js: true do
user = FactoryGirl.create(:user) user = FactoryBot.create(:user)
signin(user.email, user.password) signin(user.email, user.password)
expect(page).to have_content I18n.t 'devise.sessions.signed_in' expect(page).to have_content I18n.t 'devise.sessions.signed_in'
end end
@ -27,7 +27,7 @@ feature 'Sign in', :devise do
# When I sign in with a wrong email # When I sign in with a wrong email
# Then I see an invalid email message # Then I see an invalid email message
scenario 'user cannot sign in with wrong email', js: true do scenario 'user cannot sign in with wrong email', js: true do
user = FactoryGirl.create(:user) user = FactoryBot.create(:user)
signin('invalid@email.com', user.password) signin('invalid@email.com', user.password)
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login' expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login'
end end
@ -38,7 +38,7 @@ feature 'Sign in', :devise do
# When I sign in with a wrong password # When I sign in with a wrong password
# Then I see an invalid password message # Then I see an invalid password message
scenario 'user cannot sign in with wrong password', js: true do scenario 'user cannot sign in with wrong password', js: true do
user = FactoryGirl.create(:user) user = FactoryBot.create(:user)
signin(user.email, 'what the fuck is my p4s5w0rD again?') signin(user.email, 'what the fuck is my p4s5w0rD again?')
expect(page).to have_content I18n.t 'devise.failure.invalid', authentication_keys: 'login' expect(page).to have_content I18n.t 'devise.failure.invalid', authentication_keys: 'login'
end end

View File

@ -9,7 +9,7 @@ feature 'Sign out', :devise do
# When I sign out # When I sign out
# Then I see a signed out message # Then I see a signed out message
scenario 'user signs out successfully', js: true do scenario 'user signs out successfully', js: true do
user = FactoryGirl.create(:user) user = FactoryBot.create(:user)
signin(user.email, user.password) signin(user.email, user.password)
expect(page).to have_content I18n.t 'devise.sessions.signed_in' expect(page).to have_content I18n.t 'devise.sessions.signed_in'
click_link user.screen_name click_link user.screen_name

View File

@ -16,7 +16,7 @@ feature "User profile page", :devise do
# When I visit the user profile page # When I visit the user profile page
# Then I see my own screen name and follower count # Then I see my own screen name and follower count
scenario 'user sees own profile', js: true do scenario 'user sees own profile', js: true do
user = FactoryGirl.create(:user) user = FactoryBot.create(:user)
login_as(user, :scope => :user) login_as(user, :scope => :user)
@ -31,8 +31,8 @@ feature "User profile page", :devise do
# When I visit another user's profile # When I visit another user's profile
# Then I see that user's screen name and follower count # Then I see that user's screen name and follower count
scenario "user sees another user's profile", js: true do scenario "user sees another user's profile", js: true do
me = FactoryGirl.create(:user) me = FactoryBot.create(:user)
other = FactoryGirl.create(:user) other = FactoryBot.create(:user)
login_as me, scope: :user login_as me, scope: :user
@ -49,8 +49,8 @@ feature "User profile page", :devise do
# And I click on "Ask" # And I click on "Ask"
# Then I see "Question asked successfully." # Then I see "Question asked successfully."
scenario "user gets asked a question", js: true do scenario "user gets asked a question", js: true do
me = FactoryGirl.create(:user) me = FactoryBot.create(:user)
other = FactoryGirl.create(:user) other = FactoryBot.create(:user)
login_as me, scope: :user login_as me, scope: :user
visit show_user_profile_path(other.screen_name) visit show_user_profile_path(other.screen_name)

View File

@ -4,8 +4,8 @@ RSpec.describe Answer, :type => :model do
before :each do before :each do
@answer = Answer.new( @answer = Answer.new(
content: 'This is an answer.', content: 'This is an answer.',
user: FactoryGirl.create(:user), user: FactoryBot.create(:user),
question: FactoryGirl.create(:question) question: FactoryBot.create(:question)
) )
end end

View File

@ -4,7 +4,7 @@ RSpec.describe Question, :type => :model do
before :each do before :each do
@question = Question.new( @question = Question.new(
content: 'Is this a question?', content: 'Is this a question?',
user: FactoryGirl.create(:user) user: FactoryBot.create(:user)
) )
end end
@ -22,12 +22,12 @@ RSpec.describe Question, :type => :model do
end end
it 'has many answers' do it 'has many answers' do
5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryGirl.create(:user), question: @question) } 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) }
expect(@question.answer_count).to match 5 expect(@question.answer_count).to match 5
end end
it 'also deletes the answers when deleted' do it 'also deletes the answers when deleted' do
5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryGirl.create(:user), question: @question) } 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) }
first_answer_id = @question.answers.first.id first_answer_id = @question.answers.first.id
@question.destroy @question.destroy
expect{Answer.find(first_answer_id)}.to raise_error(ActiveRecord::RecordNotFound) expect{Answer.find(first_answer_id)}.to raise_error(ActiveRecord::RecordNotFound)

View File

@ -17,7 +17,7 @@ Capybara.register_driver :poltergeist do |app|
end end
Capybara.javascript_driver = :poltergeist Capybara.javascript_driver = :poltergeist
require 'factory_girl_rails' require 'factory_bot_rails'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all # This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

View File

@ -1,3 +1,3 @@
RSpec.configure do |config| RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods config.include FactoryBot::Syntax::Methods
end end