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.



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

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::MechanizeSession.new
  visit @host if @host
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 = "localhost:3000", tours = [], number = 1, tour_id = nil) ⇒ Object

Factory method, creates the named child class instance



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

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

.tests(tour_name) ⇒ Object



109
110
111
# File 'lib/tour.rb', line 109

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

.tour?(tour_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



98
99
100
101
102
103
104
105
106
107
# File 'lib/tour.rb', line 98

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



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

def after_tour; end

#before_tourObject

before_tour runs once per tour, before any tests get run



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

def before_tour; end

#run_test(test_name) ⇒ Object

Raises:



128
129
130
131
132
133
134
# File 'lib/tour.rb', line 128

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



85
86
# File 'lib/tour.rb', line 85

def setup
end

#teardownObject



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

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).



124
125
126
# File 'lib/tour.rb', line 124

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

#wait(time) ⇒ Object



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

def wait(time)
  sleep time.to_i
end