File: let.feature

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (53 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (5)
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
Feature: let and let!

  Use `let` to define a memoized helper method. The value will be cached across
  multiple calls in the same example but not across examples.

  Note that `let` is lazy-evaluated: it is not evaluated until the first time
  the method it defines is invoked. You can use `let!` to force the method's
  invocation before each example.

  By default, `let` is threadsafe, but you can configure it not to be
  by disabling `config.threadsafe`, which makes `let` perform a bit faster.

  Scenario: Use `let` to define memoized helper method
    Given a file named "let_spec.rb" with:
      """ruby
      $count = 0
      RSpec.describe "let" do
        let(:count) { $count += 1 }

        it "memoizes the value" do
          expect(count).to eq(1)
          expect(count).to eq(1)
        end

        it "is not cached across examples" do
          expect(count).to eq(2)
        end
      end
      """
    When I run `rspec let_spec.rb`
    Then the examples should all pass

  Scenario: Use `let!` to define a memoized helper method that is called in a `before` hook
    Given a file named "let_bang_spec.rb" with:
      """ruby
      $count = 0
      RSpec.describe "let!" do
        invocation_order = []

        let!(:count) do
          invocation_order << :let!
          $count += 1
        end

        it "calls the helper method in a before hook" do
          invocation_order << :example
          expect(invocation_order).to eq([:let!, :example])
          expect(count).to eq(1)
        end
      end
      """
    When I run `rspec let_bang_spec.rb`
    Then the examples should all pass