2015-11-23 98 views
-4

我有一個RSpec測試如下:Rspec的合格測試

it "calculates highest count words across lines to be will, it, really" do 
    solution.analyze_file 

    expect(solution.highest_count_words_across_lines).to be nil 

    solution.calculate_line_with_highest_frequency 

    words_found = solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten 
    expect(words_found).to match_array ["will", "it", "really"] 
end 

它給出了一個錯誤

Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really 
    Failure/Error: words_found solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten 
    NoMethodError: 
     undefined method `highest_wf_words' for "really":String 
    # ./spec/solution_spec.rb:38:in `map' 
    # ./spec/solution_spec.rb:38:in `block (3 levels) in <top (required)>' 

在另一方面,如果我寫這個測試沒有

.map(&:highest_wf_words).flatten 

然後它通過。

@highest_count_words_across_lines = ["really","will","it"] 

我怎樣才能使此測試通過,而includding映射:

.map(&:highest_wf_words).flatten? 
+0

你能正確格式化你的代碼/錯誤信息嗎?並重新說明你的問題標題? – onebree

+0

我更新我的問題。現在看到 –

+0

有錯誤提示您正在調用'''''真高'_wf_words'。 highest_wf_words()方法的樣子是什麼? – 7stud

回答

0

solution.highest_count_words_across_lines是一個字符串數組。當您這樣做時:solution.highest_count_words_across_lines.map(&:highest_wf_words)您在每個數組項上調用highest_wf_words,並且該方法沒有爲String定義(這是錯誤消息所說的內容)。

我想,其實你想是這樣的,而不是:

words_found = solution.highest_count_words_across_lines.map(|x| highest_wf_words(x)).flatten 

UPDATE

如果你映射的目的是讓只有words_found包括highest_wf_words,假設這是一個陣列,你可以這樣做:

words_found = solution.highest_count_words_across_lines.flatten & highest_wf_words 

[1,2,3] & [2,3,4] 
=> [2, 3] 
+0

我接受這個問題,因爲它是正確的。它幫助我理解。但我的問題是,我不能改變這個 solution.highest_count_words_across_lines.map &:highest_wf_words) 哪個更改可以匹配? –