Rspec Failing Error: Expected False to Respond to 'False'

rspec failing error: expected false to respond to `false?`

If you browse the RSpec Expectations 2.99 and RSpec Expectations 2.14 and search the section - Truthiness and existentialism, you will find

expect(actual).to be_true  # passes if actual is truthy (not nil or false)
expect(actual).to be_false # passes if actual is falsy (nil or false)
# ...............
# ...

But of you browse RSpec Expectations 3.0 , the above method names got changed to -

expect(actual).to be_truthy    # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
# ...........
#......

It seems you are in 3.0, and using the method which were exist prior to this version. Thus you were getting the error.

I put the code in my test.rb file as below :-

class Dictionary
def initialize
@hash = {}
end

def add(new_entry)
new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition}
end

def entries
@hash
end

def keywords
@hash.keys
end

def include?(word)
if @hash.has_key?(word)
true
else
false
end
end
end

And my spec/test_spec.rb file is -

require_relative "../test.rb"

describe Dictionary do
before do
@d = Dictionary.new
end

it 'can check whether a given keyword exists' do
@d.include?('fish').should be_false
end
end

Now I am running the code from my console, and it works :

arup@linux-wzza:~/Ruby> rspec -v
2.14.8
arup@linux-wzza:~/Ruby> rspec spec
.

Finished in 0.00169 seconds
1 example, 0 failures

Now I am changing the code in my spec/test_spec.rb file :-

require_relative "../test.rb"

describe Dictionary do
before do
@d = Dictionary.new
end

it 'can check whether a given keyword exists' do
@d.include?('fish').should be_falsey
end
end

and again run the test :-

arup@linux-wzza:~/Ruby> rspec -v
2.14.8
arup@linux-wzza:~/Ruby> rspec spec
F

Failures:

1) Dictionary can check whether a given keyword exists
Failure/Error: @d.include?('fish').should be_falsey
NoMethodError:
undefined method `falsey?' for false:FalseClass
# ./spec/test_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.00179 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/test_spec.rb:8 # Dictionary can check whether a given keyword exists
arup@linux-wzza:~/Ruby>

Now, they also mentioned in the 3.0.0.beta1 / 2013-11-07 changelog

Rename be_true and be_false to be_truthy and be_falsey. (Sam Phippen)

expected true to respond to true?

From rspec 3.0, be_true is renamed to be_truthy and be_false to be_falsey

The behavior has not changed. So

(nil).should be_falsey
(false).should be_falsey

will pass, and

(anything other than nil or false).should be_truthy

will also pass

From the changelog 3.0.0.beta1 / 2013-11-07

Rename be_true and be_false to be_truthy and be_falsey. (Sam Phippen)

testing with Rails Rspec - expected and got aren't matching

When writing your tests, you are comparing the entire user object. You should instead compare the attribute you are looking for. Try something like this:

it "ensures name presence" do
user = User.new(email: "ben@ben.com", password: "123123")
expect(user.name).to be_nil
end

As you can see, I've added expect(user.name) instead of just expect(user). Also, just for your information, when you use .new, it doesn't actually save the instance.

rspec test failing on expect(user.save).to be_true

It appears that even though a unique email test was passing, requiring a unique user name was preventing a save. Using Factory Girl with username#{n} got my test to pass.

Rails error with rspec: expected valid? to return true, got false

This got rid of the errors.

micropost_spec.rb

describe Micropost do

let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum",
title: "This is a test title",
privacy: "1",
groups: "This is a test Group",
loc1T: "21 Bond St. Toronto, Ontario",
loc1Lat: "43.654653",
loc1Lon: "-79.377627",
loc2T: "21 Bond St. Toronto, Ontario",
loc2Lat: "43.654653",
loc2Lon: "-79.377627",
startTime: "Jan 1, 2000 12:01:01",
endTime: "Jan 2, 2000 12:01:01",
imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif")

puts @micropost.errors.messages
}

Model's validation test does not work in rspec

You're not structuring the if part of your validation correctly. You're currently checking whether the symbol :is_employed? equals false, which it never will. You can check out the correct syntax here:

https://guides.rubyonrails.org/active_record_validations.html#conditional-validation

You can either do

class WorkHistory < ApplicationRecord
belongs_to :account

validates :name,
:since_date,
:position, presence: true
validates :is_employed, inclusion: [true, false]
validates :until_date, presence: true, if: -> { :not_employed? }

private

def not_enployed?
!is_employed?
end
end

or

class WorkHistory < ApplicationRecord
belongs_to :account

validates :name,
:since_date,
:position, presence: true
validates :is_employed, inclusion: [true, false]
validates :until_date, presence: true, unless: Proc.new { |work_history| work_history.is_employed? }
end

RSpec - expected equal?( Time Object ) to return true, got false

Rather than compare the time zones directly (and get bogged down in subtle differences), you could try comparing the string representations to each other, since that's what you're checking with in the console anyhow.

Depending on how you use this timezone in your app, this should be sufficient to say they are essentially the same time.

expect(subject.in_time_zone.round.to_s).to eq(0.day.ago.midnight.in_time_zone.round.to_s)

You could probably drop the .round if you wanted to as well.

EDIT

Try changing your code so there is no difference between what is printed and what is compared:

it "To return current shift datetime" do
a = subject.in_time_zone.round.to_s
b = 0.day.ago.midnight.in_time_zone.round.to_s

puts a
puts b

expect(a).to eq(b)
end


Related Topics



Leave a reply



Submit