Class: RSpec::Jasmine::SpecBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/jasmine/spec_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(world, config = {}) ⇒ SpecBuilder

Returns a new instance of SpecBuilder.



8
9
10
11
12
# File 'lib/rspec/jasmine/spec_builder.rb', line 8

def initialize(world, config = {})
  @world   = world
  @config  = config
  @running = false
end

Instance Method Details

#browserObject



18
19
20
# File 'lib/rspec/jasmine/spec_builder.rb', line 18

def browser
  @config[:browser] || ENV["JASMINE_BROWSER"] || 'firefox'
end

#declare_spec!(parent, spec) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspec/jasmine/spec_builder.rb', line 96

def declare_spec!(parent, spec)
  me = self
  
  spec_id = spec['id']
  @spec_ids << spec_id
  
  meta = parent.([])
  block = proc do
    @result = Jasmine::ExampleResult.new(me.results_for(spec_id))
    Jasmine.failed = true if @result.failed?
  end
  
  parent.examples << Jasmine::Example.new(parent, spec['name'], meta, block)
end

#declare_suite!(parent, suite) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rspec/jasmine/spec_builder.rb', line 79

def declare_suite!(parent, suite)
  me = self
  
  parent.describe suite['name'] do
    suite['children'].each do |suite_or_spec|
      case suite_or_spec["type"]
      when "suite"
        me.declare_suite!(self, suite_or_spec)
      when "spec"
        me.declare_spec!(self, suite_or_spec)
      else
        raise "unknown type #{type} for #{suite_or_spec.inspect}"
      end
    end
  end
end

#eval_js(script) ⇒ Object



56
57
58
# File 'lib/rspec/jasmine/spec_builder.rb', line 56

def eval_js(script)
  @client.eval_js(script)
end

#generate_report!Object



73
74
75
76
77
# File 'lib/rspec/jasmine/spec_builder.rb', line 73

def generate_report!
  @test_suites.to_a.each do |suite| 
    declare_suite!(@world, suite)
  end
end

#hostObject



22
23
24
# File 'lib/rspec/jasmine/spec_builder.rb', line 22

def host
  @config[:host] || ENV["JASMINE_HOST"] || 'localhost'
end

#json_generate(obj) ⇒ Object



111
112
113
# File 'lib/rspec/jasmine/spec_builder.rb', line 111

def json_generate(obj)
  @client.json_generate(obj)
end

#load_resultsObject



120
121
122
123
124
125
126
127
# File 'lib/rspec/jasmine/spec_builder.rb', line 120

def load_results
  @spec_ids.each_slice(50).inject({}) do |results, slice|
    results.merge(eval_js(<<-JS))
      var result = jsApiReporter.resultsForSpecs(#{json_generate(slice)}); 
      return JSON.stringify(result);
    JS
  end
end

#load_suite_info!Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/jasmine/spec_builder.rb', line 60

def load_suite_info!
  started = Time.now
  
  while !eval_js('return jsApiReporter && jsApiReporter.started') do
    raise "couldn't connect to Jasmine after 60 seconds" if (started + 60 < Time.now)
    sleep 0.1
  end
  
  @spec_ids = []
  @spec_results = nil
  @test_suites = eval_js("var result = jsApiReporter.suites(); return JSON.stringify(result)")
end

#portObject



26
27
28
# File 'lib/rspec/jasmine/spec_builder.rb', line 26

def port
  @config[:port] || ENV["JASMINE_PORT"] || '5001'
end

#results_for(spec_id) ⇒ Object



115
116
117
118
# File 'lib/rspec/jasmine/spec_builder.rb', line 115

def results_for(spec_id)
  @spec_results ||= load_results
  @spec_results[spec_id.to_s]
end

#startObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rspec/jasmine/spec_builder.rb', line 34

def start
  @client  = Jasmine::SeleniumDriver.new(browser)
  @running = true
  
  @client.connect(url)
  
  puts "Running test suite with Jasmine against: #{url}"
  
  load_suite_info!
  generate_report!
  wait_for_suites_to_finish
end

#stopObject



47
48
49
50
# File 'lib/rspec/jasmine/spec_builder.rb', line 47

def stop
  @client.disconnect if @running
  @running = false
end

#suiteObject



14
15
16
# File 'lib/rspec/jasmine/spec_builder.rb', line 14

def suite
  @config[:suite] || '/tests'
end

#urlObject



30
31
32
# File 'lib/rspec/jasmine/spec_builder.rb', line 30

def url
  "http://#{host}:#{port}#{suite}"
end

#wait_for_suites_to_finishObject



52
53
54
# File 'lib/rspec/jasmine/spec_builder.rb', line 52

def wait_for_suites_to_finish
  sleep 0.1 until eval_js('return jsApiReporter.finished')
end