类: RSpec::Core::Reporter

继承
Object
  • Object
显示全部
定义于
lib/rspec/core/reporter.rb

概述

报告器会向监听器(通常是规格套件运行的格式化程序)发送通知。

实例方法摘要 折叠

构造函数详细信息

#initialize(configuration) ⇒Reporter

返回 Reporter 的新实例。

14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/core/reporter.rb', line 14
def initialize(configuration)
  @configuration = configuration
  @listeners = Hash.new { |h, k| h[k] = Set.new }
  @examples = []
  @failed_examples = []
  @pending_examples = []
  @duration = @start = @load_time = nil
  @non_example_exception_count = 0
  @setup_default = lambda {}
  @setup = false
  @profiler = nil
end

实例方法详细信息

#exit_early(exit_code) ⇒void

报告在未运行任何示例的情况下提前退出的运行。

参数

  • exit_code (Integer)

    报告器将返回的退出代码

84
85
86
# File 'lib/rspec/core/reporter.rb', line 84
def exit_early(exit_code)
  report(0) { exit_code }
end

#message(message) ⇒void

向支持的格式化程序发送自定义消息。

参数

  • message (#to_s)

    要发送给格式化程序的消息对象

99
100
101
# File 'lib/rspec/core/reporter.rb', line 99
def message(message)
  notify :message, Notifications::MessageNotification.new(message)
end

#publish(event, options = {}) ⇒void

向支持的注册格式化程序发布自定义事件。

参数

  • event (Symbol)

    要在格式化程序上触发的自定义事件名称

  • options (Hash) (默认值: {})

    通过 CustomNotification 提供的参数哈希

另请参阅

108
109
110
111
112
113
114
# File 'lib/rspec/core/reporter.rb', line 108
def publish(event, options={})
  if RSPEC_NOTIFICATIONS.include? event
    raise "RSpec::Core::Reporter#publish is intended for sending custom " \
          "events not internal RSpec ones, please rename your custom event."
  end
  notify event, Notifications::CustomNotification.for(options)
end

#register_listener(listener, *notifications) ⇒void

将监听器注册到通知列表。报告器会将事件通知发送给所有注册的监听器。

参数

  • listener (Object)

    希望收到报告器事件通知的对象

  • notifications (Array)

    符号数组表示监听器希望订阅的事件

37
38
39
40
41
42
# File 'lib/rspec/core/reporter.rb', line 37
def register_listener(listener, *notifications)
  notifications.each do |notification|
    @listeners[notification.to_sym] << listener
  end
  true
end

#report(count, &block) ⇒ void #report(count, &block) ⇒ void

初始化报告运行并自身让渡以进行进一步报告。需要块,以便报告器可以管理运行后的清理工作。

示例


reporter.report(group.examples.size) do |r|
  example_groups.map {|g| g.run(r) }
end

参数

  • expected_example_count (Integer)

    正在运行的示例数量

让渡

  • (Block)

    块让渡自身以进行进一步报告。

71
72
73
74
75
76
77
78
# File 'lib/rspec/core/reporter.rb', line 71
def report(expected_example_count)
  start(expected_example_count)
  begin
    yield self
  ensure
    finish
  end
end