所以我試圖在Ruby中創建一個字典對象,並讓它通過一堆RSPEC測試作爲項目的一部分。到目前爲止它一直很好,但是我被困在一個特定的測試中。這裏的RSPEC,直到該測試:Ruby中的字典對象
require 'dictionary'
describe Dictionary do
before do
@d = Dictionary.new
end
it 'is empty when created' do
@d.entries.should == {}
end
it 'can add whole entries with keyword and definition' do
@d.add('fish' => 'aquatic animal')
@d.entries.should == {'fish' => 'aquatic animal'}
@d.keywords.should == ['fish']
end
it 'add keywords (without definition)' do
@d.add('fish')
@d.entries.should == {'fish' => nil}
@d.keywords.should == ['fish']
end
it 'can check whether a given keyword exists' do
@d.include?('fish').should be_false
end
it "doesn't cheat when checking whether a given keyword exists" do
@d.include?('fish').should be_false # if the method is empty, this test passes with nil returned
@d.add('fish')
@d.include?('fish').should be_true # confirms that it actually checks
@d.include?('bird').should be_false # confirms not always returning true after add
end
end
一切經過到目前爲止除了最後測試「檢查一個給定的關鍵字是否存在時,不會欺騙」。我試圖圍繞如何讓這個通過,但迄今沒有成功。任何幫助將不勝感激。這是我到目前爲止:
class Dictionary
attr_accessor :keywords, :entries
def initialize
@entries = {}
end
def add(defs)
defs.each do |word, definition|
@entries[word] = definition
end
end
def keywords
input = []
@entries.each do |key, value|
input << key
end
input.sort
end
def include?(key)
self.keywords.include?(keywords.to_s)
end
end
在此先感謝!
「在檢查給定關鍵字是否存在時不作弊」中的哪一個失敗? – sawa 2013-03-05 16:52:21
原諒我,如果這是粗魯的,但我可以問這個對象的重點是什麼?我沒有看到任何不是由哈希提供的功能.. – 2013-03-05 16:52:26
你忽略提及它如何失敗。 – 2013-03-05 16:54:00