1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Add with_prefix to delegate_method

Secondary author: Elliot Winkler <elliot.winkler@gmail.com>
This commit is contained in:
Aditya Kapoor 2014-12-04 16:37:34 +05:30 committed by Elliot Winkler
parent f82d4eeedd
commit 18201d4f95
3 changed files with 278 additions and 2 deletions

View file

@ -64,6 +64,9 @@
* Change `set_session` so that its #to qualifier supports regexps, similar to
`set_flash`.
* Add `with_prefix` qualifier to `delegate_method` to correspond to the `prefix`
option for Rails's `delegate` macro. [#622]
### Improvements
* Tweak `allow_value` failure message so that it reads a bit nicer when listing
@ -79,6 +82,7 @@
[#598]: https://github.com/thoughtbot/shoulda-matchers/pull/598
[#602]: https://github.com/thoughtbot/shoulda-matchers/pull/602
[#543]: https://github.com/thoughtbot/shoulda-matchers/pull/543
[#622]: https://github.com/thoughtbot/shoulda-matchers/pull/622
# 2.7.0

View file

@ -31,9 +31,24 @@ module Shoulda
# should delegate_method(:deliver).to(:post_office)
# end
#
# You can also use `delegate_method` with Rails's `delegate` macro:
#
# class Courier
# attr_reader :post_office
# delegate :deliver, to: :post_office
#
# def initialize
# @post_office = PostOffice.new
# end
# end
#
# describe Courier do
# it { should delegate_method(:deliver).to(:post_office) }
# end
#
# To employ some terminology, we would say that Courier's #deliver method
# is the delegating method, PostOffice is the delegate object, and
# PostOffice#deliver is the delegate method.
# is the *delegating method*, PostOffice is the *delegate object*, and
# PostOffice#deliver is the *delegate method*.
#
# #### Qualifiers
#
@ -67,6 +82,31 @@ module Shoulda
# should delegate_method(:deliver).to(:post_office).as(:ship)
# end
#
# ##### with_prefix
#
# Use `with_prefix` when using Rails's `delegate` helper along with the
# `:prefix` option.
#
# class Page < ActiveRecord::Base
# belongs_to :site
# delegate :name, to: :site, prefix: true
# delegate :title, to: :site, prefix: :root
# end
#
# # RSpec
# describe Page do
# it { should delegate_method(:name).to(:site).with_prefix }
# it { should delegate_method(:name).to(:site).with_prefix(true) }
# it { should delegate_method(:title).to(:site).with_prefix(:root) }
# end
#
# # Test::Unit
# class PageTest < Test::Unit::TestCase
# should delegate_method(:name).to(:site).with_prefix
# should delegate_method(:name).to(:site).with_prefix(true)
# should delegate_method(:title).to(:site).with_prefix(:root)
# end
#
# ##### with_arguments
#
# Use `with_arguments` to assert that the delegate method is called with
@ -170,6 +210,20 @@ module Shoulda
self
end
def with_prefix(prefix = nil)
@delegating_method =
:"#{build_delegating_method_prefix(prefix)}_#{delegate_method}"
delegate_method
self
end
def build_delegating_method_prefix(prefix)
case prefix
when true, nil then delegate_object_reader_method
else prefix
end
end
def failure_message
"Expected #{class_under_test} to #{description}\n" +
"Method calls sent to " +

View file

@ -32,6 +32,76 @@ describe Shoulda::Matchers::Independent::DelegateMethodMatcher do
expect(matcher.description).to eq message
end
end
context 'qualified with #with_prefix' do
context 'without arguments' do
before do
define_model('Country') do
def hello; 'hello' end
end
end
context "when the subject's delegating method also has a prefix" do
it 'produces the correct description' do
define_class('Person') do
delegate :hello, to: :country, prefix: true
def country
Country.new
end
end
matcher = delegate_method(:hello).to(:country).with_prefix
expect(matcher.description).
to eq('delegate #country_hello to #country object as #hello')
end
end
end
context 'as true' do
before do
define_model('Country') do
def hello; 'hello' end
end
end
context "when the subject's delegating method also has a prefix" do
it 'produces the correct description' do
define_class('Person') do
delegate :hello, to: :country, prefix: true
def country
Country.new
end
end
matcher = delegate_method(:hello).to(:country).with_prefix(true)
expect(matcher.description).
to eq('delegate #country_hello to #country object as #hello')
end
end
end
context 'as a symbol/string' do
it 'should delegate as (prefix_supplied)_(method_on_target)' do
define_model('Country') do
def hello; 'hello' end
end
define_class('Person') do
delegate :hello, to: :country, prefix: 'county'
def country
Country.new
end
end
matcher = delegate_method(:hello).to(:country).with_prefix('county')
expect(matcher.description).
to eq('delegate #county_hello to #country object as #hello')
end
end
end
end
context 'when the subject is a class' do
@ -296,4 +366,152 @@ describe Shoulda::Matchers::Independent::DelegateMethodMatcher do
end
end
end
context 'qualified with #with_prefix' do
context 'without arguments' do
before do
define_model('Country') do
def hello; 'hello' end
end
end
context "when the subject's delegating method also has a prefix" do
it 'accepts' do
define_class('Person') do
delegate :hello, to: :country, prefix: true
def country
Country.new
end
end
person = Person.new
expect(person).to delegate_method(:hello). to(:country).with_prefix
end
end
context "when the subject's delegating method does not have a prefix" do
it 'rejects with the correct failure message' do
define_class('Person') do
delegate :hello, to: :country
def country
Country.new
end
end
message = [
'Expected Person to delegate #country_hello to #country object as #hello',
'Method calls sent to Person#country: (none)'
].join("\n")
person = Person.new
expect {
expect(person).to delegate_method(:hello). to(:country).with_prefix
}.to fail_with_message(message)
end
end
end
context 'as true' do
before do
define_model('Country') do
def hello; 'hello' end
end
end
context "when the subject's delegating method also has a prefix" do
it 'accepts' do
define_class('Person') do
delegate :hello, to: :country, prefix: true
def country
Country.new
end
end
person = Person.new
expect(person).
to delegate_method(:hello).
to(:country).with_prefix(true)
end
end
context "when the subject's delegating method does not have a prefix" do
it 'rejects with the correct failure message' do
define_class('Person') do
delegate :hello, to: :country
def country
Country.new
end
end
message = [
'Expected Person to delegate #country_hello to #country object as #hello',
'Method calls sent to Person#country: (none)'
].join("\n")
person = Person.new
expect {
expect(person).
to delegate_method(:hello).
to(:country).with_prefix(true)
}.to fail_with_message(message)
end
end
end
context 'as a symbol/string' do
before do
define_model('Country') do
def hello; 'hello' end
end
end
context "when the subject's delegating method has the same prefix" do
it 'accepts' do
define_class('Person') do
delegate :hello, to: :country, prefix: 'county'
def country
Country.new
end
end
person = Person.new
expect(person).
to delegate_method(:hello).
to(:country).with_prefix('county')
end
end
context "when the subject's delegating method has a different prefix" do
it 'rejects with the correct failure message' do
define_class('Person') do
delegate :hello, to: :country, prefix: 'something_else'
def country
Country.new
end
end
message = [
'Expected Person to delegate #county_hello to #country object as #hello',
'Method calls sent to Person#country: (none)'
].join("\n")
person = Person.new
expect {
expect(person).
to delegate_method(:hello).
to(:country).with_prefix('county')
}.to fail_with_message(message)
end
end
end
end
end