| 1 | RSpec.describe "expect(...).to be_predicate" do |
| 2 | it "passes when actual returns true for :predicate?" do |
| 3 | actual = double("actual", :happy? => true) |
| 4 | expect(actual).to be_happy |
| 5 | end |
| 6 | |
| 7 | it 'allows composable aliases to be defined' do |
| 8 | RSpec::Matchers.alias_matcher :a_user_who_is_happy, :be_happy |
| 9 | actual = double("actual", :happy? => true) |
| 10 | expect(actual).to a_user_who_is_happy |
| 11 | expect(a_user_who_is_happy.description).to eq("a user who is happy") |
| 12 | |
| 13 | RSpec::Matchers.alias_matcher :a_user_who_is_an_admin, :be_an_admin |
| 14 | actual = double("actual", :admin? => true) |
| 15 | expect(actual).to a_user_who_is_an_admin |
| 16 | expect(a_user_who_is_an_admin.description).to eq("a user who is an admin") |
| 17 | |
| 18 | RSpec::Matchers.alias_matcher :an_animal_that_is_a_canine, :be_a_canine |
| 19 | actual = double("actual", :canine? => true) |
| 20 | expect(actual).to an_animal_that_is_a_canine |
| 21 | expect(an_animal_that_is_a_canine.description).to eq("an animal that is a canine") |
| 22 | end |
| 23 | |
| 24 | it 'composes gracefully' do |
| 25 | RSpec::Matchers.alias_matcher :a_happy_object, :be_happy |
| 26 | expect([ |
| 27 | double, |
| 28 | double(:happy? => false), |
| 29 | double(:happy? => true), |
| 30 | ]).to include a_happy_object |
| 31 | end |
| 32 | |
| 33 | it "passes when actual returns true for :predicates? (present tense)" do |
| 34 | actual = double("actual", :exists? => true, :exist? => true) |
| 35 | expect(actual).to be_exist |
| 36 | end |
| 37 | |
| 38 | context "when actual returns false for :predicate?" do |
| 39 | it "fails when actual returns false for :predicate?" do |
| 40 | actual = double("actual", :happy? => false) |
| 41 | expect { |
| 42 | expect(actual).to be_happy |
| 43 | }.to fail_with("expected `#{actual.inspect}.happy?` to return true, got false") |
| 44 | end |
| 45 | |
| 46 | it "only calls :predicate? once" do |
| 47 | actual = double "actual", :happy? => false |
| 48 | |
| 49 | expect(actual).to receive(:happy?).once |
| 50 | expect { expect(actual).to be_happy }.to fail |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | it "fails when actual returns nil for :predicate?" do |
| 55 | actual = double("actual", :happy? => nil) |
| 56 | expect { |
| 57 | expect(actual).to be_happy |
| 58 | }.to fail_with("expected `#{actual.inspect}.happy?` to return true, got nil") |
| 59 | end |
| 60 | |
| 61 | context "when strict_predicate_matchers is set to true" do |
| 62 | it "fails when actual returns 42 for :predicate?" do |
| 63 | actual = double("actual", :happy? => 42) |
| 64 | expect { |
| 65 | expect(actual).to be_happy |
| 66 | }.to fail_with("expected `#{actual.inspect}.happy?` to return true, got 42") |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | context "when strict_predicate_matchers is set to false" do |
| 71 | around do |example| |
| 72 | RSpec::Expectations.configuration.strict_predicate_matchers = false |
| 73 | example.run |
| 74 | RSpec::Expectations.configuration.strict_predicate_matchers = true |
| 75 | end |
| 76 | |
| 77 | it "passes when actual returns truthy value for :predicate?" do |
| 78 | actual = double("actual", :happy? => 42) |
| 79 | expect(actual).to be_happy |
| 80 | end |
| 81 | |
| 82 | it "states actual predicate used when it fails" do |
| 83 | actual = double("actual", :happy? => false) |
| 84 | expect { |
| 85 | expect(actual).to be_happy |
| 86 | }.to fail_with("expected `#{actual.inspect}.happy?` to be truthy, got false") |
| 87 | end |
| 88 | |
| 89 | it "allows dynamic matchers to pass for supplied methods on spies" do |
| 90 | thing = spy("thing", :has_recv? => true) |
| 91 | expect(thing).to have_recv(:foo) |
| 92 | expect(thing).to have_received(:has_recv?).with(:foo) |
| 93 | end |
| 94 | |
| 95 | it "does not allow dynamic matchers to pass for inferred methods on spies" do |
| 96 | thing = spy("thing") |
| 97 | expect { |
| 98 | expect(thing).to have_recv(:foo) |
| 99 | }.to fail |
| 100 | expect(thing).not_to have_received(:has_recv?) |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | it "fails when actual does not respond to :predicate?" do |
| 105 | expect { |
| 106 | expect(Object.new).to be_happy |
| 107 | }.to fail_including("to respond to `happy?`") |
| 108 | end |
| 109 | |
| 110 | it "indicates when a predicate was attempted to be matched against an unexpected `nil`" do |
| 111 | expect { |
| 112 | expect(nil).to be_happy |
| 113 | }.to fail_including("expected nil to respond to `happy?`") |
| 114 | end |
| 115 | |
| 116 | it 'handles arguments to the predicate' do |
| 117 | object = Object.new |
| 118 | def object.predicate?(return_val); return_val; end |
| 119 | expect(object).to be_predicate(true) |
| 120 | expect(object).to_not be_predicate(false) |
| 121 | |
| 122 | expect { expect(object).to be_predicate }.to raise_error(ArgumentError) |
| 123 | expect { expect(object).to be_predicate(false) }.to fail |
| 124 | expect { expect(object).not_to be_predicate(true) }.to fail |
| 125 | end |
| 126 | |
| 127 | it 'handles arguments to the predicate implementing to_hash' do |
| 128 | object = Object.new |
| 129 | def object.predicate?(value); value.to_return; end |
| 130 | |
| 131 | hash_a_like = |
| 132 | Class.new do |
| 133 | def to_hash |
| 134 | {:to_return => true} |
| 135 | end |
| 136 | |
| 137 | def to_return |
| 138 | true |
| 139 | end |
| 140 | end |
| 141 | |
| 142 | expect(object).to be_predicate(hash_a_like.new) |
| 143 | end |
| 144 | |
| 145 | it 'handles keyword arguments to the predicate', :if => RSpec::Support::RubyFeatures.required_kw_args_supported? do |
| 146 | object = Object.new |
| 147 | binding.eval(<<-CODE, __FILE__, __LINE__) |
| 148 | def object.predicate?(returns:); returns; end |
| 149 | |
| 150 | expect(object).to be_predicate(returns: true) |
| 151 | expect(object).to_not be_predicate(returns: false) |
| 152 | |
| 153 | expect { expect(object).to be_predicate(returns: false) }.to fail |
| 154 | expect { expect(object).to_not be_predicate(returns: true) }.to fail |
| 155 | CODE |
| 156 | |
| 157 | expect { expect(object).to be_predicate }.to raise_error(ArgumentError) |
| 158 | expect { expect(object).to be_predicate(true) }.to raise_error(ArgumentError) |
| 159 | end |
| 160 | |
| 161 | it 'falls back to a present-tense form of the predicate when needed' do |
| 162 | mouth = Object.new |
| 163 | def mouth.frowns?(return_val); return_val; end |
| 164 | |
| 165 | expect(mouth).to be_frown(true) |
| 166 | end |
| 167 | |
| 168 | it 'fails when :predicate? is private' do |
| 169 | privately_happy = Class.new do |
| 170 | private |
| 171 | def happy? |
| 172 | true |
| 173 | end |
| 174 | end |
| 175 | expect { expect(privately_happy.new).to be_happy }.to fail_with(/private/) |
| 176 | end |
| 177 | |
| 178 | it 'does not call :private_methods when the object publicly responds to the message' do |
| 179 | publicly_happy = double('happy') |
| 180 | expect(publicly_happy).to receive(:happy?) { true } |
| 181 | expect(publicly_happy).not_to receive(:private_methods) |
| 182 | expect(publicly_happy).to be_happy |
| 183 | end |
| 184 | |
| 185 | it "fails on error other than NameError" do |
| 186 | actual = double("actual") |
| 187 | expect(actual).to receive(:foo?).and_raise("aaaah") |
| 188 | expect { |
| 189 | expect(actual).to be_foo |
| 190 | }.to raise_error(/aaaah/) |
| 191 | end |
| 192 | |
| 193 | it 'raises an error when :predicate? exists but raises NameError' do |
| 194 | actual_class = Class.new do |
| 195 | def foo? |
| 196 | raise NameError, "aaaah" |
| 197 | end |
| 198 | end |
| 199 | expect { |
| 200 | expect(actual_class.new).to be_foo |
| 201 | }.to raise_error(NameError, /aaaah/) |
| 202 | end |
| 203 | |
| 204 | it "fails on error other than NameError (with the present tense predicate)" do |
| 205 | actual = double |
| 206 | expect(actual).to receive(:foos?).and_raise("aaaah") |
| 207 | expect { |
| 208 | expect(actual).to be_foo |
| 209 | }.to raise_error(/aaaah/) |
| 210 | end |
| 211 | |
| 212 | it "does not support operator chaining like a basic `be` matcher does" do |
| 213 | matcher = be_happy |
| 214 | value = double(:happy? => false) |
| 215 | expect(matcher == value).to be false |
| 216 | end |
| 217 | |
| 218 | it "indicates `be true` or `be_truthy` when using `be_true`" do |
| 219 | actual = double("actual") |
| 220 | expect { |
| 221 | expect(actual).to be_true |
| 222 | }.to fail_with(/or perhaps you meant `be true` or `be_truthy`/) |
| 223 | end |
| 224 | |
| 225 | it "shows no message if actual responds to `true?` when using `be_true`" do |
| 226 | actual = double("actual", :true? => true) |
| 227 | expect { |
| 228 | expect(actual).to be_true |
| 229 | }.not_to raise_error |
| 230 | end |
| 231 | |
| 232 | it "indicates `be false` or `be_falsey` when using `be_false`" do |
| 233 | actual = double("actual") |
| 234 | expect { |
| 235 | expect(actual).to be_false |
| 236 | }.to fail_with(/or perhaps you meant `be false` or `be_falsey`/) |
| 237 | end |
| 238 | |
| 239 | it "shows no message if actual responds to `false?` when using `be_false`" do |
| 240 | actual = double("actual", :false? => true) |
| 241 | expect { |
| 242 | expect(actual).to be_false |
| 243 | }.not_to raise_error |
| 244 | end |
| 245 | end |
| 246 | |
| 247 | RSpec.describe "expect(...).not_to be_predicate" do |
| 248 | let(:strict_predicate_matchers) { true } |
| 249 | |
| 250 | around do |example| |
| 251 | default = RSpec::Expectations.configuration.strict_predicate_matchers? |
| 252 | RSpec::Expectations.configuration.strict_predicate_matchers = strict_predicate_matchers |
| 253 | example.run |
| 254 | RSpec::Expectations.configuration.strict_predicate_matchers = default |
| 255 | end |
| 256 | |
| 257 | it "passes when actual returns false for :sym?" do |
| 258 | actual = double("actual", :happy? => false) |
| 259 | expect(actual).not_to be_happy |
| 260 | end |
| 261 | |
| 262 | context "when strict_predicate_matchers is set to true" do |
| 263 | it "fails when actual returns nil for :sym?" do |
| 264 | actual = double("actual", :happy? => nil) |
| 265 | expect { |
| 266 | expect(actual).not_to be_happy |
| 267 | }.to fail_with("expected `#{actual.inspect}.happy?` to return false, got nil") |
| 268 | end |
| 269 | end |
| 270 | |
| 271 | context "when strict_predicate_matchers is set to false" do |
| 272 | around do |example| |
| 273 | RSpec::Expectations.configuration.strict_predicate_matchers = false |
| 274 | example.run |
| 275 | RSpec::Expectations.configuration.strict_predicate_matchers = true |
| 276 | end |
| 277 | |
| 278 | it "passes when actual returns nil for :sym?" do |
| 279 | actual = double("actual", :happy? => nil) |
| 280 | expect(actual).not_to be_happy |
| 281 | end |
| 282 | |
| 283 | it "shows actual comparison made when it fails" do |
| 284 | actual = double("actual", :happy? => 42) |
| 285 | expect { |
| 286 | expect(actual).not_to be_happy |
| 287 | }.to fail_with("expected `#{actual.inspect}.happy?` to be falsey, got 42") |
| 288 | end |
| 289 | end |
| 290 | |
| 291 | it "fails when actual returns true for :sym?" do |
| 292 | actual = double("actual", :happy? => true) |
| 293 | expect { |
| 294 | expect(actual).not_to be_happy |
| 295 | }.to fail_with("expected `#{actual.inspect}.happy?` to return false, got true") |
| 296 | end |
| 297 | |
| 298 | it "fails when actual does not respond to :sym?" do |
| 299 | expect { |
| 300 | expect(Object.new).not_to be_happy |
| 301 | }.to fail_including("to respond to `happy?`") |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | RSpec.describe "expect(...).to be_predicate(*args)" do |
| 306 | it "passes when actual returns true for :predicate?(*args)" do |
| 307 | actual = double("actual") |
| 308 | expect(actual).to receive(:older_than?).with(3).and_return(true) |
| 309 | expect(actual).to be_older_than(3) |
| 310 | end |
| 311 | |
| 312 | it "fails when actual returns false for :predicate?(*args)" do |
| 313 | actual = double("actual") |
| 314 | expect(actual).to receive(:older_than?).with(3).and_return(false) |
| 315 | expect { |
| 316 | expect(actual).to be_older_than(3) |
| 317 | }.to fail_with("expected `#{actual.inspect}.older_than?(3)` to return true, got false") |
| 318 | end |
| 319 | |
| 320 | it "fails when actual does not respond to :predicate?" do |
| 321 | expect { |
| 322 | expect(Object.new).to be_older_than(3) |
| 323 | }.to fail_including("to respond to `older_than?`") |
| 324 | end |
| 325 | end |
| 326 | |
| 327 | RSpec.describe "expect(...).not_to be_predicate(*args)" do |
| 328 | it "passes when actual returns false for :predicate?(*args)" do |
| 329 | actual = double("actual") |
| 330 | expect(actual).to receive(:older_than?).with(3).and_return(false) |
| 331 | expect(actual).not_to be_older_than(3) |
| 332 | end |
| 333 | |
| 334 | it "fails when actual returns true for :predicate?(*args)" do |
| 335 | actual = double("actual") |
| 336 | expect(actual).to receive(:older_than?).with(3).and_return(true) |
| 337 | expect { |
| 338 | expect(actual).not_to be_older_than(3) |
| 339 | }.to fail_with("expected `#{actual.inspect}.older_than?(3)` to return false, got true") |
| 340 | end |
| 341 | |
| 342 | it "fails when actual does not respond to :predicate?" do |
| 343 | expect { |
| 344 | expect(Object.new).not_to be_older_than(3) |
| 345 | }.to fail_including("to respond to `older_than?`") |
| 346 | end |
| 347 | end |
| 348 | |
| 349 | RSpec.describe "expect(...).to be_predicate(&block)" do |
| 350 | it "passes when actual returns true for :predicate?(&block)" do |
| 351 | actual = double("actual") |
| 352 | delegate = double("delegate") |
| 353 | expect(actual).to receive(:happy?).and_yield |
| 354 | expect(delegate).to receive(:check_happy).and_return(true) |
| 355 | expect(actual).to be_happy { delegate.check_happy } |
| 356 | end |
| 357 | |
| 358 | it "fails when actual returns false for :predicate?(&block)" do |
| 359 | actual = double("actual") |
| 360 | delegate = double("delegate") |
| 361 | expect(actual).to receive(:happy?).and_yield |
| 362 | expect(delegate).to receive(:check_happy).and_return(false) |
| 363 | expect { |
| 364 | expect(actual).to be_happy { delegate.check_happy } |
| 365 | }.to fail_with("expected `#{actual.inspect}.happy?` to return true, got false") |
| 366 | end |
| 367 | |
| 368 | it "fails when actual does not respond to :predicate?" do |
| 369 | delegate = double("delegate", :check_happy => true) |
| 370 | expect { |
| 371 | expect(Object.new).to be_happy { delegate.check_happy } |
| 372 | }.to fail_including("to respond to `happy?`") |
| 373 | end |
| 374 | |
| 375 | it 'passes the block on to the present-tense predicate form' do |
| 376 | mouth = Object.new |
| 377 | def mouth.frowns?; yield; end |
| 378 | |
| 379 | expect(mouth).to be_frown { true } |
| 380 | expect(mouth).not_to be_frown { false } |
| 381 | end |
| 382 | |
| 383 | it 'works with a do..end block for either predicate form' do |
| 384 | mouth1 = Object.new |
| 385 | def mouth1.frown?; yield; end |
| 386 | mouth2 = Object.new |
| 387 | def mouth2.frowns?; yield; end |
| 388 | |
| 389 | expect(mouth1).to be_frown do |
| 390 | true |
| 391 | end |
| 392 | |
| 393 | expect(mouth1).not_to be_frown do |
| 394 | false |
| 395 | end |
| 396 | |
| 397 | expect(mouth2).to be_frown do |
| 398 | true |
| 399 | end |
| 400 | |
| 401 | expect(mouth2).not_to be_frown do |
| 402 | false |
| 403 | end |
| 404 | end |
| 405 | |
| 406 | it 'prefers a { ... } block to a do/end block because it binds more tightly' do |
| 407 | mouth1 = Object.new |
| 408 | def mouth1.frown?; yield; end |
| 409 | mouth2 = Object.new |
| 410 | def mouth2.frowns?; yield; end |
| 411 | |
| 412 | expect(mouth1).to be_frown { true } do |
| 413 | false |
| 414 | end |
| 415 | |
| 416 | expect(mouth1).not_to be_frown { false } do |
| 417 | true |
| 418 | end |
| 419 | |
| 420 | expect(mouth2).to be_frown { true } do |
| 421 | false |
| 422 | end |
| 423 | |
| 424 | expect(mouth2).not_to be_frown { false } do |
| 425 | true |
| 426 | end |
| 427 | end |
| 428 | end |
| 429 | |
| 430 | RSpec.describe "expect(...).not_to be_predicate(&block)" do |
| 431 | it "passes when actual returns false for :predicate?(&block)" do |
| 432 | actual = double("actual") |
| 433 | delegate = double("delegate") |
| 434 | expect(actual).to receive(:happy?).and_yield |
| 435 | expect(delegate).to receive(:check_happy).and_return(false) |
| 436 | expect(actual).not_to be_happy { delegate.check_happy } |
| 437 | end |
| 438 | |
| 439 | it "fails when actual returns true for :predicate?(&block)" do |
| 440 | actual = double("actual") |
| 441 | delegate = double("delegate") |
| 442 | expect(actual).to receive(:happy?).and_yield |
| 443 | expect(delegate).to receive(:check_happy).and_return(true) |
| 444 | expect { |
| 445 | expect(actual).not_to be_happy { delegate.check_happy } |
| 446 | }.to fail_with("expected `#{actual.inspect}.happy?` to return false, got true") |
| 447 | end |
| 448 | |
| 449 | it "fails when actual does not respond to :predicate?" do |
| 450 | delegate = double("delegate", :check_happy => true) |
| 451 | expect { |
| 452 | expect(Object.new).not_to be_happy { delegate.check_happy } |
| 453 | }.to fail_including("to respond to `happy?`") |
| 454 | end |
| 455 | end |
| 456 | |
| 457 | RSpec.describe "expect(...).to be_predicate(*args, &block)" do |
| 458 | it "passes when actual returns true for :predicate?(*args, &block)" do |
| 459 | actual = double("actual") |
| 460 | delegate = double("delegate") |
| 461 | expect(actual).to receive(:older_than?).with(3).and_yield(3) |
| 462 | expect(delegate).to receive(:check_older_than).with(3).and_return(true) |
| 463 | expect(actual).to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 464 | end |
| 465 | |
| 466 | it "fails when actual returns false for :predicate?(*args, &block)" do |
| 467 | actual = double("actual") |
| 468 | delegate = double("delegate") |
| 469 | expect(actual).to receive(:older_than?).with(3).and_yield(3) |
| 470 | expect(delegate).to receive(:check_older_than).with(3).and_return(false) |
| 471 | expect { |
| 472 | expect(actual).to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 473 | }.to fail_with("expected `#{actual.inspect}.older_than?(3)` to return true, got false") |
| 474 | end |
| 475 | |
| 476 | it "fails when actual does not respond to :predicate?" do |
| 477 | delegate = double("delegate", :check_older_than => true) |
| 478 | expect { |
| 479 | expect(Object.new).to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 480 | }.to fail_including("to respond to `older_than?`") |
| 481 | end |
| 482 | end |
| 483 | |
| 484 | RSpec.describe "expect(...).not_to be_predicate(*args, &block)" do |
| 485 | it "passes when actual returns false for :predicate?(*args, &block)" do |
| 486 | actual = double("actual") |
| 487 | delegate = double("delegate") |
| 488 | expect(actual).to receive(:older_than?).with(3).and_yield(3) |
| 489 | expect(delegate).to receive(:check_older_than).with(3).and_return(false) |
| 490 | expect(actual).not_to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 491 | end |
| 492 | |
| 493 | it "fails when actual returns true for :predicate?(*args, &block)" do |
| 494 | actual = double("actual") |
| 495 | delegate = double("delegate") |
| 496 | expect(actual).to receive(:older_than?).with(3).and_yield(3) |
| 497 | expect(delegate).to receive(:check_older_than).with(3).and_return(true) |
| 498 | expect { |
| 499 | expect(actual).not_to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 500 | }.to fail_with("expected `#{actual.inspect}.older_than?(3)` to return false, got true") |
| 501 | end |
| 502 | |
| 503 | it "fails when actual does not respond to :predicate?" do |
| 504 | delegate = double("delegate", :check_older_than => true) |
| 505 | expect { |
| 506 | expect(Object.new).not_to be_older_than(3) { |age| delegate.check_older_than(age) } |
| 507 | }.to fail_including("to respond to `older_than?`") |
| 508 | end |
| 509 | end |
| 510 | |
| 511 | RSpec.describe "expect(...).to be_truthy" do |
| 512 | it "passes when actual equal?(true)" do |
| 513 | expect(true).to be_truthy |
| 514 | end |
| 515 | |
| 516 | it "passes when actual is 1" do |
| 517 | expect(1).to be_truthy |
| 518 | end |
| 519 | |
| 520 | it "fails when actual equal?(false)" do |
| 521 | expect { |
| 522 | expect(false).to be_truthy |
| 523 | }.to fail_with("expected: truthy value\n got: false") |
| 524 | end |
| 525 | end |
| 526 | |
| 527 | RSpec.describe "expect(...).not_to be_truthy" do |
| 528 | it "passes when actual equal?(false)" do |
| 529 | expect(false).not_to be_truthy |
| 530 | end |
| 531 | |
| 532 | it "passes when actual equal?(nil)" do |
| 533 | expect(nil).not_to be_truthy |
| 534 | end |
| 535 | |
| 536 | it "fails when actual equal?(true)" do |
| 537 | expect { |
| 538 | expect(true).not_to be_truthy |
| 539 | }.to fail_with("expected: falsey value\n got: true") |
| 540 | end |
| 541 | end |
| 542 | |
| 543 | RSpec.describe "expect(...).to be_falsey" do |
| 544 | it "passes when actual equal?(false)" do |
| 545 | expect(false).to be_falsey |
| 546 | end |
| 547 | |
| 548 | it "passes when actual equal?(nil)" do |
| 549 | expect(nil).to be_falsey |
| 550 | end |
| 551 | |
| 552 | it "fails when actual equal?(true)" do |
| 553 | expect { |
| 554 | expect(true).to be_falsey |
| 555 | }.to fail_with("expected: falsey value\n got: true") |
| 556 | end |
| 557 | end |
| 558 | |
| 559 | RSpec.describe "expect(...).to be_falsy" do |
| 560 | it "passes when actual equal?(false)" do |
| 561 | expect(false).to be_falsy |
| 562 | end |
| 563 | |
| 564 | it "passes when actual equal?(nil)" do |
| 565 | expect(nil).to be_falsy |
| 566 | end |
| 567 | |
| 568 | it "fails when actual equal?(true)" do |
| 569 | expect { |
| 570 | expect(true).to be_falsy |
| 571 | }.to fail_with("expected: falsey value\n got: true") |
| 572 | end |
| 573 | end |
| 574 | |
| 575 | RSpec.describe "expect(...).not_to be_falsey" do |
| 576 | it "passes when actual equal?(true)" do |
| 577 | expect(true).not_to be_falsey |
| 578 | end |
| 579 | |
| 580 | it "passes when actual is 1" do |
| 581 | expect(1).not_to be_falsey |
| 582 | end |
| 583 | |
| 584 | it "fails when actual equal?(false)" do |
| 585 | expect { |
| 586 | expect(false).not_to be_falsey |
| 587 | }.to fail_with("expected: truthy value\n got: false") |
| 588 | end |
| 589 | end |
| 590 | |
| 591 | RSpec.describe "expect(...).to be_nil" do |
| 592 | it "passes when actual is nil" do |
| 593 | expect(nil).to be_nil |
| 594 | end |
| 595 | |
| 596 | it "fails when actual is not nil" do |
| 597 | expect { |
| 598 | expect(:not_nil).to be_nil |
| 599 | }.to fail_with(/^expected: nil/) |
| 600 | end |
| 601 | end |
| 602 | |
| 603 | RSpec.describe "expect(...).not_to be_nil" do |
| 604 | it "passes when actual is not nil" do |
| 605 | expect(:not_nil).not_to be_nil |
| 606 | end |
| 607 | |
| 608 | it "fails when actual is nil" do |
| 609 | expect { |
| 610 | expect(nil).not_to be_nil |
| 611 | }.to fail_with(/^expected: not nil/) |
| 612 | end |
| 613 | end |
| 614 | |
| 615 | RSpec.describe "expect(...).to be <" do |
| 616 | it "passes when < operator returns true" do |
| 617 | expect(3).to be < 4 |
| 618 | expect('a').to be < 'b' |
| 619 | end |
| 620 | |
| 621 | it "fails when < operator returns false" do |
| 622 | expect { |
| 623 | expect(3).to be < 3 |
| 624 | }.to fail_with("expected: < 3\n got: 3") |
| 625 | |
| 626 | expect { |
| 627 | expect('a').to be < 'a' |
| 628 | }.to fail_with(%(expected: < "a"\n got: "a")) |
| 629 | end |
| 630 | |
| 631 | it "fails when < operator raises ArgumentError" do |
| 632 | expect { |
| 633 | expect('a').to be < 1 |
| 634 | }.to fail_with(%(expected: < 1\n got: "a")) |
| 635 | end |
| 636 | |
| 637 | it 'fails when < operator is not defined' do |
| 638 | expect { |
| 639 | expect(nil).to be < 1 |
| 640 | }.to fail_with(%(expected: < 1\n got: nil)) |
| 641 | end |
| 642 | |
| 643 | it "describes itself" do |
| 644 | expect(be.<(4).description).to eq "be < 4" |
| 645 | end |
| 646 | |
| 647 | it 'does not lie and say that it is equal to a number' do |
| 648 | matcher = (be < 3) |
| 649 | expect(5 == matcher).to be false |
| 650 | end |
| 651 | end |
| 652 | |
| 653 | RSpec.describe "expect(...).to be <=" do |
| 654 | it "passes when <= operator returns true" do |
| 655 | expect(3).to be <= 4 |
| 656 | expect(4).to be <= 4 |
| 657 | expect('a').to be <= 'b' |
| 658 | expect('a').to be <= 'a' |
| 659 | end |
| 660 | |
| 661 | it "fails when <= operator returns false" do |
| 662 | expect { |
| 663 | expect(3).to be <= 2 |
| 664 | }.to fail_with("expected: <= 2\n got: 3") |
| 665 | |
| 666 | expect { |
| 667 | expect('c').to be <= 'a' |
| 668 | }.to fail_with(%(expected: <= "a"\n got: "c")) |
| 669 | end |
| 670 | |
| 671 | it "fails when <= operator raises ArgumentError" do |
| 672 | expect { |
| 673 | expect('a').to be <= 1 |
| 674 | }.to fail_with(%(expected: <= 1\n got: "a")) |
| 675 | end |
| 676 | |
| 677 | it 'fails when <= operator is not defined' do |
| 678 | expect { |
| 679 | expect(nil).to be <= 1 |
| 680 | }.to fail_with(%(expected: <= 1\n got: nil)) |
| 681 | end |
| 682 | end |
| 683 | |
| 684 | RSpec.describe "expect(...).to be >=" do |
| 685 | it "passes when >= operator returns true" do |
| 686 | expect(4).to be >= 4 |
| 687 | expect(5).to be >= 4 |
| 688 | end |
| 689 | |
| 690 | it "fails when >= operator returns false" do |
| 691 | expect { |
| 692 | expect(3).to be >= 4 |
| 693 | }.to fail_with("expected: >= 4\n got: 3") |
| 694 | |
| 695 | expect { |
| 696 | expect('a').to be >= 'c' |
| 697 | }.to fail_with(%(expected: >= "c"\n got: "a")) |
| 698 | end |
| 699 | |
| 700 | it "fails when >= operator raises ArgumentError" do |
| 701 | expect { |
| 702 | expect('a').to be >= 1 |
| 703 | }.to fail_with(%(expected: >= 1\n got: "a")) |
| 704 | end |
| 705 | |
| 706 | it 'fails when >= operator is not defined' do |
| 707 | expect { |
| 708 | expect(nil).to be >= 1 |
| 709 | }.to fail_with(%(expected: >= 1\n got: nil)) |
| 710 | end |
| 711 | end |
| 712 | |
| 713 | RSpec.describe "expect(...).to be >" do |
| 714 | it "passes when > operator returns true" do |
| 715 | expect(5).to be > 4 |
| 716 | end |
| 717 | |
| 718 | it "fails when > operator returns false" do |
| 719 | expect { |
| 720 | expect(3).to be > 4 |
| 721 | }.to fail_with("expected: > 4\n got: 3") |
| 722 | |
| 723 | expect { |
| 724 | expect('a').to be > 'a' |
| 725 | }.to fail_with(%(expected: > "a"\n got: "a")) |
| 726 | end |
| 727 | |
| 728 | it "fails when > operator raises ArgumentError" do |
| 729 | expect { |
| 730 | expect('a').to be > 1 |
| 731 | }.to fail_with(%(expected: > 1\n got: "a")) |
| 732 | end |
| 733 | |
| 734 | it 'fails when > operator is not defined' do |
| 735 | expect { |
| 736 | expect(nil).to be > 1 |
| 737 | }.to fail_with(%(expected: > 1\n got: nil)) |
| 738 | end |
| 739 | end |
| 740 | |
| 741 | RSpec.describe "expect(...).to be ==" do |
| 742 | it "passes when == operator returns true" do |
| 743 | expect(5).to be == 5 |
| 744 | end |
| 745 | |
| 746 | it "fails when == operator returns false" do |
| 747 | expect { |
| 748 | expect(3).to be == 4 |
| 749 | }.to fail_with("expected: == 4\n got: 3") |
| 750 | |
| 751 | expect { |
| 752 | expect('a').to be == 'c' |
| 753 | }.to fail_with(%(expected: == "c"\n got: "a")) |
| 754 | end |
| 755 | |
| 756 | it "fails when == operator raises ArgumentError" do |
| 757 | failing_equality_klass = Class.new do |
| 758 | def inspect |
| 759 | "<Class>" |
| 760 | end |
| 761 | |
| 762 | def ==(other) |
| 763 | raise ArgumentError |
| 764 | end |
| 765 | end |
| 766 | |
| 767 | expect { |
| 768 | expect(failing_equality_klass.new).to be == 1 |
| 769 | }.to fail_with(%(expected: == 1\n got: <Class>)) |
| 770 | end |
| 771 | |
| 772 | it 'works when the target overrides `#send`' do |
| 773 | klass = Struct.new(:message) do |
| 774 | def send |
| 775 | :message_sent |
| 776 | end |
| 777 | end |
| 778 | |
| 779 | msg_1 = klass.new("hello") |
| 780 | msg_2 = klass.new("hello") |
| 781 | expect(msg_1).to be == msg_2 |
| 782 | end |
| 783 | end |
| 784 | |
| 785 | RSpec.describe "expect(...).to be =~" do |
| 786 | it "passes when =~ operator returns true" do |
| 787 | expect("a string").to be =~ /str/ |
| 788 | end |
| 789 | |
| 790 | it "fails when =~ operator returns false" do |
| 791 | expect { |
| 792 | expect("a string").to be =~ /blah/ |
| 793 | }.to fail_with(%(expected: =~ /blah/\n got: "a string")) |
| 794 | end |
| 795 | end |
| 796 | |
| 797 | RSpec.describe "should be =~", :uses_should do |
| 798 | it "passes when =~ operator returns true" do |
| 799 | "a string".should be =~ /str/ |
| 800 | end |
| 801 | |
| 802 | it "fails when =~ operator returns false" do |
| 803 | expect { |
| 804 | "a string".should be =~ /blah/ |
| 805 | }.to fail_with(%(expected: =~ /blah/\n got: "a string")) |
| 806 | end |
| 807 | end |
| 808 | |
| 809 | RSpec.describe "expect(...).to be ===" do |
| 810 | it "passes when === operator returns true" do |
| 811 | expect(Hash).to be === {} |
| 812 | end |
| 813 | |
| 814 | it "fails when === operator returns false" do |
| 815 | expect { |
| 816 | expect(Hash).to be === "not a hash" |
| 817 | }.to fail_with(%(expected: === "not a hash"\n got: Hash)) |
| 818 | end |
| 819 | end |
| 820 | |
| 821 | RSpec.describe "expect(...).not_to with comparison operators" do |
| 822 | it "coaches user to stop using operators with expect().not_to with numerical comparison operators" do |
| 823 | expect { |
| 824 | expect(5).not_to be < 6 |
| 825 | }.to fail_with("`expect(5).not_to be < 6` not only FAILED, it is a bit confusing.") |
| 826 | |
| 827 | expect { |
| 828 | expect(5).not_to be <= 6 |
| 829 | }.to fail_with("`expect(5).not_to be <= 6` not only FAILED, it is a bit confusing.") |
| 830 | |
| 831 | expect { |
| 832 | expect(6).not_to be > 5 |
| 833 | }.to fail_with("`expect(6).not_to be > 5` not only FAILED, it is a bit confusing.") |
| 834 | |
| 835 | expect { |
| 836 | expect(6).not_to be >= 5 |
| 837 | }.to fail_with("`expect(6).not_to be >= 5` not only FAILED, it is a bit confusing.") |
| 838 | end |
| 839 | |
| 840 | it "coaches users to stop using negation with string comparison operators" do |
| 841 | expect { |
| 842 | expect("foo").not_to be > "bar" |
| 843 | }.to fail_with('`expect("foo").not_to be > "bar"` not only FAILED, it is a bit confusing.') |
| 844 | end |
| 845 | |
| 846 | it "handles ArgumentError as a failure" do |
| 847 | [:<, :<=, :>=, :>].each do |operator| |
| 848 | expect { |
| 849 | expect('a').to_not be.send(operator, 1) |
| 850 | }.to fail_with(%(`expect("a").not_to be #{operator} 1` not only FAILED, it is a bit confusing.)) |
| 851 | end |
| 852 | end |
| 853 | |
| 854 | it "handles NameError as a failure" do |
| 855 | [:<, :<=, :>=, :>].each do |operator| |
| 856 | expect { |
| 857 | expect( |
| 858 | Class.new do |
| 859 | def inspect |
| 860 | '<Class>' |
| 861 | end |
| 862 | |
| 863 | define_method(operator) { |arg| self.non_existent_attribute == operator } |
| 864 | end.new |
| 865 | ).to_not be.send(operator, 1) |
| 866 | }.to fail_with(%(`expect(<Class>).not_to be #{operator} 1` not only FAILED, it is a bit confusing.)) |
| 867 | end |
| 868 | end |
| 869 | end |
| 870 | |
| 871 | RSpec.describe "expect(...).not_to with equality operators" do |
| 872 | it "raises normal error with expect().not_to with equality operators" do |
| 873 | expect { |
| 874 | expect(6).not_to be == 6 |
| 875 | }.to fail_with("`expect(6).not_to be == 6`") |
| 876 | |
| 877 | expect { |
| 878 | expect(String).not_to be === "Hello" |
| 879 | }.to fail_with('`expect(String).not_to be === "Hello"`') |
| 880 | end |
| 881 | |
| 882 | it "handles ArgumentError as a failure" do |
| 883 | failing_equality_klass = Class.new do |
| 884 | def inspect |
| 885 | "<Class>" |
| 886 | end |
| 887 | |
| 888 | def ==(other) |
| 889 | raise ArgumentError |
| 890 | end |
| 891 | end |
| 892 | |
| 893 | expect { |
| 894 | expect(failing_equality_klass.new).not_to be == 1 |
| 895 | }.to fail_with(%(`expect(<Class>).not_to be == 1`)) |
| 896 | |
| 897 | expect { |
| 898 | expect(failing_equality_klass.new).not_to be === 1 |
| 899 | }.to fail_with(%(`expect(<Class>).not_to be === 1`)) |
| 900 | end |
| 901 | |
| 902 | it "handles NameError as a failure" do |
| 903 | failing_equality_klass = Class.new do |
| 904 | def inspect |
| 905 | "<Class>" |
| 906 | end |
| 907 | |
| 908 | undef == |
| 909 | end |
| 910 | |
| 911 | expect { |
| 912 | expect(failing_equality_klass.new).not_to be == 1 |
| 913 | }.to fail_with(%(`expect(<Class>).not_to be == 1`)) |
| 914 | |
| 915 | expect { |
| 916 | expect(failing_equality_klass.new).not_to be === 1 |
| 917 | }.to fail_with(%(`expect(<Class>).not_to be === 1`)) |
| 918 | end |
| 919 | end |
| 920 | |
| 921 | RSpec.describe "expect(...).to be" do |
| 922 | it "passes if actual is truthy" do |
| 923 | expect(true).to be |
| 924 | expect(1).to be |
| 925 | end |
| 926 | |
| 927 | it "fails if actual is false" do |
| 928 | expect { |
| 929 | expect(false).to be |
| 930 | }.to fail_with("expected false to evaluate to true") |
| 931 | end |
| 932 | |
| 933 | it "fails if actual is nil" do |
| 934 | expect { |
| 935 | expect(nil).to be |
| 936 | }.to fail_with("expected nil to evaluate to true") |
| 937 | end |
| 938 | |
| 939 | it "describes itself" do |
| 940 | expect(be.description).to eq "be" |
| 941 | end |
| 942 | end |
| 943 | |
| 944 | RSpec.describe "expect(...).not_to be" do |
| 945 | it "passes if actual is falsy" do |
| 946 | expect(false).not_to be |
| 947 | expect(nil).not_to be |
| 948 | end |
| 949 | |
| 950 | it "fails on true" do |
| 951 | expect { |
| 952 | expect(true).not_to be |
| 953 | }.to fail_with("expected true to evaluate to false") |
| 954 | end |
| 955 | end |
| 956 | |
| 957 | RSpec.describe "expect(...).to be(value)" do |
| 958 | it "delegates to equal" do |
| 959 | matcher = equal(5) |
| 960 | expect(self).to receive(:equal).with(5).and_return(matcher) |
| 961 | expect(5).to be(5) |
| 962 | end |
| 963 | end |
| 964 | |
| 965 | RSpec.describe "expect(...).not_to be(value)" do |
| 966 | it "delegates to equal" do |
| 967 | matcher = equal(4) |
| 968 | expect(self).to receive(:equal).with(4).and_return(matcher) |
| 969 | expect(5).not_to be(4) |
| 970 | end |
| 971 | end |
| 972 | |
| 973 | RSpec.describe "'expect(...).to be' with operator" do |
| 974 | it "includes 'be' in the description" do |
| 975 | expect((be > 6).description).to match(/be > 6/) |
| 976 | expect((be >= 6).description).to match(/be >= 6/) |
| 977 | expect((be <= 6).description).to match(/be <= 6/) |
| 978 | expect((be < 6).description).to match(/be < 6/) |
| 979 | end |
| 980 | end |
| 981 | |
| 982 | RSpec.describe "arbitrary predicate with DelegateClass" do |
| 983 | it "accesses methods defined in the delegating class (LH[#48])" do |
| 984 | in_sub_process_if_possible do |
| 985 | require 'delegate' |
| 986 | class ArrayDelegate < DelegateClass(Array) |
| 987 | def initialize(array) |
| 988 | @internal_array = array |
| 989 | super(@internal_array) |
| 990 | end |
| 991 | |
| 992 | def large? |
| 993 | @internal_array.size >= 5 |
| 994 | end |
| 995 | end |
| 996 | |
| 997 | delegate = ArrayDelegate.new([1, 2, 3, 4, 5, 6]) |
| 998 | expect(delegate).to be_large |
| 999 | end |
| 1000 | end |
| 1001 | end |
| 1002 | |
| 1003 | RSpec.describe "be_a, be_an" do |
| 1004 | it "passes when class matches" do |
| 1005 | expect("foobar").to be_a(String) |
| 1006 | expect([1, 2, 3]).to be_an(Array) |
| 1007 | end |
| 1008 | |
| 1009 | it "fails when class does not match" do |
| 1010 | expect("foobar").not_to be_a(Hash) |
| 1011 | expect([1, 2, 3]).not_to be_an(Integer) |
| 1012 | end |
| 1013 | end |
| 1014 | |
| 1015 | RSpec.describe "be_an_instance_of" do |
| 1016 | it "passes when direct class matches" do |
| 1017 | expect("string").to be_an_instance_of(String) |
| 1018 | end |
| 1019 | |
| 1020 | it "fails when class is higher up hierarchy" do |
| 1021 | expect(5).not_to be_an_instance_of(Numeric) |
| 1022 | end |
| 1023 | end |