模块:RSpec::Core::Formatters

定义于
lib/rspec/core/formatters.rb,
lib/rspec/core/formatters/helpers.rb,
lib/rspec/core/formatters/protocol.rb,
lib/rspec/core/formatters/html_printer.rb,
lib/rspec/core/formatters/console_codes.rb,
lib/rspec/core/formatters/base_formatter.rb,
lib/rspec/core/formatters/html_formatter.rb,
lib/rspec/core/formatters/json_formatter.rb,
lib/rspec/core/formatters/profile_formatter.rb,
lib/rspec/core/formatters/snippet_extractor.rb,
lib/rspec/core/formatters/progress_formatter.rb,
lib/rspec/core/formatters/syntax_highlighter.rb,
lib/rspec/core/formatters/base_text_formatter.rb,
lib/rspec/core/formatters/exception_presenter.rb,
lib/rspec/core/formatters/bisect_drb_formatter.rb,
lib/rspec/core/formatters/base_bisect_formatter.rb,
lib/rspec/core/formatters/deprecation_formatter.rb,
lib/rspec/core/formatters/failure_list_formatter.rb,
lib/rspec/core/formatters/html_snippet_extractor.rb,
lib/rspec/core/formatters/documentation_formatter.rb,
lib/rspec/core/formatters/bisect_progress_formatter.rb,
lib/rspec/core/formatters/fallback_message_formatter.rb

概述

内置格式化程序

  • progress (默认) - 为通过的示例打印点,F 为失败,* 为待处理。
  • documentation - 打印传递给 describeit 方法(及其别名)的文档字符串。
  • html
  • json - 用于将数据存档以供后续分析。

progress 格式化程序是默认的,但您可以通过使用 --format(或简写为 -f)命令行选项传递来选择一个或多个其他格式化程序,例如

rspec --format documentation

您还可以将多个格式化程序的输出发送到不同的流,例如

rspec --format documentation --format html --out results.html

此示例将 documentation 格式化程序的输出发送到 $stdout,并将 html 格式化程序的输出发送到 results.html。

自定义格式化程序

您可以通过将自定义格式化程序的路径和名称传递给 rspec 命令,告诉 RSpec 使用它。例如,如果您在 path/to/my_custom_formatter.rb 中定义 MyCustomFormatter,您将键入以下命令

rspec --require path/to/my_custom_formatter.rb --format MyCustomFormatter

报告器使用以下协议调用每个格式化程序

  • 开始
    • start(StartNotification)
  • 每个示例组一次
    • example_group_started(GroupNotification)
  • 每个示例一次
    • example_started(ExampleNotification)
  • 每个示例根据结果使用其中一个
    • example_passed(ExampleNotification)
    • example_failed(FailedExampleNotification)
    • example_pending(ExampleNotification)
  • 可选地在任何时候
    • message(MessageNotification)
  • 在套件结束时
    • stop(ExamplesNotification)
    • start_dump(NullNotification)
    • dump_pending(ExamplesNotification)
    • dump_failures(ExamplesNotification)
    • dump_summary(SummaryNotification)
    • seed(SeedNotification)
    • close(NullNotification)

只有您为您的格式化程序订阅的通知才会在您的格式化程序上被调用。要订阅您的格式化程序,请使用:RSpec::Core::Formatters#register 例如

RSpec::Core::Formatters.register FormatterClassName, :example_passed, :example_failed

我们建议您自己实现这些方法;为简单起见,我们通过通知对象提供默认格式化程序输出,但如果您愿意,您可以子类化 RSpec::Core::Formatters::BaseTextFormatter 并覆盖您希望增强的方法。

定义于命名空间下

模块: ConsoleCodes, Helpers 类: BaseFormatter, BaseTextFormatter, FallbackMessageFormatter, HtmlSnippetExtractor, Loader, ProfileFormatter, Protocol

类方法摘要 折叠

类方法详情

.register(formatter_class, *notifications) ⇒void

注册格式化程序类

参数

  • formatter_class (Class)

    要注册的格式化程序类

  • notifications (Array<Symbol>)

    一个或多个要注册到指定格式化程序的通知

另见

86
87
88
# File 'lib/rspec/core/formatters.rb', line 86
def self.register(formatter_class, *notifications)
  Loader.formatters[formatter_class] = notifications
end