Matcher be_true ruby/rspec

Asked

Viewed 56 times

1

I’m starting to study about ruby/rspec by following a book, but I’m not making progress using the matcher be_true.

Follow below the error:

BagOfWords#push is possible to put words on it (FAILED - 1)

Failures:

1) BagOfWords#push is possible to put words on it
  Failure/Error: expect(isTrue).to be_true
    expected true to respond to `true?`
  # ./spec/bag_of_words_spec.rb:12:in `block (3 levels) in <top (require d)>'

Finished in 0.015 seconds (files took 0.54403 seconds to load)  
1 example, 1 failure

Failed examples:

rspec ./spec/bag_of_words_spec.rb:7 # BagOfWords#push is possible to put words on it
  • 1

    Which version of Rspec are you using? You can try using be_truthy if it is Rspec 3.

  • Sorry for the delay in answering... That’s right, I’m using Rspec 3, I just switched and it worked worth.

  • 1

    @18, can you include the answer to the problem and accept it? This avoids questions that are already answered in the comments being listed as "No answer".

2 answers

1


According to this thread, the matchers be_true and be_false were replaced by be_truthy and be_falsey. You can still use the be true and be false (note that they are separate).

The difference between be true and be_truthy is that a test using the be_truthy will pass if the object is not nil. The reverse happens to the be false and be_falsey.

Of official documentation, we have:

expect(objeto).to be_truthy   # passa se o objeto não for nil ou false
expect(objeto).to be true     # passa somente se o objeto for true
expect(objeto).to be_falsy    # passa se o objeto for nil ou false
expect(objeto).to be false    # passa somente se o objeto for false

Examples

it { expect(true).to be true }        # passa
it { expect("string").to be true }    # falha
it { expect(nil).to be true }         # falha
it { expect(false).to be true }       # falha

it { expect(false).to be false }      # passa
it { expect("string").to be false}    # falha
it { expect(nil).to be false}         # falha
it { expect(true).to be false}        # falha

0

Answer: Which version of Rspec are you using? You can try be_truthy if you are Rspec 3. - @Bruno dos Santos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.