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 'puma'
gem 'rspec-rails', '~> 3.9'
gem 'factory_girl_rails', require: false
gem 'factory_bot_rails', require: false
gem 'faker'
gem 'capybara'
gem 'poltergeist'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,7 +17,7 @@ feature "Ban users", :devise do
# And I am banned
# Then I see the sign in page
scenario "user gets banned", js: true do
me = FactoryGirl.create :user
me = FactoryBot.create :user
login_as me, scope: :user
visit root_path
@ -34,7 +34,7 @@ feature "Ban users", :devise do
end
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.save

View File

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

View File

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

View File

@ -5,7 +5,7 @@
feature 'Sign in', :devise 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)
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login'
end
@ -16,7 +16,7 @@ feature 'Sign in', :devise do
# When I sign in with valid credentials
# Then I see a success message
scenario 'user can sign in with valid credentials', js: true do
user = FactoryGirl.create(:user)
user = FactoryBot.create(:user)
signin(user.email, user.password)
expect(page).to have_content I18n.t 'devise.sessions.signed_in'
end
@ -27,7 +27,7 @@ feature 'Sign in', :devise do
# When I sign in with a wrong email
# Then I see an invalid email message
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)
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'login'
end
@ -38,7 +38,7 @@ feature 'Sign in', :devise do
# When I sign in with a wrong password
# Then I see an invalid password message
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?')
expect(page).to have_content I18n.t 'devise.failure.invalid', authentication_keys: 'login'
end

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ RSpec.describe Question, :type => :model do
before :each do
@question = Question.new(
content: 'Is this a question?',
user: FactoryGirl.create(:user)
user: FactoryBot.create(:user)
)
end
@ -22,12 +22,12 @@ RSpec.describe Question, :type => :model do
end
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
end
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
@question.destroy
expect{Answer.find(first_answer_id)}.to raise_error(ActiveRecord::RecordNotFound)

View File

@ -17,7 +17,7 @@ Capybara.register_driver :poltergeist do |app|
end
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
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

View File

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