Module: JSpecRunner::Extensions

Defined in:
lib/jspec_runner.rb

Instance Method Summary collapse

Instance Method Details

#assert_jspec(options = {}) ⇒ Object



43
44
45
46
# File 'lib/jspec_runner.rb', line 43

def assert_jspec(options={})
  result = jspec(options)
  assert_block(build_message("One or more specs failed", result[:output])) { result[:passed] }
end

#jspec(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jspec_runner.rb', line 48

def jspec(options={})
  opts = {
    :execs => []
  }.merge(options)
  scripts = Nokogiri::HTML.parse(@response.body).css('script').map(&:to_s)
  
  exec_commands = opts[:execs].map {|spec| ".exec('#{spec}')"}
  run_suites = <<-HTML
    <script>
      function runSuites() {
        JSpec
        #{exec_commands.join("\n")}
        .run({ fixturePath: 'fixtures' })
        .report()
      }
    </script>
  HTML
  
  html = jspec_dom.gsub(/<userscripts\/>/, scripts.join("\n")).gsub(/<runSuites\/>/, run_suites)
  
  # TODO: consider making this gem use Harmony and not depend on HolyGrail -- too much duplication here
  XhrProxy.context = self
  @__page = Harmony::Page.new(rewrite_script_paths(html))
  Harmony::Page::Window::BASE_RUNTIME.wait
  
  result = {:passed => true}
  js_dom = HTML::Document.new(@__page.to_html, false, true).root
  HTML::Selector.new(".has-failures").select(js_dom).each do |wrapper|
    output = HTML::Selector.new("tr").select(wrapper).map do |row|
      if row.attributes["class"] == "description"
        "\n" + text_only(row)
      elsif HTML::Selector.new(".pass").select(row).any?
        "\e[32mPassed\e[0m: " + text_only(row)
      elsif HTML::Selector.new(".failed").select(row).any?
        "\e[31mFailed\e[0m: " + text_only(row)
      else
        text_only(row)
      end
    end
    output = output.join("\n") + "\n\nJSpec: \e[32m#{select_text(wrapper, ".heading .passes em")} passes\e[0m, \e[31m#{select_text(wrapper, ".heading .failures em")} failures\e[0m\n"
    # puts js_dom
    
    result = {:passed => false, :output => output}
  end
  return result
end