From 9aa6d646d418335d09d61c8e689add352598d8a7 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Sat, 10 Feb 2018 19:34:54 +0100 Subject: [PATCH] Remove usage of instance variables at specs --- .../features/admin/budget_investments_spec.rb | 184 +++++++++--------- 1 file changed, 91 insertions(+), 93 deletions(-) diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index cc2d54c00..18b82cf9e 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -2,11 +2,14 @@ require 'rails_helper' feature 'Admin budget investments' do + let(:budget) { create(:budget) } + let(:administrator) do + create(:administrator, user: create(:user, username: 'Ana', email: 'ana@admins.org')) + end + background do admin = create(:administrator) login_as(admin.user) - - @budget = create(:budget) end context "Feature flag" do @@ -28,8 +31,8 @@ feature 'Admin budget investments' do context "Index" do scenario 'Displaying investments' do - budget_investment = create(:budget_investment, budget: @budget, cached_votes_up: 77) - visit admin_budget_budget_investments_path(budget_id: @budget.id) + budget_investment = create(:budget_investment, budget: budget, cached_votes_up: 77) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_content(budget_investment.title) expect(page).to have_content(budget_investment.heading.name) expect(page).to have_content(budget_investment.id) @@ -52,9 +55,9 @@ feature 'Admin budget investments' do end scenario 'Displaying assignments info' do - budget_investment1 = create(:budget_investment, budget: @budget) - budget_investment2 = create(:budget_investment, budget: @budget) - budget_investment3 = create(:budget_investment, budget: @budget) + budget_investment1 = create(:budget_investment, budget: budget) + budget_investment2 = create(:budget_investment, budget: budget) + budget_investment3 = create(:budget_investment, budget: budget) valuator1 = create(:valuator, user: create(:user, username: 'Olga'), description: 'Valuator Olga') valuator2 = create(:valuator, user: create(:user, username: 'Miriam'), description: 'Valuator Miriam') @@ -64,7 +67,7 @@ feature 'Admin budget investments' do budget_investment2.valuator_ids = [valuator1.id, valuator2.id] budget_investment3.update(administrator_id: admin.id) - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) within("#budget_investment_#{budget_investment1.id}") do expect(page).to have_content("No admin assigned") @@ -84,18 +87,18 @@ feature 'Admin budget investments' do end scenario "Filtering by budget heading", :js do - group1 = create(:budget_group, name: "Streets", budget: @budget) - group2 = create(:budget_group, name: "Parks", budget: @budget) + group1 = create(:budget_group, name: "Streets", budget: budget) + group2 = create(:budget_group, name: "Parks", budget: budget) group1_heading1 = create(:budget_heading, group: group1, name: "Main Avenue") group1_heading2 = create(:budget_heading, group: group1, name: "Mercy Street") group2_heading1 = create(:budget_heading, group: group2, name: "Central Park") - create(:budget_investment, title: "Realocate visitors", budget: @budget, group: group1, heading: group1_heading1) - create(:budget_investment, title: "Change name", budget: @budget, group: group1, heading: group1_heading2) - create(:budget_investment, title: "Plant trees", budget: @budget, group: group2, heading: group2_heading1) + create(:budget_investment, title: "Realocate visitors", budget: budget, group: group1, heading: group1_heading1) + create(:budget_investment, title: "Change name", budget: budget, group: group1, heading: group1_heading2) + create(:budget_investment, title: "Plant trees", budget: budget, group: group2, heading: group2_heading1) - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_link("Realocate visitors") expect(page).to have_link("Change name") @@ -130,10 +133,10 @@ feature 'Admin budget investments' do user = create(:user, username: 'Admin 1') administrator = create(:administrator, user: user) - create(:budget_investment, title: "Realocate visitors", budget: @budget, administrator: administrator) - create(:budget_investment, title: "Destroy the city", budget: @budget) + create(:budget_investment, title: "Realocate visitors", budget: budget, administrator: administrator) + create(:budget_investment, title: "Destroy the city", budget: budget) - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_link("Realocate visitors") expect(page).to have_link("Destroy the city") @@ -159,12 +162,12 @@ feature 'Admin budget investments' do user = create(:user) valuator = create(:valuator, user: user, description: 'Valuator 1') - budget_investment = create(:budget_investment, title: "Realocate visitors", budget: @budget) + budget_investment = create(:budget_investment, title: "Realocate visitors", budget: budget) budget_investment.valuators << valuator - create(:budget_investment, title: "Destroy the city", budget: @budget) + create(:budget_investment, title: "Destroy the city", budget: budget) - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_link("Realocate visitors") expect(page).to have_link("Destroy the city") @@ -193,13 +196,13 @@ feature 'Admin budget investments' do 'under_valuation' => 'Under valuation', 'valuation_finished' => 'Valuation finished' } - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).not_to have_link(filters_links.values.first) filters_links.keys.drop(1).each { |filter| expect(page).to have_link(filters_links[filter]) } filters_links.each_pair do |current_filter, link| - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: current_filter) + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: current_filter) expect(page).not_to have_link(link) @@ -210,55 +213,55 @@ feature 'Admin budget investments' do end scenario "Filtering by assignment status" do - assigned = create(:budget_investment, title: "Assigned idea", budget: @budget, administrator: create(:administrator)) - valuating = create(:budget_investment, title: "Evaluating...", budget: @budget) + assigned = create(:budget_investment, title: "Assigned idea", budget: budget, administrator: create(:administrator)) + valuating = create(:budget_investment, title: "Evaluating...", budget: budget) valuating.valuators.push(create(:valuator)) - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'without_admin') + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: 'without_admin') expect(page).to have_content("Evaluating...") expect(page).not_to have_content("Assigned idea") - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'without_valuator') + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: 'without_valuator') expect(page).to have_content("Assigned idea") expect(page).not_to have_content("Evaluating...") end scenario "Filtering by valuation status" do - valuating = create(:budget_investment, budget: @budget, title: "Ongoing valuation", administrator: create(:administrator)) - valuated = create(:budget_investment, budget: @budget, title: "Old idea", valuation_finished: true) + valuating = create(:budget_investment, budget: budget, title: "Ongoing valuation", administrator: create(:administrator)) + valuated = create(:budget_investment, budget: budget, title: "Old idea", valuation_finished: true) valuating.valuators.push(create(:valuator)) valuated.valuators.push(create(:valuator)) - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'under_valuation') + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: 'under_valuation') expect(page).to have_content("Ongoing valuation") expect(page).not_to have_content("Old idea") - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'valuation_finished') + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: 'valuation_finished') expect(page).not_to have_content("Ongoing valuation") expect(page).to have_content("Old idea") - visit admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'all') + visit admin_budget_budget_investments_path(budget_id: budget.id, filter: 'all') expect(page).to have_content("Ongoing valuation") expect(page).to have_content("Old idea") end scenario "Filtering by tag" do - create(:budget_investment, budget: @budget, title: 'Educate the children', tag_list: 'Education') - create(:budget_investment, budget: @budget, title: 'More schools', tag_list: 'Education') - create(:budget_investment, budget: @budget, title: 'More hospitals', tag_list: 'Health') + create(:budget_investment, budget: budget, title: 'Educate the children', tag_list: 'Education') + create(:budget_investment, budget: budget, title: 'More schools', tag_list: 'Education') + create(:budget_investment, budget: budget, title: 'More hospitals', tag_list: 'Health') - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_css(".budget_investment", count: 3) expect(page).to have_content("Educate the children") expect(page).to have_content("More schools") expect(page).to have_content("More hospitals") - visit admin_budget_budget_investments_path(budget_id: @budget.id, tag_name: 'Education') + visit admin_budget_budget_investments_path(budget_id: budget.id, tag_name: 'Education') expect(page).not_to have_content("More hospitals") expect(page).to have_css(".budget_investment", count: 2) @@ -267,8 +270,8 @@ feature 'Admin budget investments' do end scenario "Filtering by tag, display only valuation tags" do - investment1 = create(:budget_investment, budget: @budget, tag_list: 'Education') - investment2 = create(:budget_investment, budget: @budget, tag_list: 'Health') + investment1 = create(:budget_investment, budget: budget, tag_list: 'Education') + investment2 = create(:budget_investment, budget: budget, tag_list: 'Health') investment1.set_tag_list_on(:valuation, 'Teachers') investment2.set_tag_list_on(:valuation, 'Hospitals') @@ -276,14 +279,14 @@ feature 'Admin budget investments' do investment1.save investment2.save - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_select("tag_name", options: ["All tags", "Hospitals", "Teachers"]) end scenario "Filtering by tag, display only valuation tags of the current budget" do new_budget = create(:budget) - investment1 = create(:budget_investment, budget: @budget, tag_list: 'Roads') + investment1 = create(:budget_investment, budget: budget, tag_list: 'Roads') investment2 = create(:budget_investment, budget: new_budget, tag_list: 'Accessibility') investment1.set_tag_list_on(:valuation, 'Roads') @@ -292,15 +295,15 @@ feature 'Admin budget investments' do investment1.save investment2.save - visit admin_budget_budget_investments_path(budget_id: @budget.id) + visit admin_budget_budget_investments_path(budget_id: budget.id) expect(page).to have_select("tag_name", options: ["All tags", "Roads"]) expect(page).not_to have_select("tag_name", options: ["All tags", "Accessibility"]) end scenario "Limiting by max number of investments per heading", :js do - group_1 = create(:budget_group, budget: @budget) - group_2 = create(:budget_group, budget: @budget) + group_1 = create(:budget_group, budget: budget) + group_2 = create(:budget_group, budget: budget) parks = create(:budget_heading, group: group_1) roads = create(:budget_heading, group: group_2) streets = create(:budget_heading, group: group_2) @@ -317,7 +320,7 @@ feature 'Admin budget investments' do create(:budget_investment, heading: streets, cached_votes_up: n, title: "Street with #{n} supports") end - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) [2, 4, 90, 100, 200, 300].each do |n| expect(page).to have_link("Park with #{n} supports") @@ -357,72 +360,67 @@ feature 'Admin budget investments' do context 'Search' do background do - @budget = create(:budget) - @investment_1 = create(:budget_investment, title: "Some investment", budget: @budget) - @investment_2 = create(:budget_investment, title: "Some other investment", budget: @budget) + create(:budget_investment, title: 'Some investment', budget: budget) + create(:budget_investment, title: 'Some other investment', budget: budget, id: 999999) end scenario "Search investments by title" do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) - expect(page).to have_content(@investment_1.title) - expect(page).to have_content(@investment_2.title) + expect(page).to have_content('Some investment') + expect(page).to have_content('Some other investment') fill_in 'title_or_id', with: 'Some investment' click_button 'Search' - expect(page).to have_content(@investment_1.title) - expect(page).not_to have_content(@investment_2.title) + expect(page).to have_content('Some investment') + expect(page).not_to have_content('Some other investment') end scenario 'Search investments by ID' do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) - expect(page).to have_content(@investment_1.title) - expect(page).to have_content(@investment_2.title) + expect(page).to have_content('Some investment') + expect(page).to have_content('Some other investment') - fill_in 'title_or_id', with: @investment_2.id + fill_in 'title_or_id', with: 999999 click_button 'Search' - expect(page).to have_content(@investment_2.title) - expect(page).not_to have_content(@investment_1.title) + expect(page).to have_content('Some other investment') + expect(page).not_to have_content('Some investment') end end context 'Sorting' do background do - @budget = create(:budget) - @investment_1 = create(:budget_investment, title: "BBBB", cached_votes_up: 50, budget: @budget) - @investment_2 = create(:budget_investment, title: "AAAA", cached_votes_up: 25, budget: @budget) - @investment_3 = create(:budget_investment, title: "CCCC", cached_votes_up: 10, budget: @budget) + create(:budget_investment, title: 'B First Investment', cached_votes_up: 50, budget: budget) + create(:budget_investment, title: 'A Second Investment', cached_votes_up: 25, budget: budget) + create(:budget_investment, title: 'C Third Investment', cached_votes_up: 10, budget: budget) end scenario 'Sort by ID' do - visit admin_budget_budget_investments_path(@budget, sort_by: 'id') + visit admin_budget_budget_investments_path(budget, sort_by: 'id') - expect(@investment_1.title).to appear_before(@investment_2.title) - expect(@investment_2.title).to appear_before(@investment_3.title) + expect('B First Investment').to appear_before('A Second Investment') + expect('A Second Investment').to appear_before('C Third Investment') end scenario 'Sort by title' do - visit admin_budget_budget_investments_path(@budget, sort_by: 'title') + visit admin_budget_budget_investments_path(budget, sort_by: 'title') - expect(@investment_2.title).to appear_before(@investment_1.title) - expect(@investment_1.title).to appear_before(@investment_3.title) + expect('A Second Investment').to appear_before('B First Investment') + expect('B First Investment').to appear_before('C Third Investment') end scenario 'Sort by supports' do - visit admin_budget_budget_investments_path(@budget, sort_by: 'supports') + visit admin_budget_budget_investments_path(budget, sort_by: 'supports') - expect(@investment_3.title).to appear_before(@investment_2.title) - expect(@investment_2.title).to appear_before(@investment_1.title) + expect('C Third Investment').to appear_before('A Second Investment') + expect('A Second Investment').to appear_before('B First Investment') end end context 'Show' do - background do - @administrator = create(:administrator, user: create(:user, username: 'Ana', email: 'ana@admins.org')) - end scenario 'Show the investment details' do valuator = create(:valuator, user: create(:user, username: 'Rachel', email: 'rachel@valuators.org')) @@ -431,7 +429,7 @@ feature 'Admin budget investments' do price_first_year: 1000, feasibility: "unfeasible", unfeasibility_explanation: 'It is impossible', - administrator: @administrator) + administrator: administrator) budget_investment.valuators << valuator visit admin_budget_budget_investments_path(budget_investment.budget) @@ -461,7 +459,7 @@ feature 'Admin budget investments' do finished_budget = create(:budget, :finished) budget_investment = create(:budget_investment, budget: finished_budget, - administrator: @administrator) + administrator: administrator) visit admin_budget_budget_investments_path(budget_investment.budget) click_link budget_investment.title @@ -698,15 +696,15 @@ feature 'Admin budget investments' do context "Selecting" do - let!(:unfeasible_bi) { create(:budget_investment, :unfeasible, budget: @budget, title: "Unfeasible project") } - let!(:feasible_bi) { create(:budget_investment, :feasible, budget: @budget, title: "Feasible project") } - let!(:feasible_vf_bi) { create(:budget_investment, :feasible, :finished, budget: @budget, title: "Feasible, VF project") } - let!(:selected_bi) { create(:budget_investment, :selected, budget: @budget, title: "Selected project") } - let!(:winner_bi) { create(:budget_investment, :winner, budget: @budget, title: "Winner project") } - let!(:undecided_bi) { create(:budget_investment, :undecided, budget: @budget, title: "Undecided project") } + let!(:unfeasible_bi) { create(:budget_investment, :unfeasible, budget: budget, title: "Unfeasible project") } + let!(:feasible_bi) { create(:budget_investment, :feasible, budget: budget, title: "Feasible project") } + let!(:feasible_vf_bi) { create(:budget_investment, :feasible, :finished, budget: budget, title: "Feasible, VF project") } + let!(:selected_bi) { create(:budget_investment, :selected, budget: budget, title: "Selected project") } + let!(:winner_bi) { create(:budget_investment, :winner, budget: budget, title: "Winner project") } + let!(:undecided_bi) { create(:budget_investment, :undecided, budget: budget, title: "Undecided project") } scenario "Filtering by valuation and selection", :js do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) within('#filter-subnav') { click_link 'Valuation finished' } expect(page).not_to have_content(unfeasible_bi.title) @@ -744,7 +742,7 @@ feature 'Admin budget investments' do end scenario "Aggregating results", :js do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) click_link 'Advanced filters' within('#advanced_filters') { find(:css, "#advanced_filters_[value='undecided']").set(true) } @@ -769,7 +767,7 @@ feature 'Admin budget investments' do end scenario "Showing the selection buttons", :js do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) within("#budget_investment_#{unfeasible_bi.id}") do expect(page).not_to have_link('Select') @@ -793,7 +791,7 @@ feature 'Admin budget investments' do end scenario "Selecting an investment", :js do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) within("#budget_investment_#{feasible_vf_bi.id}") do click_link('Select') @@ -811,7 +809,7 @@ feature 'Admin budget investments' do end scenario "Unselecting an investment", :js do - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) click_link 'Advanced filters' within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) } click_button 'Filter' @@ -825,7 +823,7 @@ feature 'Admin budget investments' do expect(page).not_to have_content(selected_bi.title) expect(page).to have_content('There is 1 investment') - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) within("#budget_investment_#{selected_bi.id}") do expect(page).to have_link('Select') @@ -837,7 +835,7 @@ feature 'Admin budget investments' do context "Selecting csv" do scenario "Downloading CSV file" do - investment = create(:budget_investment, :feasible, budget: @budget, + investment = create(:budget_investment, :feasible, budget: budget, price: 100) valuator = create(:valuator, user: create(:user, username: 'Rachel', email: 'rachel@val.org')) @@ -846,7 +844,7 @@ feature 'Admin budget investments' do admin = create(:administrator, user: create(:user, username: 'Gema')) investment.update(administrator_id: admin.id) - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) click_link "Download current selection" @@ -871,12 +869,12 @@ feature 'Admin budget investments' do end scenario "Downloading CSV file with applied filter" do - investment1 = create(:budget_investment, :unfeasible, budget: @budget, + investment1 = create(:budget_investment, :unfeasible, budget: budget, title: 'compatible') - investment2 = create(:budget_investment, :finished, budget: @budget, + investment2 = create(:budget_investment, :finished, budget: budget, title: 'finished') - visit admin_budget_budget_investments_path(@budget) + visit admin_budget_budget_investments_path(budget) within('#filter-subnav') { click_link 'Valuation finished' } click_link "Download current selection"