File: custom_formatter.feature

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (37 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Feature: Custom formatters

  RSpec ships with general purpose output formatters. You can tell RSpec which
  one to use using the [`--format` command line option](../command-line/format-option).

  When RSpec's built-in output formatters don't, however, give you everything
  you need, you can write your own custom formatter and tell RSpec to use that
  one instead. The simplest way is to subclass RSpec's `BaseTextFormatter`, and
  then override just the methods that you want to modify.

  Scenario: Custom formatter
    Given a file named "custom_formatter.rb" with:
      """ruby
      class CustomFormatter
        # This registers the notifications this formatter supports, and tells
        # us that this was written against the RSpec 3.x formatter API.
        RSpec::Core::Formatters.register self, :example_started

        def initialize(output)
          @output = output
        end

        def example_started(notification)
          @output << "example: " << notification.example.description
        end
      end
      """
    And a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "my group" do
        specify "my example" do
        end
      end
      """
    When I run `rspec example_spec.rb --require ./custom_formatter.rb --format CustomFormatter`
    Then the output should contain "example: my example"
    And  the exit status should be 0