Skip to content

Commit

Permalink
add spec for DelegateAttribute module
Browse files Browse the repository at this point in the history
  • Loading branch information
hamajyotan committed Jan 14, 2024
1 parent f881740 commit ec9ec0b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]

- remove `add_development_dependency` from gemspec
- add spec for DelegateAttribute module

## [0.1.6] - 2024-01-14

Expand Down
2 changes: 1 addition & 1 deletion lib/active_record_compose/delegate_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def delegate_attribute(*attributes, to:, **options)
end

def attributes
super.merge(delegated_attributes.to_h { [_1, public_send(_1)] })
(defined?(super) ? super : {}).merge(delegated_attributes.to_h { [_1, public_send(_1)] })
end
end
end
46 changes: 46 additions & 0 deletions spec/active_record_compose/delegate_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'spec_helper'
require 'active_record_compose/delegate_attribute'

RSpec.describe ActiveRecordCompose::DelegateAttribute do
before { stub_const('Dummy', klass) }

subject { klass.new(data) }

let(:data) { Struct.new(:x, :y, :z, keyword_init: true).new }

let(:klass) do
Class.new do
include ActiveRecordCompose::DelegateAttribute

def initialize(data)
@data = data
end

delegate_attribute :x, :y, to: :data

private

attr_reader :data
end
end

specify 'reader method must is defined' do
data.x = 'foo'
expect(data.x).to eq 'foo'
expect(subject.x).to eq 'foo'
end

specify 'writer method must is defined' do
subject.y = 'bar'
expect(data.y).to eq 'bar'
expect(subject.y).to eq 'bar'
end

specify 'definition declared in delegate must be included in attributes' do
subject.x = 'foo'
subject.y = 'bar'
expect(subject.attributes).to eq({ 'x' => 'foo', 'y' => 'bar' })
end
end

0 comments on commit ec9ec0b

Please sign in to comment.