Class: Tour

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Test::Unit::Assertions, Webrat::Matchers, Webrat::SaveAndOpenPage
Defined in:
lib/tour.rb

Overview

A tour is essentially a test suite file. A Tour subclass encapsulates a set of tests that can be done, and may contain helper and support methods for a given task. If you have a two or three paths through a specific area of your website, define a tour for that area and create test_ methods for each type of test to be done.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, tours, number, tour_id) ⇒ Tour

Returns a new instance of Tour.



73
74
75
76
77
# File 'lib/tour.rb', line 73

def initialize(host, tours, number, tour_id)
  @host, @tours, @number, @tour_id = host, tours, number, tour_id
  @tour_type = self.send(:class).to_s
  @webrat_session = Webrat::MechanizeAdapter.new()
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/tour.rb', line 20

def host
  @host
end

#numberObject (readonly)

Returns the value of attribute number.



20
21
22
# File 'lib/tour.rb', line 20

def number
  @number
end

#tour_idObject (readonly)

Returns the value of attribute tour_id.



20
21
22
# File 'lib/tour.rb', line 20

def tour_id
  @tour_id
end

#tour_typeObject (readonly)

Returns the value of attribute tour_type.



20
21
22
# File 'lib/tour.rb', line 20

def tour_type
  @tour_type
end

#toursObject (readonly)

Returns the value of attribute tours.



20
21
22
# File 'lib/tour.rb', line 20

def tours
  @tours
end

#webrat_sessionObject (readonly)

Returns the value of attribute webrat_session.



20
21
22
# File 'lib/tour.rb', line 20

def webrat_session
  @webrat_session
end

Class Method Details

.make_tour(tour_name, host = "http://localhost:3000", tours = [], number = 1, tour_id = nil) ⇒ Object

Factory method, creates the named child class instance



122
123
124
# File 'lib/tour.rb', line 122

def self.make_tour(tour_name,host="http://localhost:3000",tours=[],number=1,tour_id=nil)
  tour_name.classify.constantize.new(host,tours,number,tour_id)
end

.tests(tour_name) ⇒ Object



113
114
115
# File 'lib/tour.rb', line 113

def self.tests(tour_name)
  Tour.make_tour(tour_name).tests
end

.tour?(tour_name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/tour.rb', line 117

def self.tour?(tour_name)
  Object.const_defined?(tour_name.classify) && tour_name.classify.constantize.ancestors.include?(Tour)
end

.tours(filter = []) ⇒ Object

Lists tours in tours folder. If a string is given, filters the list by that string. If an array of filter strings is given, returns items that match ANY filter string in the array.



102
103
104
105
106
107
108
109
110
111
# File 'lib/tour.rb', line 102

def self.tours(filter=[])
  filter = [filter].flatten
  # All files in tours folder, stripped to basename, that match any item in filter
  # I do loves me a long chain. This returns an array containing
  # 1. All *.rb files in tour folder (recursive)
  # 2. Each filename stripped to its basename
  # 3. If you passed in any filters, these basenames are rejected unless they match at least one filter
  # 4. The filenames remaining are then checked to see if they define a class of the same name that inherits from Tour
  Dir[File.join('.', 'tours', '**', '*.rb')].map {|fn| File.basename(fn, ".rb")}.select {|fn| filter.size.zero? || filter.any?{|f| fn =~ /#{f}/}}.select {|tour| Tour.tour? tour }
end

Instance Method Details

#after_tourObject

after_tour runs once per tour, after all the tests have run



87
# File 'lib/tour.rb', line 87

def after_tour; end

#before_tourObject

before_tour runs once per tour, before any tests get run



84
# File 'lib/tour.rb', line 84

def before_tour; end

#run_test(test_name) ⇒ Object

Raises:



132
133
134
135
136
137
138
# File 'lib/tour.rb', line 132

def run_test(test_name)
  @test = "test_#{test_name}"
  raise TourBusException.new("run_test couldn't run test '#{test_name}' because this tour did not respond to :#{@test}") unless respond_to? @test
  setup
  send @test
  teardown
end

#setupObject



89
90
# File 'lib/tour.rb', line 89

def setup
end

#teardownObject



92
93
# File 'lib/tour.rb', line 92

def teardown
end

#testsObject

Returns list of tests in this tour. (Meant to be run on a subclass instance; returns the list of tests available).



128
129
130
# File 'lib/tour.rb', line 128

def tests
  methods.grep(/^test_/).map {|m| m.sub(/^test_/,'')}
end

#visit(url, data = nil) ⇒ Object



79
80
81
# File 'lib/tour.rb', line 79

def visit(url, data=nil)
  get url, data
end

#wait(time) ⇒ Object



95
96
97
# File 'lib/tour.rb', line 95

def wait(time)
  sleep time.to_i
end