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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
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
expect(obj).to respond_to(:baz).with(1..2).arguments
expect(obj).to respond_to(:xyz).with_unlimited_arguments
```
If your Ruby version supports keyword arguments, you can specify a list of keywords accepted
by the method.
```ruby
expect(obj).to respond_to(:foo).with_keywords(:ichi, :ni)
expect(obj).to respond_to(:bar).with(2).arguments.and_keywords(:san, :yon)
expect(obj).to respond_to(:baz).with_arbitrary_keywords
```
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 |
@skip-when-splat-args-unsupported
Scenario: Specify arguments range
Given a file named "respond_to_matcher_argument_range_checking_spec.rb" with:
"""ruby
class MyClass
def build(name, options = {})
end
def inspect
'my_object'
end
end
RSpec.describe MyClass do
it { is_expected.to respond_to(:build).with(1..2).arguments }
it { is_expected.not_to respond_to(:build).with(0..1).arguments }
it { is_expected.not_to respond_to(:build).with(2..3).arguments }
it { is_expected.not_to respond_to(:build).with(0..3).arguments }
# deliberate failures
it { is_expected.not_to respond_to(:build).with(1..2).arguments }
it { is_expected.to respond_to(:build).with(0..1).arguments }
it { is_expected.to respond_to(:build).with(2..3).arguments }
it { is_expected.to respond_to(:build).with(0..3).arguments }
end
"""
When I run `rspec respond_to_matcher_argument_range_checking_spec.rb`
Then the output should contain all of these:
| 8 examples, 4 failures |
| expected my_object not to respond to :build with 1..2 arguments |
| expected my_object to respond to :build with 0..1 arguments |
| expected my_object to respond to :build with 2..3 arguments |
| expected my_object to respond to :build with 0..3 arguments |
@skip-when-splat-args-unsupported
Scenario: Specify unlimited arguments
Given a file named "respond_to_matcher_unlimited_argument_checking_spec.rb" with:
"""ruby
class MyClass
def greet(message = 'Hello', *people)
end
def hail(person)
end
def inspect
'my_object'
end
end
RSpec.describe MyClass do
it { is_expected.to respond_to(:greet).with_unlimited_arguments }
it { is_expected.to respond_to(:greet).with(1).argument.and_unlimited_arguments }
it { is_expected.not_to respond_to(:hail).with_unlimited_arguments }
it { is_expected.not_to respond_to(:hail).with(1).argument.and_unlimited_arguments }
# deliberate failures
it { is_expected.not_to respond_to(:greet).with_unlimited_arguments }
it { is_expected.not_to respond_to(:greet).with(1).argument.and_unlimited_arguments }
it { is_expected.to respond_to(:hail).with_unlimited_arguments }
it { is_expected.to respond_to(:hail).with(1).argument.and_unlimited_arguments }
end
"""
When I run `rspec respond_to_matcher_unlimited_argument_checking_spec.rb`
Then the output should contain all of these:
| 8 examples, 4 failures |
| expected my_object not to respond to :greet with unlimited arguments |
| expected my_object not to respond to :greet with 1 argument and unlimited arguments |
| expected my_object to respond to :hail with unlimited arguments |
| expected my_object to respond to :hail with 1 argument and unlimited arguments |
@skip-when-keyword-args-unsupported
Scenario: Specify keywords
Given a file named "respond_to_matcher_keyword_checking_spec.rb" with:
"""ruby
class MyClass
def find(name = 'id', limit: 1_000, offset: 0)
[]
end
def inspect
'my_object'
end
end
RSpec.describe MyClass do
it { is_expected.to respond_to(:find).with_keywords(:limit, :offset) }
it { is_expected.to respond_to(:find).with(1).argument.and_keywords(:limit, :offset) }
it { is_expected.not_to respond_to(:find).with_keywords(:limit, :offset, :page) }
it { is_expected.not_to respond_to(:find).with(1).argument.and_keywords(:limit, :offset, :page) }
# deliberate failures
it { is_expected.to respond_to(:find).with_keywords(:limit, :offset, :page) }
it { is_expected.to respond_to(:find).with(1).argument.and_keywords(:limit, :offset, :page) }
it { is_expected.not_to respond_to(:find).with_keywords(:limit, :offset) }
it { is_expected.not_to respond_to(:find).with(1).argument.and_keywords(:limit, :offset) }
end
"""
When I run `rspec respond_to_matcher_keyword_checking_spec.rb`
Then the output should contain all of these:
| 8 examples, 4 failures |
| expected my_object to respond to :find with keywords :limit, :offset, and :page |
| expected my_object to respond to :find with 1 argument and keywords :limit, :offset, and :page |
| expected my_object not to respond to :find with keywords :limit and :offset |
| expected my_object not to respond to :find with 1 argument and keywords :limit and :offset |
@skip-when-keyword-args-unsupported
Scenario: Specify any keywords
Given a file named "respond_to_matcher_any_keywords_checking_spec.rb" with:
"""ruby
class MyClass
def build(name: 'object', **opts)
end
def create(name: 'object', type: String)
end
def inspect
'my_object'
end
end
RSpec.describe MyClass do
it { is_expected.to respond_to(:build).with_any_keywords }
it { is_expected.to respond_to(:build).with_keywords(:name).and_any_keywords }
it { is_expected.not_to respond_to(:create).with_any_keywords }
it { is_expected.not_to respond_to(:create).with_keywords(:name).and_any_keywords }
# deliberate failures
it { is_expected.not_to respond_to(:build).with_any_keywords }
it { is_expected.not_to respond_to(:build).with_keywords(:name).and_any_keywords }
it { is_expected.to respond_to(:create).with_any_keywords }
it { is_expected.to respond_to(:create).with_keywords(:name).and_any_keywords }
end
"""
When I run `rspec respond_to_matcher_any_keywords_checking_spec.rb`
Then the output should contain all of these:
| 8 examples, 4 failures |
| expected my_object not to respond to :build with any keywords |
| expected my_object not to respond to :build with keyword :name and any keywords |
| expected my_object to respond to :create with any keywords |
| expected my_object to respond to :create with keyword :name and any keywords |
@skip-when-required-keyword-args-unsupported
Scenario: Specify required keywords
Given a file named "respond_to_matcher_required_keyword_checking_spec.rb" with:
"""ruby
class MyClass
def plant(seed:, fertilizer: nil, water: 'daily')
[]
end
def inspect
'my_object'
end
end
RSpec.describe MyClass do
it { is_expected.to respond_to(:plant).with_keywords(:seed) }
it { is_expected.to respond_to(:plant).with_keywords(:seed, :fertilizer, :water) }
it { is_expected.not_to respond_to(:plant).with_keywords(:fertilizer, :water) }
# deliberate failures
it { is_expected.not_to respond_to(:plant).with_keywords(:seed) }
it { is_expected.not_to respond_to(:plant).with_keywords(:seed, :fertilizer, :water) }
it { is_expected.to respond_to(:plant).with_keywords(:fertilizer, :water) }
end
"""
When I run `rspec respond_to_matcher_required_keyword_checking_spec.rb`
Then the output should contain all of these:
| 6 examples, 3 failures |
| expected my_object not to respond to :plant with keyword :seed |
| expected my_object not to respond to :plant with keywords :seed, :fertilizer, and :water |
| expected my_object to respond to :plant with keywords :fertilizer and :water |
|