Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions test/controllers/diary_comments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
require "test_helper"

class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
def setup
super
# Create the default language for diary entries
create(:language, :code => "en")
end

def test_routes
assert_routing(
{ :path => "/user/username/diary/1/comments", :method => :post },
Expand Down
2 changes: 0 additions & 2 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ def test_auth_failure_callback

def test_show_profile_diaries
user = create(:user)
create(:language, :code => "en")
create(:diary_entry, :user => user, :title => "First Entry", :body => "First body")
create(:diary_entry, :user => user, :title => "Second Entry", :body => "Second body")
create(:diary_entry, :user => user, :title => "Third Entry", :body => "Third body")
Expand All @@ -393,7 +392,6 @@ def test_show_profile_diaries

def test_show_profile_diaries_with_comments
user = create(:user)
create(:language, :code => "en")
entry = create(:diary_entry, :user => user, :title => "Entry with Comments")
create(:diary_comment, :diary_entry => entry)
create(:diary_comment, :diary_entry => entry)
Expand Down
1 change: 1 addition & 0 deletions test/factories/diary_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
sequence(:title) { |n| "Diary entry #{n}" }
sequence(:body) { |n| "This is diary entry #{n}" }

language { Language.find_by(:code => "en") || create(:language, :code => "en") }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use find_or_create_by here? It does side-step factory_bot entirely, but is perhaps easier to read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, losing the factory feels a bit wrong. How about the following?

Suggested change
language { Language.find_by(:code => "en") || create(:language, :code => "en") }
language do
Language.find_or_create_by(:code => "en") do |record|
record.assign_attributes(attributes_for(:language))
end
end

Significantly more verbose though...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, and after spending some time hunting through the factorybot documentation assuming that there must be a better way, it seems that @pablobm's original suggestion is probably best.

To be honest I'm struggling to understand how this has been working... Does factorybot really just associate to a random record if you don't tell it to do something else?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It confused me too when I was looking into it. It's not FactoryBot, it's our SQL schema, which defaults to en:

    language_code character varying DEFAULT 'en'::character varying NOT NULL,

user
end
end
2 changes: 0 additions & 2 deletions test/helpers/issues_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class IssuesHelperTest < ActionView::TestCase
attr_accessor :current_user

def test_reportable_heading_diary_comment
create(:language, :code => "en")
diary_entry = create(:diary_entry, :title => "A Discussion")
diary_comment = create(:diary_comment, :diary_entry => diary_entry, :created_at => "2020-03-15", :updated_at => "2021-05-17")

Expand All @@ -20,7 +19,6 @@ def test_reportable_heading_diary_comment
end

def test_reportable_heading_diary_entry
create(:language, :code => "en")
diary_entry = create(:diary_entry, :title => "Important Subject", :created_at => "2020-03-24", :updated_at => "2021-05-26")

heading = reportable_heading diary_entry
Expand Down
2 changes: 0 additions & 2 deletions test/mailers/user_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def test_message_notification
end

def test_diary_comment_notification
create(:language, :code => "en")
user = create(:user)
other_user = create(:user)
diary_entry = create(:diary_entry, :user => user)
Expand Down Expand Up @@ -142,7 +141,6 @@ def test_note_comment_notification
end

def test_changeset_comment_notification
create(:language, :code => "en")
user = create(:user)
other_user = create(:user)
changeset = create(:changeset, :user => user)
Expand Down
5 changes: 0 additions & 5 deletions test/models/diary_comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
require "test_helper"

class DiaryCommentTest < ActiveSupport::TestCase
def setup
# Create the default language for diary entries
create(:language, :code => "en")
end

test "body must be present" do
comment = build(:diary_comment, :body => "")
assert_not comment.valid?
Expand Down
5 changes: 0 additions & 5 deletions test/models/diary_entry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
require "test_helper"

class DiaryEntryTest < ActiveSupport::TestCase
def setup
# Create the default language for diary entries
create(:language, :code => "en")
end

def test_diary_entry_validations
diary_entry_valid({})
diary_entry_valid({ :title => "" }, :valid => false)
Expand Down
1 change: 0 additions & 1 deletion test/models/issue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def test_assigned_role
end

def test_reported_user
create(:language, :code => "en")
user = create(:user)
note = create(:note, :author => create(:user))
anonymous_note = create(:note, :author => nil)
Expand Down
1 change: 0 additions & 1 deletion test/system/report_diary_comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ReportDiaryCommentTest < ApplicationSystemTestCase
def setup
create(:language, :code => "en")
@diary_entry = create(:diary_entry)
@comment = create(:diary_comment, :diary_entry => @diary_entry)
end
Expand Down
1 change: 0 additions & 1 deletion test/system/report_diary_entry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ReportDiaryEntryTest < ApplicationSystemTestCase
def setup
create(:language, :code => "en")
@diary_entry = create(:diary_entry)
end

Expand Down
1 change: 0 additions & 1 deletion test/system/user_logout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class UserLogoutTest < ApplicationSystemTestCase
end

test "Sign out after navigating diary entries with Turbo pagination" do
create(:language, :code => "en")
create(:diary_entry, :title => "First Diary Entry")
create_list(:diary_entry, 20) # rubocop:disable FactoryBot/ExcessiveCreateList

Expand Down