Class: ItgTest::TestCase

Inherits:
Object
  • Object
show all
Extended by:
Declarative
Defined in:
lib/itg_test/test_case.rb

Instance Method Summary collapse

Methods included from Declarative

test

Constructor Details

#initialize(url, pid) ⇒ TestCase

Returns a new instance of TestCase.



24
25
26
27
# File 'lib/itg_test/test_case.rb', line 24

def initialize url, pid
  @pid = pid
  @client = ItgTest::HTTPClient.new url
end

Instance Method Details

#assert_difference(expression, difference = 1) ⇒ Object



37
38
39
40
41
42
# File 'lib/itg_test/test_case.rb', line 37

def assert_difference expression, difference=1
  exs = [expression].flatten
  before = exs.map{|e| eval e}
  yield
  exs.each_with_index{ |e, i| assert_equal(before[i] + difference, eval(e), e) }
end

#assert_equal(a, b, msg = "") ⇒ Object



50
51
52
# File 'lib/itg_test/test_case.rb', line 50

def assert_equal a, b, msg=""
  raise "Expected #{a} == #{b} #{msg}" unless a == b
end

#assert_select(selector, content) ⇒ Object



44
45
46
47
48
# File 'lib/itg_test/test_case.rb', line 44

def assert_select selector, content
  unless Nokogiri::HTML(@response).at_css(selector).try(:content) == content
    raise "#{content} doesn't match #{selector} of\n#{@response}"
  end
end

#child_pids(pid) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/itg_test/test_case.rb', line 61

def child_pids pid
  pipe = IO.popen("ps -ef | grep #{pid}")
  pipe.readlines.map do |line|
    parts = line.split(/\s+/)
    parts[2] if parts[3] == pid.to_s and parts[2] != pipe.pid.to_s
  end.compact.map(&:to_i)
end

#get(path) ⇒ Object



33
34
35
# File 'lib/itg_test/test_case.rb', line 33

def get path
  @response = @client.get path
end

#post(path, data) ⇒ Object



29
30
31
# File 'lib/itg_test/test_case.rb', line 29

def post path, data
  @response = @client.post path, data
end

#run_testObject



14
15
16
17
18
19
20
21
22
# File 'lib/itg_test/test_case.rb', line 14

def run_test
  methods.select{|m| /^test_\w+$/ =~ m.to_s}.each{|m| send(m); print "."}
  puts "\nFinished Tests"
rescue Exception => e
  puts "#{e.class}: #{e}"
  puts e.backtrace
ensure
  wait_exit
end

#wait_exitObject



54
55
56
57
58
59
# File 'lib/itg_test/test_case.rb', line 54

def wait_exit
  child_pids(@pid).each{|p| Process.kill "INT", p} # kill grand_children
  Process.kill "INT", @pid # kill child
  Process.wait
  exit
end