File: included_spec.rb

package info (click to toggle)
ruby-equalizer 0.0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: ruby: 356; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,618 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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