How Not To Crash Rails 4 With System Tests

SengMing Tan
Aug 9, 2017 · 2 min read

You can also read this over here for better syntax highlighting


Michael Heizer’s North, East, South, West, 1967/2002 seen at DIA:Beacon.

I fixed a Javascript bug and wanted to write a system test for it. Unfortunately I have to run the test on multiple versions of Rails, and system tests only started appearing with newer versions of Rails. Here are a few things I tried to stop it from crashing on Rails 4.

According to the Rails docs you need two files to get started with system tests,

# application_system_test_case.rb
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :poltergeist
end
# dummy_spec.rb
require 'application_system_test_case'
class DummySpec < ApplicationSystemTestCase
test "loading the default javascript helper" do
visit new_stripe_url
assert_text 'This page tests the loading and initialization of Stripe JS'
end
end

While this works fine on Rails 5.1, running the same test on Rails 4 will cause a NameError since ActionDispatch::SystemTestCase does not exist. An easy way to get around it is by rescuing it,

# application_system_test_case.rb (nothing changed!)
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :poltergeist
end
# dummy_spec.rb (adds a rescue)
begin
require 'application_system_test_case'
class DummySpec < ApplicationSystemTestCase
test "loading the default javascript helper" do
visit new_stripe_url
assert_text 'This page tests the loading and initialization of Stripe JS'
end
end
rescue NameError
warn 'WARNING: Skipping because this version of Rails version does not support it!'
end

But this looked terrible. I ended up using a null class,

# application_system_test_case.rb (selectively loads the null class) SystemTestCaseKlass = defined?(ActionDispatch::SystemTestCase) ? ActionDispatch::SystemTestCase : NullSystemTestCaseclass ApplicationSystemTestCase < SystemTestCaseKlass
driven_by :poltergeist
end
# null_system_test_case.rb (new file!)class NullSystemTestCase
def self.driven_by(_, _); end
def self.test(_)
warn 'WARNING: Skipping because this version of Rails does not support it!'
end
end
# dummy_spec.rb (nothing changed)require 'application_system_test_case'
class DummySpec < ApplicationSystemTestCase
test "loading the default javascript helper" do
visit new_stripe_url
assert_text 'This page tests the loading and initialization of Stripe JS'
end
end

I think this looks nicer! And if I ever wanted to add another system test I won’t have to futz around with rescues. Anyways, this is my take on how not to crash my tests when I’m using newer Rails features. How would you have done it?


Originally published at tansengming.github.io on August 9, 2017.

SengMing Tan

Written by

Easily amused. Constantly bemused. Also at tansengming.github.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade