Skip to content

Commit

Permalink
Reduce number of requests in user tests
Browse files Browse the repository at this point in the history
We were adding a `visit` in a `before` block but then we started some
tests with another `visit`.

We also destroyed records in the database in between, which increased
the risk of database inconsistency since the process running the browser
had already been started.

Besides, some tests were wrong; they were visiting a page with the
browser, then destroying records in the database, and then checking the
page without reloading the browser. Since we aren't automatically
refreshing the affected areas of the page, obviously the page content
before and after destroying records is exactly the same, and the test
was passing because it's testing content that isn't there in any
situation.
  • Loading branch information
javierm committed Apr 13, 2021
1 parent b52d381 commit 16fdffd
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions spec/system/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
describe "Show (public page)" do
let(:user) { create(:user) }

before do
1.times { create(:debate, author: user) }
2.times { create(:proposal, author: user) }
3.times { create(:budget_investment, author: user) }
4.times { create(:comment, user: user) }
let!(:debates) { 1.times.map { create(:debate, author: user) } }
let!(:proposals) { 2.times.map { create(:proposal, author: user) } }
let!(:investments) { 3.times.map { create(:budget_investment, author: user) } }
let!(:comments) { 4.times.map { create(:comment, user: user) } }

scenario "shows user public activity" do
visit user_path(user)
end

scenario "shows user public activity" do
expect(page).to have_content("1 Debate")
expect(page).to have_content("2 Proposals")
expect(page).to have_content("3 Investments")
expect(page).to have_content("4 Comments")
end

scenario "shows only items where user has activity" do
user.proposals.destroy_all
proposals.each(&:destroy)

visit user_path(user)

expect(page).not_to have_content("0 Proposals")
expect(page).to have_content("1 Debate")
Expand All @@ -30,85 +30,92 @@
end

scenario "default filter is proposals" do
user.proposals.each do |proposal|
visit user_path(user)

proposals.each do |proposal|
expect(page).to have_content(proposal.title)
end

user.debates.each do |debate|
debates.each do |debate|
expect(page).not_to have_content(debate.title)
end

user.comments.each do |comment|
comments.each do |comment|
expect(page).not_to have_content(comment.body)
end
end

scenario "shows debates by default if user has no proposals" do
user.proposals.destroy_all
proposals.each(&:destroy)

visit user_path(user)

expect(page).to have_content(user.debates.first.title)
expect(page).to have_content(debates.first.title)
end

scenario "shows investments by default if user has no proposals nor debates" do
user.proposals.destroy_all
user.debates.destroy_all
proposals.each(&:destroy)
debates.each(&:destroy)

visit user_path(user)

expect(page).to have_content(user.budget_investments.first.title)
expect(page).to have_content(investments.first.title)
end

scenario "shows comments by default if user has no proposals nor debates nor investments" do
user.proposals.destroy_all
user.debates.destroy_all
user.budget_investments.destroy_all
proposals.each(&:destroy)
debates.each(&:destroy)
investments.each(&:destroy)

visit user_path(user)

user.comments.each do |comment|
comments.each do |comment|
expect(page).to have_content(comment.body)
end
end

scenario "filters" do
visit user_path(user)

click_link "1 Debate"

user.debates.each do |debate|
debates.each do |debate|
expect(page).to have_content(debate.title)
end

user.proposals.each do |proposal|
proposals.each do |proposal|
expect(page).not_to have_content(proposal.title)
end

user.comments.each do |comment|
comments.each do |comment|
expect(page).not_to have_content(comment.body)
end

click_link "4 Comments"

user.comments.each do |comment|
comments.each do |comment|
expect(page).to have_content(comment.body)
end

user.proposals.each do |proposal|
proposals.each do |proposal|
expect(page).not_to have_content(proposal.title)
end

user.debates.each do |debate|
debates.each do |debate|
expect(page).not_to have_content(debate.title)
end

click_link "2 Proposals"

user.proposals.each do |proposal|
proposals.each do |proposal|
expect(page).to have_content(proposal.title)
end

user.comments.each do |comment|
comments.each do |comment|
expect(page).not_to have_content(comment.body)
end

user.debates.each do |debate|
debates.each do |debate|
expect(page).not_to have_content(debate.title)
end
end
Expand Down

0 comments on commit 16fdffd

Please sign in to comment.