Test broken links in rails application

I have an existing rails application where I want to test broken links. in which testing should I use integration testing or rspec? Is new to BDD.

Thanks in advance!

+6
ruby-on-rails
source share
2 answers

Well, if you write the right integration tests (for example, with Cucumber or something else), you should touch the entire interface of the interface. For example. click links, fill out forms, etc.

This test suite will give you an immediate warning if you have dead links in your application.

0
source share

I think it depends on why you want to "check" for broken links.

Scenario 1) Proper use of URLs contributed by users may use a method similar to this:

def active_link?(url) uri = URI.parse(url) response = nil Net::HTTP.start(uri.host, uri.port) { |http| response = http.head(uri.path.size > 0 ? uri.path : "/") } return response.code == "200" end 

Then you can use this in your Rspec tests:

 active_link?('http://example.com').should be 

Scenario 2) . You just want to make sure all the links on your site work:

If so, you can try using the wget 'Unix command:

 wget --spider -r -l 1 --header='User-Agent: Mozilla/5.0' http://example.com 2>&1 | grep -B 2 '404' 

With Scenario 2, it will output all 404s to your terminal screen. This is a pretty simple question to put on a rake team; Jason Seir has a wonderful blog entry (http://jasonseifer.com/2010/04/06/rake-tutorial)

+5
source share

All Articles