File: one_liner_syntax.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 (73 lines) | stat: -rw-r--r-- 2,668 bytes parent folder | download | duplicates (3)
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
38
39
40
41
42
43
44
45
46
47
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
@oneliner-should
Feature: One-liner syntax

  RSpec supports a one-liner syntax for setting an expectation on the
  `subject`.  RSpec will give the examples a doc string that is auto-
  generated from the matcher used in the example. This is designed specifically
  to help avoid duplication in situations where the doc string and the matcher
  used in the example mirror each other exactly. When used excessively, it can
  produce documentation output that does not read well or contribute to
  understanding the object you are describing.

  This comes in two flavors:

    * `is_expected` is defined simply as `expect(subject)` and is designed for
      when you are using rspec-expectations with its newer expect-based syntax.
    * `should` was designed back when rspec-expectations only had a should-based
      syntax. However, it continues to be available and work even if the
      `:should` syntax is disabled (since that merely removes `Object#should`
      but this is `RSpec::Core::ExampleGroup#should`).

  Notes:

    * This feature is only available when using rspec-expectations.
    * Examples defined using this one-liner syntax cannot be directly selected from the command line using the [`--example` option](../command-line/example-option).
    * The one-liner syntax only works with non-block expectations (e.g. `expect(obj).to eq`, etc) and it cannot be used with block expectations (e.g. `expect { object }`).

  Scenario: Implicit subject
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe Array do
        describe "when first created" do
          # Rather than:
          # it "should be empty" do
          #   subject.should be_empty
          # end

          it { should be_empty }
          # or
          it { is_expected.to be_empty }
        end
      end
      """
    When I run `rspec example_spec.rb --format doc`
    Then the examples should all pass
     And the output should contain:
       """
       Array
         when first created
           is expected to be empty
           is expected to be empty
       """

  Scenario: Explicit subject
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe Array do
        describe "with 3 items" do
          subject { [1,2,3] }
          it { should_not be_empty }
          # or
          it { is_expected.not_to be_empty }
        end
      end
      """
    When I run `rspec example_spec.rb --format doc`
    Then the examples should all pass
     And the output should contain:
       """
       Array
         with 3 items
           is expected not to be empty
           is expected not to be empty
       """