2013-08-23 2 views
0

저는 루비 튜토리얼을 배우려고합니다. 인쇄 가능한 메소드를 테스트하는 마지막 예제를 전달하려고합니다. 나는 루비 프로그램 내에서 메소드를 직접 호출하여 메소드를 테스트했으며, 필요한 메소드가 무엇인지 정확하게 알려준다. 내 코드가 제대로 전달되지 못하게하는 요인은 무엇입니까? 어떤 도움이라도 대단히 감사합니다.rspec 테스트가 통과하지 않습니다

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 

    it "doesn't include a prefix that wasn't added as a word in and of itself" do 
    @d.add('fish') 
    @d.include?('fi').should be_false 
    end 

    it "doesn't find a word in empty dictionary" do 
    @d.find('fi').should be_empty # {} 
    end 

    it 'finds nothing if the prefix matches nothing' do 
    @d.add('fiend') 
    @d.add('great') 
    @d.find('nothing').should be_empty 
    end 

    it "finds an entry" do 
    @d.add('fish' => 'aquatic animal') 
    @d.find('fish').should == {'fish' => 'aquatic animal'} 
    end 

    it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do 
    @d.add('fish' => 'aquatic animal') 
    @d.add('fiend' => 'wicked person') 
    @d.add('great' => 'remarkable') 
    @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'} 
    end 

    it 'lists keywords alphabetically' do 
    @d.add('zebra' => 'African land animal with stripes') 
    @d.add('fish' => 'aquatic animal') 
    @d.add('apple' => 'fruit') 
    @d.keywords.should == %w(apple fish zebra) 
    end 

    it 'can produce printable output like so: [keyword] "definition"' do 
    @d.add('zebra' => 'African land animal with stripes') 
    @d.add('fish' => 'aquatic animal') 
    @d.add('apple' => 'fruit') 
    @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"} 
    end 
end 

여기에 내가 인쇄 기능을 위해 지금까지 만든 작업은 다음과 같습니다 :

다음은 RSpec에 파일의

class Dictionary 
def initialize(opts = {}) 
    @opts = opts 
end 

def entries 
    @opts 
end 

def add(opts) 
    opts.is_a?(String) ? @opts.merge!(opts => nil) : @opts.merge!(opts) 
end 

def keywords 
    @opts.keys.sort 
end 

def include?(key) 
    @opts.has_key?(key) 
end 

def find(key) 
    @opts.select { |word,defin| word.scan(key).join == key } 
end 

def printable 
    opts_sorted = @opts.sort_by { |word,defin| word} 
    opts_sorted.each do |word,defin| 
     print "[#{word}] \"#{defin}\"\n" 
    end 
end 
end 

을 여기에 오류 발생 :

1) Dictionary can produce printable output like so: [keyword] "definition" 
    Failure/Error: @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal 
"\n[zebra] "African land animal with stripes"} 
     expected: "[apple] \"fruit\"\n[fish] \"aquatic animal\"\n[zebra] \"African lan 
d animal with stripes\"" 
      got: [["apple", "fruit"], ["fish", "aquatic animal"], ["zebra", "African 
land animal with stripes"]] (using ==) 
     Diff: 
     @@ -1,4 +1,4 @@ 
     -[apple] "fruit" 
     -[fish] "aquatic animal" 
     -[zebra] "African land animal with stripes" 
     +["apple", "fruit"] 
     +["fish", "aquatic animal"] 
     +["zebra", "African land animal with stripes"] 
    # ./11_dictionary/dictionary_spec.rb:81:in `block (2 levels) in <top (required)> 
' 

답변

0

I 문제가 귀하의 printable 메소드가 반환하고자하는 것을 반환하지 않는다고 생각하십시오.

printable의 반환 값은 메서드의 마지막 문 (opts_sorted.each ...)의 값입니다. 예상 된 문자열을 출력하기 위해 print을 사용하고 있지만이 값은 메서드의 반환 값 (테스트중인 값)을 변경하지 않습니다.

def printable 
    opts_sorted = @opts.sort_by { |word, defin| word} 
    opts_sorted.map{ |word, defin| "[#{word}] \"#{defin}\"\n" }.join 
end 

map 포맷 문자열의 배열로 해시의 키 - 값 쌍을 변형시킬 것이다 당신이 그들을 원하는 방식으로 다음 join : 반환 할 경우

대신 인쇄 문자열이 시도 그것들을 단일 문자열에 추가합니다.

1

당신을 위해 행운아 나는이 모든 것을 지금 막했다! 아래 설명에 대한 설명과 함께 응답 코드가 있습니다! 나는 이것이 4 년 후에도 여전히 도움이되기를 바랍니다!

class Dictionary             # Create the class 
    attr_accessor :entries           # Attribute Accessor; this is our setter and getter 

    def initialize(entries = {})         # Create constructor; if there is no value passed the default value is {} 
    @entries = {}             # Declare instance variable with empty hash 

    if entries.is_a? Hash           # is_a? is a method where it sees if its a class; Hash is the class we compare it to 
     entries.each {|key, value| @entries[key] = value}   # if there is a value that's passed we copy the hash to our instance variable 
    end               # End conditional 
    end                # End constructor 

    def keywords             # Main purpose of this method is to return what's inside our keys 
    keywords = []             # Create empty keyword variable 
    @entries.each_key {|key| keywords.push(key.to_s)}    # each_key method only takes the keys in our hash and pushes them into the keywords array 
    keywords.sort             # We sort the keywords variable 
    end                # End method 

    def add(entry)             # add method adds in an entry either a hash or a string 
    if entry.is_a? Hash           # If the argument belongs to the class Hash; or if its a hash 
     entry.each {|key, value| @entries[key] = value}    # Then we copy the key and values to our instance variable 
    elsif entry.is_a? String          # If the arguemnt belongs to the class String; or if its a String 
     @entries[entry] = nil          # We create a key for that string and set the value to nil 
    end               # End conditional 
    end                # End method 

    def include?(entry)            # include? method this checks if the argument is in our entries and returns a boolean value 
    @entries.has_key?(entry)          # has_key? checks the instance variable if it has the key 
    end                # End method 

    def find(entry)             # find method finds if certain letters are in our keys 
    @entries.select {|key| /#{entry}/.match(key)}     # select gets the keys that match a certain keyword in our entries 
    # if @entries.has_key?(entry)         # First attepmt to solve the test case 
    # @entries.select {|key,value| key == entry} 
    # else 
    # puts {} 
    # end 
    end                # End method 

    def printable             # printable method just prints out our entries like a dictionary 
    printable = []            # Create an empty array 
    @entries.sort.each do |key,value|        # Sort and iterate to each key-value pair 
     printable.push("[#{key}] \"#{value}\"")      # push the key-value pair formatted accordingly to the test case 
    end               # End hash iteration 

    printable.join("\n")           # join with newlines to get desired result 
    end                # End method 
end                # End class 
관련 문제