From 2aabcce70d9cfcb734410ebd8783308ead4959f6 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Sun, 19 Oct 2014 12:13:17 +0100 Subject: [PATCH 1/4] Feature tests for scientific names. --- spec/features/scientific_name_spec.rb | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 spec/features/scientific_name_spec.rb diff --git a/spec/features/scientific_name_spec.rb b/spec/features/scientific_name_spec.rb new file mode 100644 index 000000000..5a3a82989 --- /dev/null +++ b/spec/features/scientific_name_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +feature "Scientific names" do + let!(:zea_mays) { FactoryGirl.create(:zea_mays) } + let(:crop) { zea_mays.crop } + + scenario "Display scientific names on crop page" do + visit crop_path(zea_mays.crop) + expect(page).to have_content zea_mays.scientific_name + end + + scenario "Index page for scientific names" do + visit scientific_names_path + expect(page).to have_content zea_mays.scientific_name + end + + context "User is a crop wrangler" do + let!(:crop_wranglers) { FactoryGirl.create_list(:crop_wrangling_member, 3) } + let(:member){crop_wranglers.first} + + before :each do + login_as(member) + end + + scenario "Crop wranglers can edit scientific names" do + visit crop_path(crop) + expect(page).to have_content "CROP WRANGLER" + expect(page).to have_content zea_mays.scientific_name + expect(page).to have_link "Edit", :href => edit_scientific_name_path(zea_mays) + within('.scientific_names') { click_on "Edit" } + expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" + fill_in 'Scientific name', with: "Zea mirabila" + click_on "Save" + expect(page).to have_content "Zea mirabila" + expect(page).to have_content 'Scientific name was successfully updated' + end + + scenario "Crop wranglers can delete scientific names" do + visit crop_path(zea_mays.crop) + expect(page).to have_link "Delete", + href: scientific_name_path(zea_mays) + within('.scientific_names') { click_on "Delete" } + expect(page).to_not have_content zea_mays.scientific_name + expect(page).to have_content 'Scientific name was successfully deleted' + end + + scenario "Crop wranglers can add scientific names" do + visit crop_path(crop) + expect(page).to have_link "Add", + href: new_scientific_name_path(crop_id: crop.id) + within('.scientific_names') { click_on "Add" } + expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" + fill_in 'Scientific name', with: "Zea mirabila" + click_on "Save" + expect(page).to have_content "Zea mirabila" + expect(page).to have_content 'Scientific name was successfully created' + end + + scenario "The show-scientific-name page works" do + visit scientific_name_path(zea_mays) + expect(page).to have_content zea_mays.crop.name + end + + end + +end From 6de2112c4d042e95d0555d9b0ac8018ac6606cb2 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Sun, 19 Oct 2014 12:16:05 +0100 Subject: [PATCH 2/4] Display note on successful sciname deletion. --- app/controllers/scientific_names_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/scientific_names_controller.rb b/app/controllers/scientific_names_controller.rb index 202c3942c..3911a6618 100644 --- a/app/controllers/scientific_names_controller.rb +++ b/app/controllers/scientific_names_controller.rb @@ -83,7 +83,9 @@ class ScientificNamesController < ApplicationController @scientific_name.destroy respond_to do |format| - format.html { redirect_to @crop } + format.html { + redirect_to @crop, notice: 'Scientific name was successfully deleted.' + } format.json { head :no_content } end end From 56b7d89d9ea709033ae5d74eba182a0c694240bb Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Sun, 19 Oct 2014 12:19:55 +0100 Subject: [PATCH 3/4] Link scinames to crops, don't show numeric crop IDs --- app/views/scientific_names/show.html.haml | 2 +- spec/features/scientific_name_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/scientific_names/show.html.haml b/app/views/scientific_names/show.html.haml index 23e3a3c47..a3efe2d6f 100644 --- a/app/views/scientific_names/show.html.haml +++ b/app/views/scientific_names/show.html.haml @@ -5,7 +5,7 @@ = @scientific_name.scientific_name %p %b Crop: - = @scientific_name.crop_id + = link_to @scientific_name.crop, @scientific_name.crop = link_to 'Edit', edit_scientific_name_path(@scientific_name), :class => 'btn btn-default btn-xs' \| diff --git a/spec/features/scientific_name_spec.rb b/spec/features/scientific_name_spec.rb index 5a3a82989..01093a9c2 100644 --- a/spec/features/scientific_name_spec.rb +++ b/spec/features/scientific_name_spec.rb @@ -58,7 +58,8 @@ feature "Scientific names" do scenario "The show-scientific-name page works" do visit scientific_name_path(zea_mays) - expect(page).to have_content zea_mays.crop.name + expect(page).to have_link zea_mays.crop.name, + href: crop_path(zea_mays.crop) end end From b17aaf262dc04b916e46269d32362ba4ac74683a Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Sun, 19 Oct 2014 12:25:41 +0100 Subject: [PATCH 4/4] Test HTTP statuses in sciname feature tests This makes it easier to tell the difference between "test failed because the expected content isn't there" and "test failed because the whole page is broken". It also guards against "test passed incorrectly because the expected content was part of the error message". --- spec/features/scientific_name_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/features/scientific_name_spec.rb b/spec/features/scientific_name_spec.rb index 01093a9c2..1fda79d42 100644 --- a/spec/features/scientific_name_spec.rb +++ b/spec/features/scientific_name_spec.rb @@ -6,11 +6,13 @@ feature "Scientific names" do scenario "Display scientific names on crop page" do visit crop_path(zea_mays.crop) + expect(page.status_code).to equal 200 expect(page).to have_content zea_mays.scientific_name end scenario "Index page for scientific names" do visit scientific_names_path + expect(page.status_code).to equal 200 expect(page).to have_content zea_mays.scientific_name end @@ -24,10 +26,12 @@ feature "Scientific names" do scenario "Crop wranglers can edit scientific names" do visit crop_path(crop) + expect(page.status_code).to equal 200 expect(page).to have_content "CROP WRANGLER" expect(page).to have_content zea_mays.scientific_name expect(page).to have_link "Edit", :href => edit_scientific_name_path(zea_mays) within('.scientific_names') { click_on "Edit" } + expect(page.status_code).to equal 200 expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" fill_in 'Scientific name', with: "Zea mirabila" click_on "Save" @@ -40,6 +44,7 @@ feature "Scientific names" do expect(page).to have_link "Delete", href: scientific_name_path(zea_mays) within('.scientific_names') { click_on "Delete" } + expect(page.status_code).to equal 200 expect(page).to_not have_content zea_mays.scientific_name expect(page).to have_content 'Scientific name was successfully deleted' end @@ -49,15 +54,18 @@ feature "Scientific names" do expect(page).to have_link "Add", href: new_scientific_name_path(crop_id: crop.id) within('.scientific_names') { click_on "Add" } + expect(page.status_code).to equal 200 expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" fill_in 'Scientific name', with: "Zea mirabila" click_on "Save" + expect(page.status_code).to equal 200 expect(page).to have_content "Zea mirabila" expect(page).to have_content 'Scientific name was successfully created' end scenario "The show-scientific-name page works" do visit scientific_name_path(zea_mays) + expect(page.status_code).to equal 200 expect(page).to have_link zea_mays.crop.name, href: crop_path(zea_mays.crop) end