/usr/share/doc/ruby-rspec-expectations/features/built_in_matchers/respond_to.feature is in ruby-rspec-expectations 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 | Feature: `respond_to` matcher
Use the `respond_to` matcher to specify details of an object's interface. In its most basic form:
```ruby
expect(obj).to respond_to(:foo) # pass if obj.respond_to?(:foo)
```
You can specify that an object responds to multiple messages in a single statement with
multiple arguments passed to the matcher:
```ruby
expect(obj).to respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
```
If the number of arguments accepted by the method is important to you, you can specify
that as well:
```ruby
expect(obj).to respond_to(:foo).with(1).argument
expect(obj).to respond_to(:bar).with(2).arguments
```
Note that this matcher relies entirely upon `#respond_to?`. If an object dynamically responds
to a message via `#method_missing`, but does not indicate this via `#respond_to?`, then this
matcher will give you false results.
Scenario: basic usage
Given a file named "respond_to_matcher_spec.rb" with:
"""ruby
RSpec.describe "a string" do
it { is_expected.to respond_to(:length) }
it { is_expected.to respond_to(:hash, :class, :to_s) }
it { is_expected.not_to respond_to(:to_model) }
it { is_expected.not_to respond_to(:compact, :flatten) }
# deliberate failures
it { is_expected.to respond_to(:to_model) }
it { is_expected.to respond_to(:compact, :flatten) }
it { is_expected.not_to respond_to(:length) }
it { is_expected.not_to respond_to(:hash, :class, :to_s) }
# mixed examples--String responds to :length but not :flatten
# both specs should fail
it { is_expected.to respond_to(:length, :flatten) }
it { is_expected.not_to respond_to(:length, :flatten) }
end
"""
When I run `rspec respond_to_matcher_spec.rb`
Then the output should contain all of these:
| 10 examples, 6 failures |
| expected "a string" to respond to :to_model |
| expected "a string" to respond to :compact, :flatten |
| expected "a string" not to respond to :length |
| expected "a string" not to respond to :hash, :class, :to_s |
| expected "a string" to respond to :flatten |
| expected "a string" not to respond to :length |
Scenario: specify arguments
Given a file named "respond_to_matcher_argument_checking_spec.rb" with:
"""ruby
RSpec.describe 7 do
it { is_expected.to respond_to(:zero?).with(0).arguments }
it { is_expected.not_to respond_to(:zero?).with(1).argument }
it { is_expected.to respond_to(:between?).with(2).arguments }
it { is_expected.not_to respond_to(:between?).with(7).arguments }
# deliberate failures
it { is_expected.to respond_to(:zero?).with(1).argument }
it { is_expected.not_to respond_to(:zero?).with(0).arguments }
it { is_expected.to respond_to(:between?).with(7).arguments }
it { is_expected.not_to respond_to(:between?).with(2).arguments }
end
"""
When I run `rspec respond_to_matcher_argument_checking_spec.rb`
Then the output should contain all of these:
| 8 examples, 4 failures |
| expected 7 to respond to :zero? with 1 argument |
| expected 7 not to respond to :zero? with 0 arguments |
| expected 7 to respond to :between? with 7 arguments |
| expected 7 not to respond to :between? with 2 arguments |
|