Skip to content

Commit

Permalink
Specify desired state in unit spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0d1r2 committed Oct 3, 2024
1 parent e8f81e3 commit 43d14fb
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def country
end
end
end

context 'qualified with #with_private' do
it 'states that it should delegate method to the right object with right argument and makes is private' do
matcher = delegate_method(:method_name).to(:delegate).with_private
message = 'delegate #method_name to the #delegate object privately'

expect(matcher.description).to eq message
end
end
end

context 'when the subject is a class' do
Expand Down Expand Up @@ -655,4 +664,116 @@ def hello
end
end
end

context 'qualified with #with_private' do
context 'when using delegate from Rails' do
context 'when delegations were defined with :private' do
it 'accepts' do
define_class('Person') do
delegate :hello, to: :country, private: true
def country
end
end

person = Person.new

expect(person).to delegate_method(:hello).to(:country).with_private
end
end

context 'when delegations were not defined with :private' do
it 'rejects with the correct failure message' do
define_class('Person') do
delegate :hello, to: :country
def country
end
end

person = Person.new

message = <<-MESSAGE
Expected Person to delegate #hello to the #country object privately.
Person#hello did delegate to #country, but 'private: true' is missing.
MESSAGE

expectation = lambda do
expect(person).to delegate_method(:hello).to(:country).with_private
end

expect(&expectation).to fail_with_message(message)
end

context 'with :prefix' do
it 'accepts' do
define_class('Person') do
delegate :hello, to: :country, private: true, prefix: :user
def country
end
end

person = Person.new

expect(person).to delegate_method(:hello).to(:country).with_prefix(:user).with_private
end
end

context 'with :as' do
it 'accepts' do
define_class('Company') do
def name
'Acme Company'
end
end

define_class('Person') do
private

def company_name
company.name
end

def company
Company.new
end
end

person = Person.new
matcher = delegate_method(:company_name).to(:company).as(:name).with_private
matcher.matches?(person)

expect(person.send(:company).name).to eq 'Acme Company'
end

context 'and :prefix' do
it 'accepts' do
define_class('Company') do
def name
'Acme Company'
end
end

define_class('Person') do
private

def company_name
company.name
end

def company
Company.new
end
end

person = Person.new
matcher = delegate_method(:company_name).to(:company).with_prefix(:user).as(:name).with_private
matcher.matches?(person)

expect(person.send(:company).name).to eq 'Acme Company'
end
end
end
end
end
end
end

0 comments on commit 43d14fb

Please sign in to comment.