Module: Saharspec::Its::Block

Defined in:
lib/saharspec/its/block.rb

Instance Method Summary collapse

Instance Method Details

#its_block(*options, &block) ⇒ Object

Creates nested example which converts current subject to a block-subject.

Examples:


subject { calc_something(params) }

# without its_block
context 'with this params' do
  it { expect { subject }.to change(some, :value).by(1) }
end

context 'with that params' do
  it { expect { subject }.to raise_error(SomeError) }
end

# with its_block
context 'with this params' do
  its_block { is_expected.to change(some, :value).by(1) }
end

context 'with that params' do
  its_block { is_expected.to raise_error(SomeError) }
end

Parameters:

  • options

    Options (metadata) that can be passed to usual RSpec example.

  • block (Proc)

    The test itself. Inside it, is_expected is a synonom for expect { subject }.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/saharspec/its/block.rb', line 34

def its_block(*options, &block)
  # rubocop:disable Lint/NestedMethodDefinition
  describe('as block') do
    let(:__call_subject) do
      -> { subject }
    end

    def is_expected
      expect(__call_subject)
    end

    example(nil, *options, &block)
  end
  # rubocop:enable Lint/NestedMethodDefinition
end