Module: Cuprum::Collections::RSpec::Contracts::RelationContracts::ShouldDisambiguateParameter

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/relation_contracts.rb

Overview

Contract asserting that the Relation resolves the given parameter.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, key, as: , value: ) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • key (Symbol)

    the original parameter key.

  • as (Symbol, Array<Symbol>) (defaults to: )

    the aliased key or keys.

  • value (Object) (defaults to: )

    the custom value for the property.



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cuprum/collections/rspec/contracts/relation_contracts.rb', line 20

contract do |key, as:, value: 'custom value'|
  describe '.new' do
    Array(as).each do |alt|
      describe "with #{key}: value and #{alt}: value" do
        let(:error_message) do
          "ambiguous parameter #{key}: initialized with parameters " \
            "#{key}: #{value.inspect}, #{alt}: #{value.inspect}"
        end

        it 'should raise an exception' do
          expect do
            described_class.new(
              name: 'books',
              key => value,
              alt => value
            )
          end
            .to raise_error ArgumentError, error_message
        end
      end
    end
  end

  Array(as).each do |alt|
    describe "##{alt}" do
      let(:core_tools) do
        SleepingKingStudios::Tools::Toolbelt.instance.core_tools
      end

      before(:example) { allow(core_tools).to receive(:deprecate) }

      include_examples 'should define reader',
        alt,
        -> { subject.send(key) }

      it 'should print a deprecation warning' do
        subject.send(alt)

        expect(core_tools)
          .to have_received(:deprecate)
          .with(
            "##{alt} method",
            message: "Use ##{key} instead"
          )
      end

      context "when initialized with #{key}: value" do
        let(:constructor_options) do
          super().merge(key => value)
        end

        it { expect(subject.send(alt)).to be == value }
      end

      Array(as).each do |given|
        context "when initialized with #{given}: value" do
          let(:constructor_options) do
            super()
              .tap { |hsh| hsh.delete(key) }
              .merge(given => value)
          end

          it { expect(subject.send(alt)).to be == value }
        end
      end
    end
  end

  describe "##{key}" do
    Array(as).each do |alt|
      context "when initialized with #{alt}: value" do
        let(:constructor_options) do
          super()
            .tap { |hsh| hsh.delete(key) }
            .merge(alt => value)
        end
        let(:core_tools) do
          SleepingKingStudios::Tools::Toolbelt.instance.core_tools
        end

        before(:example) { allow(core_tools).to receive(:deprecate) }

        it { expect(subject.send(key)).to be == value }
      end
    end
  end

  describe '#options' do
    Array(as).each do |alt|
      context "when initialized with #{alt}: value" do
        let(:constructor_options) do
          super()
            .tap { |hsh| hsh.delete(key) }
            .merge(alt => value)
        end
        let(:core_tools) do
          SleepingKingStudios::Tools::Toolbelt.instance.core_tools
        end

        before(:example) { allow(core_tools).to receive(:deprecate) }

        it { expect(subject.options).not_to have_key alt }
      end
    end
  end
end