ruby - rspec failing error: expected false to respond to `false?` -
i running portion of test:
describe dictionary before @d = dictionary.new end 'can check whether given keyword exists' @d.include?('fish').should be_false end
with code:
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
i don't know i'm doing wrong, tests keep failing , saying this:
> 1) dictionary can check whether given keyword exists > failure/error: @d.include?('fish').should be_false > expected false respond `false?`
i confused @ error since seems giving correct answer. appreciate if take few minutes tell me what's wrong code. thank tons.
if browse rspec expectations 2.99 , rspec expectations 2.14 , search section - truthiness , existentialism, find
expect(actual).to be_true # passes if actual truthy (not nil or false) expect(actual).to be_false # passes if actual falsy (nil or false) # ............... # ...
but of browse rspec expectations 3.0 , above method names got changed -
expect(actual).to be_truthy # passes if actual truthy (not nil or false) expect(actual).to true # passes if actual == true expect(actual).to be_falsey # passes if actual falsy (nil or false) # ........... #......
it seems in 3.0, , using method exist prior version. getting error.
i put code in test.rb file 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 spec/test_spec.rb file -
require_relative "../test.rb" describe dictionary before @d = dictionary.new end 'can check whether given keyword exists' @d.include?('fish').should be_false end end
now running code console, , 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 changing code in spec/test_spec.rb file :-
require_relative "../test.rb" describe dictionary before @d = dictionary.new end 'can check whether given keyword exists' @d.include?('fish').should be_falsey end end
and again run test :-
arup@linux-wzza:~/ruby> rspec -v 2.14.8 arup@linux-wzza:~/ruby> rspec spec f failures: 1) dictionary can check whether given keyword exists failure/error: @d.include?('fish').should be_falsey nomethoderror: undefined method `falsey?' 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 given keyword exists arup@linux-wzza:~/ruby>
now, mentioned in 3.0.0.beta1 / 2013-11-07 changelog
rename
be_true
,be_false
be_truthy
,be_falsey
. (sam phippen)
Comments
Post a Comment