Skip to content

Commit aa634fc

Browse files
committed
Implement #respond_to_missing?
1 parent 72aa572 commit aa634fc

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Unreleased
22

33
* Avoid modifying frozen strings ([yuri-zubov][yuri-zubov])
4+
* Define `#respond_to_missing?` on `Capybara::Node::Email`
5+
([tylerhunt][tylerhunt])
46

57
## 3.0.0
68

@@ -17,4 +19,5 @@
1719
* Corrects `inspect` of `Capybara::Node::Email`
1820
* Delegate all missing methods in `Capybara::Node::Email` to `base.email`
1921

22+
[tylerhunt]: https://github.com/tylerhunt
2023
[yuri-zubov]: https://github.com/yuri-zubov

lib/capybara/email/driver.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,22 @@ def raw
6868

6969
private
7070

71-
def method_missing(meth, *args, &block)
72-
if email.respond_to?(meth)
71+
def method_missing(method_name, *args, &block)
72+
if email.respond_to?(method_name)
7373
if args.empty?
74-
email.send(meth)
74+
email.send(method_name)
7575
else
76-
email.send(meth, args)
76+
email.send(method_name, args)
7777
end
7878
else
7979
super
8080
end
8181
end
8282

83+
def respond_to_missing?(method_name, include_private = false)
84+
email.respond_to?(method_name, include_private || super)
85+
end
86+
8387
def convert_to_html(text)
8488
"<html><body>#{convert_links(text)}</body></html>"
8589
end

lib/capybara/node/email.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ def save_and_open(file_name = nil)
6262

6363
private
6464

65-
# Tries to send to `base` first.
66-
# If an NotImplementedError is hit because some finders/matchers/etc. aren't implemented,
67-
# fall back to treating the message body as a Capybara::Node::Simple
68-
def method_missing(meth, *args, &block)
69-
begin
70-
base.send(meth, *args)
71-
rescue NotImplementedError
72-
body_as_simple_node.send(meth, *args)
73-
end
65+
# Tries to send to `base` first. If an NotImplementedError is hit because
66+
# some finders/matchers/etc. aren't implemented, fall back to treating the
67+
# message body as a Capybara::Node::Simple
68+
def method_missing(method_name, *args, &block)
69+
base.send(method_name, *args)
70+
rescue NotImplementedError
71+
body_as_simple_node.send(method_name, *args)
72+
end
73+
74+
def respond_to_missing?(method_name, include_private = false)
75+
base.respond_to?(method_name, include_private) || super
7476
end
7577
end

spec/node/email_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
it 'delegates to the base' do
3838
expect(email.subject).to eq 'Test subject'
3939
end
40+
41+
it 'responds to :subject' do
42+
expect(email).to respond_to(:subject)
43+
end
4044
end
4145

4246
describe '#to' do
@@ -47,6 +51,10 @@
4751
it 'delegates to the base' do
4852
expect(email.to).to include '[email protected]'
4953
end
54+
55+
it 'responds to :to' do
56+
expect(email).to respond_to(:to)
57+
end
5058
end
5159

5260
describe '#reply_to' do
@@ -57,6 +65,10 @@
5765
it 'delegates to the base' do
5866
expect(email.reply_to).to include '[email protected]'
5967
end
68+
69+
it 'responds to :reply_to' do
70+
expect(email).to respond_to(:reply_to)
71+
end
6072
end
6173

6274
describe '#from' do
@@ -67,6 +79,10 @@
6779
it 'delegates to the base' do
6880
expect(email.from).to include '[email protected]'
6981
end
82+
83+
it 'responds to :from' do
84+
expect(email).to respond_to(:from)
85+
end
7086
end
7187

7288
describe '#header' do

0 commit comments

Comments
 (0)