Add System Test for viewing a Seat’s details
This commit introduces factory_bot
and configures MiniTest to use it
instead of loading our application’s fixture files.
The first test drives the browser by visiting the Benedum Center’s
Orchestra floor, then clicking on seat AA-101
.
The click_on_seat
help is necessary due to Capybara’s inability to
treat <a>
elements nested within <svg>
elements as if they were
normal <a>
elements. Capybara’s selector strategies include a matcher
that finds <a>
elements based on their title
attribute text, or
their aria-label
attribute text. However, when matching elements exist
within an <svg>
, they are not found.
To counteract this limitation, the click_on_seat
uses an aria-label
attribute CSS selector to determine which element to click.
Running the tests
To simplify executing the entire test suite, this commit declares the
default
Rake task to execute both rails test
and rails
test:system
.
This diff intentionally commits a failing test suite to the project’s
revisions history. It represents the “Red” phase of a “Red, Green,
Refactor” cycle , and will be resolved in future commits.
Collapse Gemfile
Expand Gemfile
Gemfile
diff --git a/Gemfile b/Gemfile
index 575651b..0472bb4 100644
--- a/Gemfile
+++ b/Gemfile
@@ -40,6 +40,7 @@ end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
+ gem 'factory_bot', '~> 5.0.0'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
Collapse Gemfile.lock
Expand Gemfile.lock
Gemfile.lock
diff --git a/Gemfile.lock b/Gemfile.lock
index ccf80c4..6d80f68 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -75,6 +75,8 @@ GEM
concurrent-ruby (1.1.6)
crass (1.0.6)
erubi (1.9.0)
+ factory_bot (5.0.2)
+ activesupport (>= 4.2.0)
ffi (1.12.2)
globalid (0.4.2)
activesupport (>= 4.2.0)
@@ -203,6 +205,7 @@ DEPENDENCIES
bootsnap (>= 1.4.2)
byebug
capybara (>= 2.15)
+ factory_bot (~> 5.0.0)
jbuilder (~> 2.7)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
Collapse Rakefile
Expand Rakefile
Rakefile
diff --git a/Rakefile b/Rakefile
index e85f913..c9b01f5 100644
--- a/Rakefile
+++ b/Rakefile
@@ -4,3 +4,5 @@
require_relative 'config/application'
Rails.application.load_tasks
+
+task default: ["test", "test:system"]
Collapse test/factories.rb
Expand test/factories.rb
test/factories.rb
diff --git a/test/factories.rb b/test/factories.rb
new file mode 100644
index 0000000..45a2575
--- /dev/null
+++ b/test/factories.rb
@@ -0,0 +1,30 @@
+FactoryBot.define do
+ factory :benedum_center, class: "Venue" do
+ name { "Benedum Center" }
+ slug { "benedum_center" }
+ end
+
+ factory :orchestra, class: "Floor" do
+ association :venue, factory: [:benedum_center]
+
+ name { "Orchestra" }
+ slug { "orchestra" }
+ end
+
+ factory :section do
+ association :floor, factory: [:orchestra]
+
+ name { "Section #{id}" }
+ slug { "section-#{id}" }
+ price { 10_00 }
+ end
+
+ factory :seat do
+ association :section
+
+ sequence(:number)
+ row { "AA" }
+ x { 0 }
+ y { 0 }
+ end
+end
Collapse test/system/visitor_views_seats_test.rb
Expand test/system/visitor_views_seats_test.rb
test/system/visitor_views_seats_test.rb
diff --git a/test/system/visitor_views_seats_test.rb b/test/system/visitor_views_seats_test.rb
new file mode 100644
index 0000000..6678b42
--- /dev/null
+++ b/test/system/visitor_views_seats_test.rb
@@ -0,0 +1,19 @@
+require "application_system_test_case"
+
+class VisitorViewsSeatsTest < ApplicationSystemTestCase
+ test "visiting the seats index" do
+ venue = create(:benedum_center)
+ floor = create(:orchestra, venue: venue)
+ section = create(:section, floor: floor, price: 10_00)
+ seat = create(:seat, row: "AA", number: "101", section: section)
+
+ visit("/venues/benedum_center/floors/orchestra/seats")
+ click_on_seat("AA-101")
+
+ assert_text("$10.00")
+ end
+
+ def click_on_seat(row_number)
+ find(%{[aria-label*="#{row_number}"]}).click
+ end
+end
Collapse test/test_helper.rb
Expand test/test_helper.rb
test/test_helper.rb
diff --git a/test/test_helper.rb b/test/test_helper.rb
index d5300f8..a07007b 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -6,8 +6,9 @@ class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
- # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
- fixtures :all
+ include FactoryBot::Syntax::Methods
+
+ FactoryBot.find_definitions
# Add more helper methods to be used by all tests here...
end