Created
March 7, 2013 05:55
-
-
Save toolmantim/5105831 to your computer and use it in GitHub Desktop.
A simple Rails 3/4 integration test that spiders your application's links, checking that none return a 500 or 404
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class AllLinksRenderTest < ActionDispatch::IntegrationTest | |
test "all links render" do | |
self.host = "example.com" | |
spider "/" | |
end | |
def spider(path, spidered=[]) | |
STDERR.puts "Checking #{path}" | |
spidered << path | |
get path | |
if redirect? | |
if URI(response.location).host == self.host | |
follow_redirect! | |
else | |
puts "Ignoring external redirect: #{path} -> #{response.location}" | |
return | |
end | |
end | |
# Assert page looks okay | |
assert_response :success, "#{path} returned #{response.code} instead of 200\n\nResponse body:\n#{response.body}\n" | |
# Add your own assertions (e.g. there is a signup link) | |
# Spider all links | |
css_select("a[href^='/']").each do |link| | |
href = link.attributes['href'].gsub(/#.*$/,'') # strip frag-ids | |
spider(href, spidered) unless spidered.include?(href) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so good.