Module: Moarspec::Its::BlockWith

Defined in:
lib/moarspec/its/block_with.rb

Instance Method Summary collapse

Instance Method Details

#its_block_with(**lets, &block) ⇒ Object

Creates a nested context + example with ‘let` values defined from the arguments, and the subject treated as a block.

Examples:


subject { x + y }

its_block_with(x: 1, y: nil) { is_expected.to raise_error }

# is equivalent to

context "with x=1, y=2" do
  let(:x) { 1 }
  let(:y) { nil }

  it { expect { subject }.to raise_error }
end


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/moarspec/its/block_with.rb', line 24

def its_block_with(**lets, &block)
  context "with #{lets.map { "#{_1}=#{_2.inspect}" }.join(', ')} as block" do
    lets.each do |name, val|
      let(name) { val }
    end

    def is_expected # rubocop:disable Lint/NestedMethodDefinition
      expect { subject }
    end

    example(nil, &block)
  end
end