| 1 |
RSpec.describe "expect(...).to respond_to(:sym)" do |
| 2 |
it_behaves_like "an RSpec value matcher", :valid_value => "s", :invalid_value => 5 do |
| 3 |
let(:matcher) { respond_to(:upcase) } |
| 4 |
end |
| 5 |
|
| 6 |
it "passes if target responds to :sym" do |
| 7 |
expect(Object.new).to respond_to(:methods) |
| 8 |
end |
| 9 |
|
| 10 |
it "passes if target responds to :sym but does not implement method" do |
| 11 |
|
| 12 |
klass = Class.new { def respond_to?(_); true; end } |
| 13 |
expect(klass.new).to respond_to(:my_method) |
| 14 |
end |
| 15 |
|
| 16 |
it "fails if target does not respond to :sym" do |
| 17 |
expect { |
| 18 |
expect("this string").to respond_to(:some_method) |
| 19 |
}.to fail_with('expected "this string" to respond to :some_method') |
| 20 |
end |
| 21 |
end |
| 22 |
|
| 23 |
RSpec.describe "expect(...).to respond_to(:sym).with(1).argument" do |
| 24 |
it "passes if target responds to :sym with 1 arg" do |
| 25 |
obj = Object.new |
| 26 |
def obj.foo(arg); end |
| 27 |
expect(obj).to respond_to(:foo).with(1).argument |
| 28 |
end |
| 29 |
|
| 30 |
it "passes if target responds to any number of arguments" do |
| 31 |
obj = Object.new |
| 32 |
def obj.foo(*args); end |
| 33 |
expect(obj).to respond_to(:foo).with(1).argument |
| 34 |
end |
| 35 |
|
| 36 |
it "passes if target responds to one or more arguments" do |
| 37 |
obj = Object.new |
| 38 |
def obj.foo(a, *args); end |
| 39 |
expect(obj).to respond_to(:foo).with(1).argument |
| 40 |
end |
| 41 |
|
| 42 |
it "verifes the method signature of new as if it was initialize" do |
| 43 |
klass = Class.new { def initialize(a, b); end; } |
| 44 |
expect(klass).to respond_to(:new).with(2).arguments |
| 45 |
end |
| 46 |
|
| 47 |
it "fails if target does not respond to :sym" do |
| 48 |
obj = Object.new |
| 49 |
expect { |
| 50 |
expect(obj).to respond_to(:some_method).with(1).argument |
| 51 |
}.to fail_with(/expected .* to respond to :some_method/) |
| 52 |
end |
| 53 |
|
| 54 |
it "fails if :sym expects 0 args" do |
| 55 |
obj = Object.new |
| 56 |
def obj.foo; end |
| 57 |
expect { |
| 58 |
expect(obj).to respond_to(:foo).with(1).argument |
| 59 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/) |
| 60 |
end |
| 61 |
|
| 62 |
it "fails if :sym expects 2 args" do |
| 63 |
obj = Object.new |
| 64 |
def obj.foo(arg, arg2); end |
| 65 |
expect { |
| 66 |
expect(obj).to respond_to(:foo).with(1).argument |
| 67 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/) |
| 68 |
end |
| 69 |
|
| 70 |
it "fails if :sym expects 2 or more args" do |
| 71 |
obj = Object.new |
| 72 |
def obj.foo(arg, arg2, *args); end |
| 73 |
expect { |
| 74 |
expect(obj).to respond_to(:foo).with(1).argument |
| 75 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/) |
| 76 |
end |
| 77 |
|
| 78 |
it "fails if the method signature of initialize does not match" do |
| 79 |
klass = Class.new { def initialize(a, b); end; } |
| 80 |
expect { |
| 81 |
expect(klass).to respond_to(:new).with(1).arguments |
| 82 |
}.to fail_with(/expected #<Class.*> to respond to :new with 1 argument/) |
| 83 |
end |
| 84 |
|
| 85 |
it "still works if target has overridden the method method" do |
| 86 |
obj = Object.new |
| 87 |
def obj.method; end |
| 88 |
def obj.other_method(arg); end |
| 89 |
expect(obj).to respond_to(:other_method).with(1).argument |
| 90 |
end |
| 91 |
|
| 92 |
it "warns that the subject does not have the implementation required when method does not exist" do |
| 93 |
|
| 94 |
klass = Class.new { def respond_to?(_); true; end } |
| 95 |
expect { |
| 96 |
expect(klass.new).to respond_to(:my_method).with(0).arguments |
| 97 |
}.to raise_error(ArgumentError) |
| 98 |
end |
| 99 |
end |
| 100 |
|
| 101 |
RSpec.describe "expect(...).to respond_to(:new)" do |
| 102 |
context "with no tampering" do |
| 103 |
it "will validate new as if it was initialize" do |
| 104 |
klass = Class.new { def initialize(a, b, c); end } |
| 105 |
expect(klass).not_to respond_to(:new).with(2).arguments |
| 106 |
expect(klass).to respond_to(:new).with(3).arguments |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
context "on a class that has redefined `new`" do |
| 111 |
it "uses the method signature of the redefined `new` for arg verification" do |
| 112 |
klass = Class.new { def self.new(a); end } |
| 113 |
expect(klass).to respond_to(:new).with(1).argument |
| 114 |
expect { |
| 115 |
expect(klass).to respond_to(:new).with(2).arguments |
| 116 |
}.to fail_with(/expected #<Class.*> to respond to :new with 2 arguments/) |
| 117 |
expect { |
| 118 |
expect(klass).to_not respond_to(:new).with(1).argument |
| 119 |
}.to fail_with(/expected #<Class.*> not to respond to :new with 1 argument/) |
| 120 |
end |
| 121 |
end |
| 122 |
|
| 123 |
context "on a class that has undefined `new`" do |
| 124 |
it "will not respond to new" do |
| 125 |
klass = |
| 126 |
Class.new do |
| 127 |
class << self |
| 128 |
undef new |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
expect { |
| 133 |
expect(klass).to respond_to(:new) |
| 134 |
}.to fail_with(/expected .* to respond to :new/) |
| 135 |
end |
| 136 |
end |
| 137 |
|
| 138 |
context "on a class with a private `new`" do |
| 139 |
it "will not respond to new" do |
| 140 |
klass = Class.new { private_class_method :new; def initialize(a, b, c); end } |
| 141 |
expect(klass).not_to respond_to(:new) |
| 142 |
end |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
RSpec.describe "expect(...).to respond_to(message1, message2)" do |
| 147 |
it "passes if target responds to both messages" do |
| 148 |
expect(Object.new).to respond_to('methods', 'inspect') |
| 149 |
end |
| 150 |
|
| 151 |
it "fails if target does not respond to first message" do |
| 152 |
expect { |
| 153 |
expect(Object.new).to respond_to('method_one', 'inspect') |
| 154 |
}.to fail_with(/expected #<Object:.*> to respond to "method_one"/) |
| 155 |
end |
| 156 |
|
| 157 |
it "fails if target does not respond to second message" do |
| 158 |
expect { |
| 159 |
expect(Object.new).to respond_to('inspect', 'method_one') |
| 160 |
}.to fail_with(/expected #<Object:.*> to respond to "method_one"/) |
| 161 |
end |
| 162 |
|
| 163 |
it "fails if target does not respond to either message" do |
| 164 |
expect { |
| 165 |
expect(Object.new).to respond_to('method_one', 'method_two') |
| 166 |
}.to fail_with(/expected #<Object:.*> to respond to "method_one", "method_two"/) |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
RSpec.describe "expect(...).to respond_to(:sym).with(2).arguments" do |
| 171 |
it "passes if target responds to :sym with 2 args" do |
| 172 |
obj = Object.new |
| 173 |
def obj.foo(a1, a2); end |
| 174 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 175 |
end |
| 176 |
|
| 177 |
it "passes if target responds to any number of arguments" do |
| 178 |
obj = Object.new |
| 179 |
def obj.foo(*args); end |
| 180 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 181 |
end |
| 182 |
|
| 183 |
it "passes if target responds to one or more arguments" do |
| 184 |
obj = Object.new |
| 185 |
def obj.foo(a, *args); end |
| 186 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 187 |
end |
| 188 |
|
| 189 |
it "passes if target responds to two or more arguments" do |
| 190 |
obj = Object.new |
| 191 |
def obj.foo(a, b, *args); end |
| 192 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 193 |
end |
| 194 |
|
| 195 |
it "fails if target does not respond to :sym" do |
| 196 |
obj = Object.new |
| 197 |
expect { |
| 198 |
expect(obj).to respond_to(:some_method).with(2).arguments |
| 199 |
}.to fail_with(/expected .* to respond to :some_method/) |
| 200 |
end |
| 201 |
|
| 202 |
it "fails if :sym expects 0 args" do |
| 203 |
obj = Object.new |
| 204 |
def obj.foo; end |
| 205 |
expect { |
| 206 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 207 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/) |
| 208 |
end |
| 209 |
|
| 210 |
it "fails if :sym expects 1 args" do |
| 211 |
obj = Object.new |
| 212 |
def obj.foo(arg); end |
| 213 |
expect { |
| 214 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 215 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/) |
| 216 |
end |
| 217 |
|
| 218 |
it "fails if :sym expects 3 or more args" do |
| 219 |
obj = Object.new |
| 220 |
def obj.foo(arg, arg2, arg3, *args); end |
| 221 |
expect { |
| 222 |
expect(obj).to respond_to(:foo).with(2).arguments |
| 223 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/) |
| 224 |
end |
| 225 |
end |
| 226 |
|
| 227 |
RSpec.describe "expect(...).to respond_to(:sym).with(1..2).arguments" do |
| 228 |
it "passes if target responds to any number of arguments" do |
| 229 |
obj = Object.new |
| 230 |
def obj.foo(*args); end |
| 231 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 232 |
end |
| 233 |
|
| 234 |
it "passes if target responds to one or more arguments" do |
| 235 |
obj = Object.new |
| 236 |
def obj.foo(a, *args); end |
| 237 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 238 |
end |
| 239 |
|
| 240 |
it "passes if target responds to one or two arguments" do |
| 241 |
obj = Object.new |
| 242 |
def obj.foo(a, b=nil); end |
| 243 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 244 |
end |
| 245 |
|
| 246 |
it "passes if target responds to one to three arguments" do |
| 247 |
obj = Object.new |
| 248 |
def obj.foo(a, b=nil, c=nil); end |
| 249 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 250 |
end |
| 251 |
|
| 252 |
it "passes if target is new and initialize reponds to arguments" do |
| 253 |
klass = Class.new { def initialize(arg, arg2=nil, arg3=nil); end } |
| 254 |
expect(klass).to respond_to(:new).with(1..2).arguments |
| 255 |
end |
| 256 |
|
| 257 |
it "fails if target does not respond to :sym" do |
| 258 |
obj = Object.new |
| 259 |
expect { |
| 260 |
expect(obj).to respond_to(:some_method).with(1..2).arguments |
| 261 |
}.to fail_with(/expected .* to respond to :some_method/) |
| 262 |
end |
| 263 |
|
| 264 |
it "fails if :sym expects 0 args" do |
| 265 |
obj = Object.new |
| 266 |
def obj.foo; end |
| 267 |
expect { |
| 268 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 269 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/) |
| 270 |
end |
| 271 |
|
| 272 |
it "fails if :sym expects 1 args" do |
| 273 |
obj = Object.new |
| 274 |
def obj.foo(arg); end |
| 275 |
expect { |
| 276 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 277 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/) |
| 278 |
end |
| 279 |
|
| 280 |
it "fails if :sym expects 2 args" do |
| 281 |
obj = Object.new |
| 282 |
def obj.foo(a, b); end |
| 283 |
expect { |
| 284 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 285 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/) |
| 286 |
end |
| 287 |
|
| 288 |
it "fails if :sym expects 3 or more args" do |
| 289 |
obj = Object.new |
| 290 |
def obj.foo(arg, arg2, arg3, *args); end |
| 291 |
expect { |
| 292 |
expect(obj).to respond_to(:foo).with(1..2).arguments |
| 293 |
}.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/) |
| 294 |
end |
| 295 |
|
| 296 |
it "fails when new unless initialize matches the signature" do |
| 297 |
klass = Class.new { def initialize(arg, arg2, arg3, *args); end } |
| 298 |
expect { |
| 299 |
expect(klass).to respond_to(:new).with(1..2).arguments |
| 300 |
}.to fail_with(/expected #<Class.*> to respond to :new with 1..2 arguments/) |
| 301 |
end |
| 302 |
end |
| 303 |
|
| 304 |
RSpec.describe "expect(...).to respond_to(:sym).with_unlimited_arguments" do |
| 305 |
it "passes if target responds to any number of arguments" do |
| 306 |
obj = Object.new |
| 307 |
def obj.foo(*args); end |
| 308 |
expect(obj).to respond_to(:foo).with_unlimited_arguments |
| 309 |
end |
| 310 |
|
| 311 |
it "passes if target responds to a minimum number of arguments" do |
| 312 |
obj = Object.new |
| 313 |
def obj.foo(arg, arg2, arg3, *args); end |
| 314 |
expect(obj).to respond_to(:foo).with(3).arguments.and_unlimited_arguments |
| 315 |
end |
| 316 |
|
| 317 |
it "passes when target is new and initialize responds to any number of arguments" do |
| 318 |
|
| 319 |
klass_2 = Class.new { def initialize(*args); end } |
| 320 |
expect(klass_2).to respond_to(:new).with_unlimited_arguments |
| 321 |
end |
| 322 |
|
| 323 |
it "fails if target does not respond to :sym" do |
| 324 |
obj = Object.new |
| 325 |
expect { |
| 326 |
expect(obj).to respond_to(:some_method).with_unlimited_arguments |
| 327 |
}.to fail_with(/expected .* to respond to :some_method/) |
| 328 |
end |
| 329 |
|
| 330 |
it "fails if :sym expects a minimum number of arguments" do |
| 331 |
obj = Object.new |
| 332 |
def obj.some_method(arg, arg2, arg3, *args); end |
| 333 |
expect { |
| 334 |
expect(obj).to respond_to(:some_method).with_unlimited_arguments |
| 335 |
}.to fail_with(/expected .* to respond to :some_method with unlimited arguments/) |
| 336 |
end |
| 337 |
|
| 338 |
it "fails if :sym expects a limited number of arguments" do |
| 339 |
obj = Object.new |
| 340 |
def obj.some_method(arg); end |
| 341 |
expect { |
| 342 |
expect(obj).to respond_to(:some_method).with_unlimited_arguments |
| 343 |
}.to fail_with(/expected .* to respond to :some_method with unlimited arguments/) |
| 344 |
end |
| 345 |
|
| 346 |
it "fails when target is new and initialize responds to a set number of arguments" do |
| 347 |
klass = Class.new { def initialize(a); end } |
| 348 |
expect { |
| 349 |
expect(klass).to respond_to(:new).with_unlimited_arguments |
| 350 |
}.to fail_with(/expected .* to respond to :new with unlimited arguments/) |
| 351 |
end |
| 352 |
end |
| 353 |
|
| 354 |
RSpec.describe "expect(...).not_to respond_to(:sym)" do |
| 355 |
it "passes if target does not respond to :sym" do |
| 356 |
expect(Object.new).not_to respond_to(:some_method) |
| 357 |
end |
| 358 |
|
| 359 |
it "fails if target responds to :sym" do |
| 360 |
expect { |
| 361 |
expect(Object.new).not_to respond_to(:methods) |
| 362 |
}.to fail_with(/expected #<Object:.*> not to respond to :methods/) |
| 363 |
end |
| 364 |
end |
| 365 |
|
| 366 |
RSpec.describe "expect(...).not_to respond_to(:sym).with(1).argument" do |
| 367 |
it "fails if target responds to :sym with 1 arg" do |
| 368 |
obj = Object.new |
| 369 |
def obj.foo(arg); end |
| 370 |
expect { |
| 371 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 372 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/) |
| 373 |
end |
| 374 |
|
| 375 |
it "fails if target responds to :sym with any number of args" do |
| 376 |
obj = Object.new |
| 377 |
def obj.foo(*args); end |
| 378 |
expect { |
| 379 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 380 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/) |
| 381 |
end |
| 382 |
|
| 383 |
it "fails if target responds to :sym with one or more args" do |
| 384 |
obj = Object.new |
| 385 |
def obj.foo(a, *args); end |
| 386 |
expect { |
| 387 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 388 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/) |
| 389 |
end |
| 390 |
|
| 391 |
it "will fail when target is new and initialize matches the argument signature" do |
| 392 |
klass = Class.new { def initialize(a); end } |
| 393 |
expect { |
| 394 |
expect(klass).to_not respond_to(:new).with(1).argument |
| 395 |
}.to fail_with(/not to respond to :new with 1 argument/) |
| 396 |
end |
| 397 |
|
| 398 |
it "passes if target does not respond to :sym" do |
| 399 |
obj = Object.new |
| 400 |
expect(obj).not_to respond_to(:some_method).with(1).argument |
| 401 |
end |
| 402 |
|
| 403 |
it "passes if :sym expects 0 args" do |
| 404 |
obj = Object.new |
| 405 |
def obj.foo; end |
| 406 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 407 |
end |
| 408 |
|
| 409 |
it "passes if :sym expects 2 args" do |
| 410 |
obj = Object.new |
| 411 |
def obj.foo(arg, arg2); end |
| 412 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 413 |
end |
| 414 |
|
| 415 |
it "passes if :sym expects 2 or more args" do |
| 416 |
obj = Object.new |
| 417 |
def obj.foo(arg, arg2, *args); end |
| 418 |
expect(obj).not_to respond_to(:foo).with(1).argument |
| 419 |
end |
| 420 |
|
| 421 |
it "will pass when target is new and initialize does not matches the argument signature" do |
| 422 |
klass = Class.new { def initialize(a, b); end } |
| 423 |
expect(klass).to_not respond_to(:new).with(1).argument |
| 424 |
end |
| 425 |
end |
| 426 |
|
| 427 |
RSpec.describe "expect(...).not_to respond_to(message1, message2)" do |
| 428 |
it "passes if target does not respond to either message1 or message2" do |
| 429 |
expect(Object.new).not_to respond_to(:some_method, :some_other_method) |
| 430 |
end |
| 431 |
|
| 432 |
it "fails if target responds to message1 but not message2" do |
| 433 |
expect { |
| 434 |
expect(Object.new).not_to respond_to(:object_id, :some_method) |
| 435 |
}.to fail_with(/expected #<Object:.*> not to respond to :object_id/) |
| 436 |
end |
| 437 |
|
| 438 |
it "fails if target responds to message2 but not message1" do |
| 439 |
expect { |
| 440 |
expect(Object.new).not_to respond_to(:some_method, :object_id) |
| 441 |
}.to fail_with(/expected #<Object:.*> not to respond to :object_id/) |
| 442 |
end |
| 443 |
|
| 444 |
it "fails if target responds to both message1 and message2" do |
| 445 |
expect { |
| 446 |
expect(Object.new).not_to respond_to(:class, :object_id) |
| 447 |
}.to fail_with(/expected #<Object:.*> not to respond to :class, :object_id/) |
| 448 |
end |
| 449 |
end |
| 450 |
|
| 451 |
RSpec.describe "expect(...).not_to respond_to(:sym).with(2).arguments" do |
| 452 |
it "fails if target responds to :sym with 2 args" do |
| 453 |
obj = Object.new |
| 454 |
def obj.foo(a1, a2); end |
| 455 |
expect { |
| 456 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 457 |
}.to fail_with(/expected .* not to respond to :foo with 2 arguments/) |
| 458 |
end |
| 459 |
|
| 460 |
it "fails if target responds to :sym with any number args" do |
| 461 |
obj = Object.new |
| 462 |
def obj.foo(*args); end |
| 463 |
expect { |
| 464 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 465 |
}.to fail_with(/expected .* not to respond to :foo with 2 arguments/) |
| 466 |
end |
| 467 |
|
| 468 |
it "fails if target responds to :sym with one or more args" do |
| 469 |
obj = Object.new |
| 470 |
def obj.foo(a, *args); end |
| 471 |
expect { |
| 472 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 473 |
}.to fail_with(/expected .* not to respond to :foo with 2 arguments/) |
| 474 |
end |
| 475 |
|
| 476 |
it "fails if target responds to :sym with two or more args" do |
| 477 |
obj = Object.new |
| 478 |
def obj.foo(a, b, *args); end |
| 479 |
expect { |
| 480 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 481 |
}.to fail_with(/expected .* not to respond to :foo with 2 arguments/) |
| 482 |
end |
| 483 |
|
| 484 |
it "passes if target does not respond to :sym" do |
| 485 |
obj = Object.new |
| 486 |
expect(obj).not_to respond_to(:some_method).with(2).arguments |
| 487 |
end |
| 488 |
|
| 489 |
it "passes if :sym expects 0 args" do |
| 490 |
obj = Object.new |
| 491 |
def obj.foo; end |
| 492 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 493 |
end |
| 494 |
|
| 495 |
it "passes if :sym expects 2 args" do |
| 496 |
obj = Object.new |
| 497 |
def obj.foo(arg); end |
| 498 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 499 |
end |
| 500 |
|
| 501 |
it "passes if :sym expects 3 or more args" do |
| 502 |
obj = Object.new |
| 503 |
def obj.foo(a, b, c, *arg); end |
| 504 |
expect(obj).not_to respond_to(:foo).with(2).arguments |
| 505 |
end |
| 506 |
end |
| 507 |
|
| 508 |
RSpec.describe "expect(...).not_to respond_to(:sym).with(1..2).arguments" do |
| 509 |
it "fails if target responds to :sym with one or two args" do |
| 510 |
obj = Object.new |
| 511 |
def obj.foo(a1, a2=nil); end |
| 512 |
expect { |
| 513 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 514 |
}.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/) |
| 515 |
end |
| 516 |
|
| 517 |
it "fails if target responds to :sym with any number args" do |
| 518 |
obj = Object.new |
| 519 |
def obj.foo(*args); end |
| 520 |
expect { |
| 521 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 522 |
}.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/) |
| 523 |
end |
| 524 |
|
| 525 |
it "fails if target responds to :sym with one or more args" do |
| 526 |
obj = Object.new |
| 527 |
def obj.foo(a, *args); end |
| 528 |
expect { |
| 529 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 530 |
}.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/) |
| 531 |
end |
| 532 |
|
| 533 |
it "will fail when target is new and initialize matches the argument signature" do |
| 534 |
klass = Class.new { def initialize(a, *args); end } |
| 535 |
expect { |
| 536 |
expect(klass).to_not respond_to(:new).with(1..2).argument |
| 537 |
}.to fail_with(/not to respond to :new with 1..2 argument/) |
| 538 |
end |
| 539 |
|
| 540 |
it "passes if target does not respond to :sym" do |
| 541 |
obj = Object.new |
| 542 |
expect(obj).not_to respond_to(:some_method).with(1..2).arguments |
| 543 |
end |
| 544 |
|
| 545 |
it "passes if :sym expects 0 args" do |
| 546 |
obj = Object.new |
| 547 |
def obj.foo; end |
| 548 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 549 |
end |
| 550 |
|
| 551 |
it "passes if :sym expects 1 arg" do |
| 552 |
obj = Object.new |
| 553 |
def obj.foo(arg); end |
| 554 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 555 |
end |
| 556 |
|
| 557 |
it "passes if :sym expects 2 args" do |
| 558 |
obj = Object.new |
| 559 |
def obj.foo(a, b); end |
| 560 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 561 |
end |
| 562 |
|
| 563 |
it "passes if :sym expects 3 or more args" do |
| 564 |
obj = Object.new |
| 565 |
def obj.foo(a, b, c, *arg); end |
| 566 |
expect(obj).not_to respond_to(:foo).with(1..2).arguments |
| 567 |
end |
| 568 |
|
| 569 |
it "passes when target is new and initialize does not match the argument signature" do |
| 570 |
klass = Class.new { def initialize(a); end } |
| 571 |
expect(klass).to_not respond_to(:new).with(1..2).argument |
| 572 |
end |
| 573 |
end |
| 574 |
|
| 575 |
RSpec.describe "expect(...).not_to respond_to(:sym).with_unlimited_arguments" do |
| 576 |
it "fails if target responds to :sym with any number args" do |
| 577 |
obj = Object.new |
| 578 |
def obj.foo(*args); end |
| 579 |
expect { |
| 580 |
expect(obj).not_to respond_to(:foo).with_unlimited_arguments |
| 581 |
}.to fail_with(/expected .* not to respond to :foo with unlimited arguments/) |
| 582 |
end |
| 583 |
|
| 584 |
it "will fail when target is new and initialize has unlimited arguments" do |
| 585 |
klass = Class.new { def initialize(*args); end } |
| 586 |
expect { |
| 587 |
expect(klass).to_not respond_to(:new).with_unlimited_arguments |
| 588 |
}.to fail_with(/not to respond to :new with unlimited argument/) |
| 589 |
end |
| 590 |
|
| 591 |
it "passes if target does not respond to :sym" do |
| 592 |
obj = Object.new |
| 593 |
expect(obj).not_to respond_to(:some_method).with_unlimited_arguments |
| 594 |
end |
| 595 |
|
| 596 |
it "passes if :sym expects a limited number of arguments" do |
| 597 |
obj = Object.new |
| 598 |
def obj.some_method(arg); end |
| 599 |
expect(obj).not_to respond_to(:some_method).with_unlimited_arguments |
| 600 |
end |
| 601 |
|
| 602 |
it "passes if :sym expects a minimum number of arguments" do |
| 603 |
obj = Object.new |
| 604 |
def obj.some_method(arg, arg2, arg3, *args); end |
| 605 |
expect(obj).not_to respond_to(:some_method).with_unlimited_arguments |
| 606 |
end |
| 607 |
|
| 608 |
it "passes when target is new and initialize has arguments" do |
| 609 |
klass = Class.new { def initialize(a, *args); end } |
| 610 |
expect(klass).to_not respond_to(:new).with_unlimited_arguments |
| 611 |
end |
| 612 |
end |
| 613 |
|
| 614 |
if RSpec::Support::RubyFeatures.kw_args_supported? |
| 615 |
RSpec.describe "expect(...).to respond_to(:sym).with_keywords(:foo, :bar)" do |
| 616 |
it 'passes if target responds to :sym with specified optional keywords' do |
| 617 |
obj = Object.new |
| 618 |
eval %{def obj.foo(a: nil, b: nil); end} |
| 619 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b) |
| 620 |
end |
| 621 |
|
| 622 |
it 'passes if target responds to :sym with any keywords' do |
| 623 |
obj = Object.new |
| 624 |
eval %{def obj.foo(**kw_args); end} |
| 625 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b) |
| 626 |
end |
| 627 |
|
| 628 |
it 'passes if target is :new with keywords' do |
| 629 |
|
| 630 |
klass = eval %{Class.new { def initialize(a: nil, b: nil); end}} |
| 631 |
expect(klass).to respond_to(:new).with_keywords(:a, :b) |
| 632 |
|
| 633 |
|
| 634 |
klass_2 = eval %{Class.new { def initialize(**kw_args); end}} |
| 635 |
expect(klass_2).to respond_to(:new).with_keywords(:a, :b) |
| 636 |
end |
| 637 |
|
| 638 |
it "fails if target does not respond to :sym" do |
| 639 |
obj = Object.new |
| 640 |
expect { |
| 641 |
expect(obj).to respond_to(:some_method).with_keywords(:a, :b) |
| 642 |
}.to fail_with(/expected .* to respond to :some_method with keywords :a and :b/) |
| 643 |
end |
| 644 |
|
| 645 |
it "fails if :sym does not expect specified keywords" do |
| 646 |
obj = Object.new |
| 647 |
def obj.foo; end |
| 648 |
expect { |
| 649 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b) |
| 650 |
}.to fail_with(/expected .* to respond to :foo with keywords :a and :b/) |
| 651 |
end |
| 652 |
|
| 653 |
it "fails if :sym does not expect many specified keywords" do |
| 654 |
obj = Object.new |
| 655 |
def obj.foo; end |
| 656 |
expect { |
| 657 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b, :c, :d, :e, :f) |
| 658 |
}.to fail_with(/expected .* to respond to :foo with keywords :a, :b, :c, :d, :e, and :f/) |
| 659 |
end |
| 660 |
|
| 661 |
it 'fails if target is :new but initialize does not expect the right keywords' do |
| 662 |
|
| 663 |
klass = eval %{Class.new { def initialize(a: nil); end}} |
| 664 |
expect { |
| 665 |
expect(klass).to respond_to(:new).with_keywords(:a, :b) |
| 666 |
}.to fail_with(/expected .* to respond to :new with keywords :a and :b/) |
| 667 |
end |
| 668 |
|
| 669 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 670 |
it "passes if target responds to :sym with specified required keywords" do |
| 671 |
obj = Object.new |
| 672 |
eval %{def obj.foo(a:, b:, c: nil, d: nil); end} |
| 673 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b) |
| 674 |
end |
| 675 |
|
| 676 |
it "passes if target responds to :sym with keyword arg splat" do |
| 677 |
obj = Object.new |
| 678 |
eval %{def obj.foo(**rest); end} |
| 679 |
expect(obj).to respond_to(:foo).with_keywords(:a, :b) |
| 680 |
end |
| 681 |
|
| 682 |
it 'passes if target is :new and initialize has specified required keywords' do |
| 683 |
|
| 684 |
klass = eval %{Class.new { def initialize(a:, b:); end}} |
| 685 |
expect(klass).to respond_to(:new).with_keywords(:a, :b) |
| 686 |
end |
| 687 |
|
| 688 |
it "fails if :sym expects specified optional keywords but expects missing required keywords" do |
| 689 |
obj = Object.new |
| 690 |
eval %{def obj.foo(a:, b:, c: nil, d: nil); end} |
| 691 |
expect { |
| 692 |
expect(obj).to respond_to(:some_method).with_keywords(:c, :d) |
| 693 |
}.to fail_with(/expected .* to respond to :some_method with keywords :c and :d/) |
| 694 |
end |
| 695 |
|
| 696 |
it "fails if target responds to :sym with keyword arg splat but missing required keywords" do |
| 697 |
obj = Object.new |
| 698 |
eval %{def obj.foo(a:, b:, **rest); end} |
| 699 |
expect { |
| 700 |
expect(obj).to respond_to(:some_method).with_keywords(:c, :d) |
| 701 |
}.to fail_with(/expected .* to respond to :some_method with keywords :c and :d/) |
| 702 |
end |
| 703 |
|
| 704 |
it 'fails if target is :new and initialize has is missing required keywords' do |
| 705 |
|
| 706 |
klass = eval %{Class.new { def initialize(a:, b:); end}} |
| 707 |
expect { |
| 708 |
expect(klass).to respond_to(:new).with_keywords(:c, :d) |
| 709 |
}.to fail_with(/expected .* to respond to :new with keywords :c and :d/) |
| 710 |
end |
| 711 |
end |
| 712 |
end |
| 713 |
|
| 714 |
RSpec.describe "expect(...).to respond_to(:sym).with(2).arguments.and_keywords(:foo, :bar)" do |
| 715 |
it "passes if target responds to :sym with 2 args and specified optional keywords" do |
| 716 |
obj = Object.new |
| 717 |
eval %{def obj.foo(a, b, u: nil, v: nil); end} |
| 718 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 719 |
end |
| 720 |
|
| 721 |
it "passes if target responds to :sym with any number of arguments and specified optional keywords" do |
| 722 |
obj = Object.new |
| 723 |
eval %{def obj.foo(*args, u: nil, v: nil); end} |
| 724 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 725 |
end |
| 726 |
|
| 727 |
it "passes if target responds to :sym with one or more arguments and specified optional keywords" do |
| 728 |
obj = Object.new |
| 729 |
eval %{def obj.foo(a, *args, u: nil, v: nil); end} |
| 730 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 731 |
end |
| 732 |
|
| 733 |
it "passes if target responds to :sym with two or more arguments and specified optional keywords" do |
| 734 |
obj = Object.new |
| 735 |
eval %{def obj.foo(a, b, *args, u: nil, v: nil); end} |
| 736 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 737 |
end |
| 738 |
|
| 739 |
it "fails if target does not respond to :sym" do |
| 740 |
obj = Object.new |
| 741 |
expect { |
| 742 |
expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 743 |
}.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/) |
| 744 |
end |
| 745 |
|
| 746 |
it "fails if :sym expects 1 argument" do |
| 747 |
obj = Object.new |
| 748 |
eval %{def obj.foo(a, u: nil, v: nil); end} |
| 749 |
expect { |
| 750 |
expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 751 |
}.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/) |
| 752 |
end |
| 753 |
|
| 754 |
it "fails if :sym does not expect specified keywords" do |
| 755 |
obj = Object.new |
| 756 |
def obj.foo(a, b); end |
| 757 |
expect { |
| 758 |
expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 759 |
}.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/) |
| 760 |
end |
| 761 |
|
| 762 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 763 |
it "passes if target responds to :sym with 2 args and specified required keywords" do |
| 764 |
obj = Object.new |
| 765 |
eval %{def obj.foo(a, b, u:, v:); end} |
| 766 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 767 |
end |
| 768 |
|
| 769 |
it "passes if target responds to :sym with 2 args and keyword arg splat" do |
| 770 |
obj = Object.new |
| 771 |
eval %{def obj.foo(a, b, **rest); end} |
| 772 |
expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 773 |
end |
| 774 |
|
| 775 |
it "passes for new when target responds to initialize with a mixture of arguments" do |
| 776 |
klass = eval %{Class.new { def initialize(a, b, c:, d: nil); end }} |
| 777 |
expect(klass).to respond_to(:new).with(2).arguments.and_keywords(:c, :d) |
| 778 |
end |
| 779 |
|
| 780 |
it "fails if :sym expects 2 arguments and specified optional keywords but expects missing required keywords" do |
| 781 |
obj = Object.new |
| 782 |
eval %{def obj.foo(a, b, u: nil, v: nil, x:, y:); end} |
| 783 |
expect { |
| 784 |
expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 785 |
}.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/) |
| 786 |
end |
| 787 |
|
| 788 |
it "fails for new when target responds to initialize with the wrong mixture of arguments" do |
| 789 |
klass = eval %{Class.new { def initialize(a, b, c:); end }} |
| 790 |
expect { |
| 791 |
expect(klass).to respond_to(:new).with(2).arguments.and_keywords(:c, :d) |
| 792 |
}.to fail_with(/expected .* to respond to :new with 2 arguments and keywords :c and :d/) |
| 793 |
end |
| 794 |
end |
| 795 |
end |
| 796 |
|
| 797 |
RSpec.describe "expect(...).to respond_to(:sym).with_any_keywords" do |
| 798 |
it "passes if target responds to any keywords" do |
| 799 |
obj = Object.new |
| 800 |
eval %{def obj.foo(**kw_args); end} |
| 801 |
expect(obj).to respond_to(:foo).with_any_keywords |
| 802 |
end |
| 803 |
|
| 804 |
it "passes when initialize responds to any keywords and we check new" do |
| 805 |
klass = eval %{Class.new { def initialize(**kw_args); end }} |
| 806 |
expect(klass).to respond_to(:new).with_any_keywords |
| 807 |
end |
| 808 |
|
| 809 |
it "fails if target does not respond to :sym" do |
| 810 |
obj = Object.new |
| 811 |
expect { |
| 812 |
expect(obj).to respond_to(:some_method).with_any_keywords |
| 813 |
}.to fail_with(/expected .* to respond to :some_method/) |
| 814 |
end |
| 815 |
|
| 816 |
it "fails if :sym expects a limited set of keywords" do |
| 817 |
obj = Object.new |
| 818 |
eval %{def obj.some_method(a: nil, b: nil); end} |
| 819 |
expect { |
| 820 |
expect(obj).to respond_to(:some_method).with_any_keywords |
| 821 |
}.to fail_with(/expected .* to respond to :some_method with any keywords/) |
| 822 |
end |
| 823 |
|
| 824 |
it "fails when initialize expects a limited set of keywords and we check new" do |
| 825 |
klass = eval %{Class.new { def initialize(a: nil); end }} |
| 826 |
expect { |
| 827 |
expect(klass).to respond_to(:new).with_any_keywords |
| 828 |
}.to fail_with(/expected .* to respond to :new with any keywords/) |
| 829 |
end |
| 830 |
|
| 831 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 832 |
it "fails if :sym expects missing required keywords" do |
| 833 |
obj = Object.new |
| 834 |
eval %{def obj.some_method(a:, b:, **kw_args); end} |
| 835 |
expect { |
| 836 |
expect(obj).to respond_to(:some_method).with_any_keywords |
| 837 |
}.to fail_with(/expected .* to respond to :some_method with any keywords/) |
| 838 |
end |
| 839 |
|
| 840 |
it "fails if :initialize expects missing required keywords when we test new" do |
| 841 |
klass = eval %{Class.new { def initialize(a:, **kw_args); end }} |
| 842 |
eval %{def initialize(a:, b:, **kw_args); end} |
| 843 |
expect { |
| 844 |
expect(klass).to respond_to(:new).with_any_keywords |
| 845 |
}.to fail_with(/expected .* to respond to :new with any keywords/) |
| 846 |
end |
| 847 |
end |
| 848 |
end |
| 849 |
|
| 850 |
RSpec.describe "expect(...).not_to respond_to(:sym).with_keywords(:foo, :bar)" do |
| 851 |
it "fails if target responds to :sym with specified optional keywords" do |
| 852 |
obj = Object.new |
| 853 |
eval %{def obj.foo(a: nil, b: nil); end} |
| 854 |
expect { |
| 855 |
expect(obj).not_to respond_to(:foo).with_keywords(:a, :b) |
| 856 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/) |
| 857 |
end |
| 858 |
|
| 859 |
it "fails if target responds to :sym with any keywords" do |
| 860 |
obj = Object.new |
| 861 |
eval %{def obj.foo(**kw_args); end} |
| 862 |
expect { |
| 863 |
expect(obj).not_to respond_to(:foo).with_keywords(:a, :b) |
| 864 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/) |
| 865 |
end |
| 866 |
|
| 867 |
it "fails if target initialize responds to expected keywords when checking new" do |
| 868 |
klass = eval %{Class.new { def initialize(**kw_args); end }} |
| 869 |
expect { |
| 870 |
expect(klass).not_to respond_to(:new).with_keywords(:a, :b) |
| 871 |
}.to fail_with(/expected .* not to respond to :new with keywords :a and :b/) |
| 872 |
end |
| 873 |
|
| 874 |
it "passes if target does not respond to :sym" do |
| 875 |
obj = Object.new |
| 876 |
expect(obj).not_to respond_to(:some_method).with_keywords(:a, :b) |
| 877 |
end |
| 878 |
|
| 879 |
it "passes if :sym does not expect specified keywords" do |
| 880 |
obj = Object.new |
| 881 |
eval %{def obj.foo(a: nil, b: nil); end} |
| 882 |
expect(obj).not_to respond_to(:some_method).with_keywords(:c, :d) |
| 883 |
end |
| 884 |
|
| 885 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 886 |
it "fails if target responds to :sym with specified required keywords" do |
| 887 |
obj = Object.new |
| 888 |
eval %{def obj.foo(a:, b:); end} |
| 889 |
expect { |
| 890 |
expect(obj).not_to respond_to(:foo).with_keywords(:a, :b) |
| 891 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/) |
| 892 |
end |
| 893 |
|
| 894 |
it "fails if target responds to :sym with keyword arg splat" do |
| 895 |
obj = Object.new |
| 896 |
eval %{def obj.foo(**rest); end} |
| 897 |
expect { |
| 898 |
expect(obj).not_to respond_to(:foo).with_keywords(:a, :b) |
| 899 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/) |
| 900 |
end |
| 901 |
|
| 902 |
it "passes if :sym expects missing required keywords" do |
| 903 |
obj = Object.new |
| 904 |
eval %{def obj.foo(a:, b:, c: nil, d: nil); end} |
| 905 |
expect(obj).not_to respond_to(:some_method).with_keywords(:c, :d) |
| 906 |
end |
| 907 |
|
| 908 |
it "passes if :initialize expects missing required keywords for :new" do |
| 909 |
klass = eval %{Class.new { def initialize(a:, b:, c: nil, d: nil); end }} |
| 910 |
expect(klass).not_to respond_to(:new).with_keywords(:c, :d) |
| 911 |
end |
| 912 |
end |
| 913 |
end |
| 914 |
|
| 915 |
RSpec.describe "expect(...).to respond_to(:sym).with_keywords(:foo)" do |
| 916 |
it "fails if target responds to :sym with specified optional keyword" do |
| 917 |
obj = Object.new |
| 918 |
eval %{def obj.foo(a: nil); end} |
| 919 |
expect { |
| 920 |
expect(obj).not_to respond_to(:foo).with_keywords(:a) |
| 921 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keyword :a/) |
| 922 |
end |
| 923 |
|
| 924 |
it "fails if target responds to :sym with any keywords" do |
| 925 |
obj = Object.new |
| 926 |
eval %{def obj.foo(**kw_args); end} |
| 927 |
expect { |
| 928 |
expect(obj).not_to respond_to(:foo).with_keywords(:a) |
| 929 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keyword :a/) |
| 930 |
end |
| 931 |
|
| 932 |
it "fails if target initialize responds to expected keywords when checking new" do |
| 933 |
klass = eval %{Class.new { def initialize(**kw_args); end }} |
| 934 |
expect { |
| 935 |
expect(klass).not_to respond_to(:new).with_keywords(:a) |
| 936 |
}.to fail_with(/expected .* not to respond to :new with keyword :a/) |
| 937 |
end |
| 938 |
|
| 939 |
it "passes if target does not respond to :sym" do |
| 940 |
obj = Object.new |
| 941 |
expect(obj).not_to respond_to(:some_method).with_keywords(:a) |
| 942 |
end |
| 943 |
|
| 944 |
it "passes if :sym does not expect specified keywords" do |
| 945 |
obj = Object.new |
| 946 |
eval %{def obj.foo(a: nil, b: nil); end} |
| 947 |
expect(obj).not_to respond_to(:some_method).with_keywords(:c) |
| 948 |
end |
| 949 |
|
| 950 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 951 |
it "fails if target responds to :sym with specified required keywords" do |
| 952 |
obj = Object.new |
| 953 |
eval %{def obj.foo(a:); end} |
| 954 |
expect { |
| 955 |
expect(obj).not_to respond_to(:foo).with_keywords(:a) |
| 956 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keyword :a/) |
| 957 |
end |
| 958 |
|
| 959 |
it "fails if target responds to :sym with keyword arg splat" do |
| 960 |
obj = Object.new |
| 961 |
eval %{def obj.foo(**rest); end} |
| 962 |
expect { |
| 963 |
expect(obj).not_to respond_to(:foo).with_keywords(:a) |
| 964 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with keyword :a/) |
| 965 |
end |
| 966 |
|
| 967 |
it "passes if :sym expects missing required keywords" do |
| 968 |
obj = Object.new |
| 969 |
eval %{def obj.foo(a:, b:, c: nil); end} |
| 970 |
expect(obj).not_to respond_to(:some_method).with_keywords(:c) |
| 971 |
end |
| 972 |
|
| 973 |
it "passes if :initialize expects missing required keywords for :new" do |
| 974 |
klass = eval %{Class.new { def initialize(a:, b:, c: nil); end }} |
| 975 |
expect(klass).not_to respond_to(:new).with_keywords(:c) |
| 976 |
end |
| 977 |
end |
| 978 |
end |
| 979 |
|
| 980 |
RSpec.describe "expect(...).not_to respond_to(:sym).with(2).arguments.and_keywords(:foo, :bar)" do |
| 981 |
it "fails if target responds to :sym with 2 args and specified optional keywords" do |
| 982 |
obj = Object.new |
| 983 |
eval %{def obj.foo(a, b, u: nil, v: nil); end} |
| 984 |
expect { |
| 985 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 986 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 987 |
end |
| 988 |
|
| 989 |
it "fails if target responds to :sym with any number of arguments and specified optional keywords" do |
| 990 |
obj = Object.new |
| 991 |
eval %{def obj.foo(*args, u: nil, v: nil); end} |
| 992 |
expect { |
| 993 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 994 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 995 |
end |
| 996 |
|
| 997 |
it "fails if target responds to :sym with one or more arguments and specified optional keywords" do |
| 998 |
obj = Object.new |
| 999 |
eval %{def obj.foo(a, *args, u: nil, v: nil); end} |
| 1000 |
expect { |
| 1001 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 1002 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 1003 |
end |
| 1004 |
|
| 1005 |
it "fails if target responds to :sym with two or more arguments and specified optional keywords" do |
| 1006 |
obj = Object.new |
| 1007 |
eval %{def obj.foo(a, b, *args, u: nil, v: nil); end} |
| 1008 |
expect { |
| 1009 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 1010 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 1011 |
end |
| 1012 |
|
| 1013 |
it "passes if target does not respond to :sym" do |
| 1014 |
obj = Object.new |
| 1015 |
expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 1016 |
end |
| 1017 |
|
| 1018 |
it "passes if :sym expects 1 argument" do |
| 1019 |
obj = Object.new |
| 1020 |
eval %{def obj.foo(a, u: nil, v: nil); end} |
| 1021 |
expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 1022 |
end |
| 1023 |
|
| 1024 |
it "passes if :sym does not expect specified keywords" do |
| 1025 |
obj = Object.new |
| 1026 |
def obj.foo(a, b); end |
| 1027 |
expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 1028 |
end |
| 1029 |
|
| 1030 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 1031 |
it "fails if target responds to :sym with 2 args and specified required keywords" do |
| 1032 |
obj = Object.new |
| 1033 |
eval %{def obj.foo(a, b, u:, v:); end} |
| 1034 |
expect { |
| 1035 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 1036 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 1037 |
end |
| 1038 |
|
| 1039 |
it "fails if target responds to :sym with 2 args and keyword arg splat" do |
| 1040 |
obj = Object.new |
| 1041 |
eval %{def obj.foo(a, b, **rest); end} |
| 1042 |
expect { |
| 1043 |
expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v) |
| 1044 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/) |
| 1045 |
end |
| 1046 |
|
| 1047 |
it "passes if :sym expects 2 arguments and specified optional keywords but expects missing required keywords" do |
| 1048 |
obj = Object.new |
| 1049 |
eval %{def obj.foo(a, b, u: nil, v: nil, x:, y:); end} |
| 1050 |
expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v) |
| 1051 |
end |
| 1052 |
end |
| 1053 |
end |
| 1054 |
|
| 1055 |
RSpec.describe "expect(...).not_to respond_to(:sym).with_any_keywords" do |
| 1056 |
it "fails if target responds to any keywords" do |
| 1057 |
obj = Object.new |
| 1058 |
eval %{def obj.foo(**kw_args); end} |
| 1059 |
expect { |
| 1060 |
expect(obj).not_to respond_to(:foo).with_any_keywords |
| 1061 |
}.to fail_with(/expected #<Object:.*> not to respond to :foo with any keywords/) |
| 1062 |
end |
| 1063 |
|
| 1064 |
it "passes if target does not respond to :sym" do |
| 1065 |
obj = Object.new |
| 1066 |
expect(obj).not_to respond_to(:some_method).with_any_keywords |
| 1067 |
end |
| 1068 |
|
| 1069 |
it "passes if :sym expects a limited set of keywords" do |
| 1070 |
obj = Object.new |
| 1071 |
eval %{def obj.some_method(a: nil, b: nil); end} |
| 1072 |
expect(obj).not_to respond_to(:some_method).with_any_keywords |
| 1073 |
end |
| 1074 |
|
| 1075 |
if RSpec::Support::RubyFeatures.required_kw_args_supported? |
| 1076 |
it "passes if :sym expects missing required keywords" do |
| 1077 |
obj = Object.new |
| 1078 |
eval %{def obj.some_method(a:, b:, **kw_args); end} |
| 1079 |
expect(obj).not_to respond_to(:some_method).with_any_keywords |
| 1080 |
end |
| 1081 |
end |
| 1082 |
end |
| 1083 |
end |