Sha256: 193fa6d0ef0f032eef4a783b6fddf45f1173e0005f5926380d9be7660e017c45

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

RSpec::Matchers.define :render_local do |*args|
  match do |actual|
    allow(controller).to receive(:render).and_call_original

    set_expected_key_and_value(*args)

    if @intermediate_klass
      allow(@intermediate_klass).to receive(:call) { @value }
    end

    actual.call

    verify_local!
  end

  def using(klass)
    @intermediate_klass = klass
    self
  end

  supports_block_expectations

  def set_expected_key_and_value(*args)
    locals_hash = args.extract_options!

    @key = nil
    @value = nil

    if locals_hash.present?
      @key = locals_hash.keys.first.to_sym
      @value = locals_hash.values.first
    elsif args.size == 2
      @key = args.first.to_sym
      subject = args.pop
      if subject.is_a?(Class)
        @value = kind_of(subject)
      else
        @value = subject
      end
    elsif args.size == 1
      subject = args.pop
      if subject.is_a?(Class)
        @key = subject.name.underscore.to_sym
        @value = kind_of(subject)
      end
    end
  end

  def verify_local!
    if @intermediate_klass
      expect(@intermediate_klass).to have_received(:call)
    end
    expect(controller).to have_received(:render).
      at_least(:once).
      with(anything, hash_including(locals: hash_including(@key => @value)))
  end
end


Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rspec-controller-matchers-0.0.5 lib/rspec/matchers/render_local_matcher.rb
rspec-controller-matchers-0.0.4 lib/rspec/matchers/render_local_matcher.rb
rspec-controller-matchers-0.0.3 lib/rspec/matchers/render_local_matcher.rb
rspec-controller-matchers-0.0.2 lib/rspec/matchers/render_local_matcher.rb
rspec-controller-matchers-0.0.1 lib/rspec/matchers/render_local_matcher.rb