Skip to content

Commit

Permalink
Add System Test for viewing a Seat's details
Browse files Browse the repository at this point in the history
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`][test] and [`rails
test:system`][system-test].

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][tdd-cycle], and will be resolved in future commits.

[default-task]: https://github.com/ruby/rake/tree/v13.0.1#label-Usage
[test]: https://guides.rubyonrails.org/v6.0/command_line.html#rails-test
[system-test]: https://guides.rubyonrails.org/v6.0/testing.html#implementing-a-system-test
[tdd-cycle]: https://en.wikipedia.org/wiki/Test-driven_development#Development_style
  • Loading branch information
jho406 committed Oct 8, 2023
1 parent 2216708 commit 138eee0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -34,6 +34,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'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -96,6 +96,8 @@ GEM
railties (>= 6.0.0)
date (3.3.3)
erubi (1.12.0)
factory_bot (5.0.2)
activesupport (>= 4.2.0)
ffi (1.15.5)
form_props (0.0.3)
actionview (>= 7.0.0)
Expand Down Expand Up @@ -231,6 +233,7 @@ DEPENDENCIES
byebug
capybara (>= 2.15)
cssbundling-rails (~> 1.3)
factory_bot (~> 5.0.0)
form_props (~> 0.0.3)
jbuilder (~> 2.7)
jsbundling-rails (~> 1.2)
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Expand Up @@ -4,3 +4,5 @@
require_relative 'config/application'

Rails.application.load_tasks

task default: ["test", "test:system"]
30 changes: 30 additions & 0 deletions 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
19 changes: 19 additions & 0 deletions 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
5 changes: 3 additions & 2 deletions test/test_helper.rb
Expand Up @@ -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

0 comments on commit 138eee0

Please sign in to comment.