Ответ 1
Если вы просмотрите RSpec Expectations 2.99 и RSpec Expectations 2.14 и найдите раздел - Truthiness и экзистенциализм, вы найдете
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)
# ...............
# ...
Но вы просматриваете RSpec Expectations 3.0, указанные имена методов были изменены на -
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)
# ...........
#......
Кажется, вы находитесь в 3.0 и используете метод, существовавший до этой версии. Таким образом, вы получили ошибку.
Я поместил код в свой файл test.rb, как показано ниже: -
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
И мой файл spec/test_spec.rb -
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
Теперь я запускаю код с моей консоли, и он работает:
[email protected]:~/Ruby> rspec -v
2.14.8
[email protected]:~/Ruby> rspec spec
.
Finished in 0.00169 seconds
1 example, 0 failures
Теперь я меняю код в файле spec/test_spec.rb: -
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
и снова запустите тест: -
[email protected]:~/Ruby> rspec -v
2.14.8
[email protected]:~/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
[email protected]:~/Ruby>
Теперь они также упомянули в 3.0.0.beta1/2013-11-07 changelog
Переименуйте
be_true
иbe_false
вbe_truthy
иbe_falsey
. (Сэм Фиппен)