Sha256: ff7e68225c6b9e9f5f46f0a66bf59f690d1465f8bb9fb79c7892f6b8e6e0f648

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'spec_helper'
require 'compo'

# Mock implementation of a ParentTracker
class MockParentTracker
  include Compo::Mixins::ParentTracker

  # Initialises a MockParentTracker
  #
  # @api  private
  def initialize(parent, id_function)
    update_parent(parent, id_function)
  end
end

RSpec.describe MockParentTracker do
  subject { MockParentTracker.new(parent, id_function) }
  let(:parent) { double(:parent) }
  let(:id_function) { double(:id_function) }

  describe '#parent' do
    it 'returns the current parent' do
      expect(subject.parent).to eq(parent)
    end
  end

  describe '#id' do
    it 'calls the current ID function and returns its result' do
      allow(id_function).to receive(:call).and_return(:id)
      expect(id_function).to receive(:call).once.with(no_args)

      expect(subject.id).to eq(:id)
    end
  end

  describe '#update_parent' do
    let(:new_parent) { double(:new_parent) }
    let(:new_id_function) { double(:new_id_function) }

    it 'sets the parent to the new value' do
      subject.update_parent(new_parent, new_id_function)
      expect(subject.parent).to eq(new_parent)
    end

    it 'sets the ID function to the new ID function' do
      allow(new_id_function).to receive(:call).and_return(:new_id)
      expect(new_id_function).to receive(:call).once

      subject.update_parent(new_parent, new_id_function)
      expect(subject.id).to eq(:new_id)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
compo-0.5.1 spec/parent_tracker_spec.rb
compo-0.5.0 spec/parent_tracker_spec.rb