Sha256: 0c45dd018ab690273fd2d515b26497009f66749a745a38058d2a347fbe352eeb

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe Equalizer, '#included' do
  subject { descendant.instance_exec(object) { |mod| include mod } }

  let(:object)     { described_class.new        }
  let(:descendant) { Class.new                  }
  let(:superclass) { described_class.superclass }

  before do
    # Prevent Module.included from being called through inheritance
    allow(described_class::Methods).to receive(:included)
  end

  around do |example|
    # Restore included method after each example
    superclass.class_eval do
      alias_method :original_included, :included
      example.call
      undef_method :included
      alias_method :included, :original_included
    end
  end

  it 'delegates to the superclass #included method' do
    # This is the most succinct approach I could think of to test whether the
    # superclass#included method is called. All of the built-in rspec helpers
    # did not seem to work for this.
    included = false

    superclass.class_eval do
      define_method(:included) do |_|
        # Only set the flag when an Equalizer instance is included.
        # Otherwise, other module includes (which get triggered internally
        # in RSpec when `change` is used for the first time, since it uses
        # autoloading for its matchers) will wrongly set this flag.
        included = true if self.kind_of?(Equalizer)
      end
    end

    expect { subject }.to change { included }.from(false).to(true)
  end

  it 'includes methods into the descendant' do
    subject
    expect(descendant.included_modules).to include(described_class::Methods)
  end
end

Version data entries

7 entries across 5 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/equalizer-0.0.11/spec/unit/equalizer/included_spec.rb
equalizer-0.0.11 spec/unit/equalizer/included_spec.rb