C
Chris Gallagher
Hi,
I have a ruby script that I would like to apply unit testing against but
Ive never actually created any previously and the documentation is not
bad but im struggling to apply it to my own code.
Could someone give a couple of examples of how unit testing might be
applied to the following code just to get me started on it.
require 'net/http'
require 'uri'
require 'rexml/document'
require 'rubygems'
require_gem 'activerecord'
include REXML
def fetch(uri_str, limit=10)
fail 'http redirect too deep' if limit.zero?
puts "Scraping: #{uri_str}"
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess
response
when NetHTTPRedirection
fetch(response['location'], limit-1)
else
response.error!
end
end
#Connect to the database
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:username => "root",
:host => "localhost",
assword => "",
:database => "build"
)
puts "connected to build database"
class Result < ActiveRecord::Base
end
records = Result.find
all).each do |records|
response = fetch(records.build_url)
scraped_data = response.body
table_start_pos = scraped_data.index('<table class="index"
width="100%">')
table_end_pos = scraped_data.index('</table>') + 9
height = table_end_pos - table_start_pos
#pick out the table
gathered_data = response.body[table_start_pos,height]
#convert Data to REXML
converted_data = REXML:
ocument.new gathered_data
module_name = XPath.first(converted_data, "//td[@class='data']/a/]")
module_name = module_name
build_status_since = XPath.first(converted_data, "//td[2]/em")
build_status_since = build_status_since.text
build_status_since = build_status_since.slice(/(\d+)
\d+)/)
last_failure = XPath.first(converted_data, "//tbody/tr/td[3]")
last_failure = last_failure.text
last_success = XPath.first(converted_data, "//tbody/tr/td[4]")
last_success = last_success.text
build_number = XPath.first(converted_data, "//tbody/tr/td[5]")
build_number = build_number.text
-----------------
I know that I need to include:
require 'test/unit'
require '../name_of_code_tested'
after that though it gets less clear
any ideas?
cheers.
I have a ruby script that I would like to apply unit testing against but
Ive never actually created any previously and the documentation is not
bad but im struggling to apply it to my own code.
Could someone give a couple of examples of how unit testing might be
applied to the following code just to get me started on it.
require 'net/http'
require 'uri'
require 'rexml/document'
require 'rubygems'
require_gem 'activerecord'
include REXML
def fetch(uri_str, limit=10)
fail 'http redirect too deep' if limit.zero?
puts "Scraping: #{uri_str}"
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess
response
when NetHTTPRedirection
fetch(response['location'], limit-1)
else
response.error!
end
end
#Connect to the database
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:username => "root",
:host => "localhost",
:database => "build"
)
puts "connected to build database"
class Result < ActiveRecord::Base
end
records = Result.find
response = fetch(records.build_url)
scraped_data = response.body
table_start_pos = scraped_data.index('<table class="index"
width="100%">')
table_end_pos = scraped_data.index('</table>') + 9
height = table_end_pos - table_start_pos
#pick out the table
gathered_data = response.body[table_start_pos,height]
#convert Data to REXML
converted_data = REXML:
module_name = XPath.first(converted_data, "//td[@class='data']/a/]")
module_name = module_name
build_status_since = XPath.first(converted_data, "//td[2]/em")
build_status_since = build_status_since.text
build_status_since = build_status_since.slice(/(\d+)
last_failure = XPath.first(converted_data, "//tbody/tr/td[3]")
last_failure = last_failure.text
last_success = XPath.first(converted_data, "//tbody/tr/td[4]")
last_success = last_success.text
build_number = XPath.first(converted_data, "//tbody/tr/td[5]")
build_number = build_number.text
-----------------
I know that I need to include:
require 'test/unit'
require '../name_of_code_tested'
after that though it gets less clear
any ideas?
cheers.