2016-01-04 46 views
0

我有一個按預期工作的紅寶石模塊。然而,當我試圖耙測試,它失敗的:紅寶石模塊工作,但耙測試失敗

Error: test_directions(TestNAME) : NoMethodError: undefined method 'each' for "north":String 

/Users/antony1/Documents/Ruby/projects/ex48/lib/lexicon.rb:20:in 'scan' 

/Users/antony1/Documents/Ruby/projects/ex48/tests/test_ex48.rb:7:in 'test_directions' 


     4: class TestNAME < Test::Unit::TestCase 
     5: 
     6: def test_directions() 
    => 7:  assert_equal(Lexicon.scan("north"),[['direction', 'north']]) 
     8:  result = Lexicon.scan("north south east") 
     9:  
     10:  assert_equal(result, [['direction', 'north'], 

class Lexicon 
@sentence = [] 
@direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back'] 

def self.scan(words) 
    words.each {|word| 
     recognize = false 
     if @direction.include?(word) == true 
     direction = ['direction', word] 
     @sentence.push(direction) 
     recognize = true 
     end 
    } 
end 
end 

這裏是我的測試文件:

require './lib/lexicon.rb' 
require "test/unit" 

class TestNAME < Test::Unit::TestCase 

    def test_directions() 
    assert_equal(Lexicon.scan("north"),[['direction', 'north']]) 
    result = Lexicon.scan("north south east") 

    assert_equal(result, [['direction', 'north'], 
      ['direction', 'south'], 
      ['direction', 'east']]) 
    end 
end 

我有不同的陣列和條件五個額外的測試,所有六個測試失敗耙測試。但是,代碼本身按預期工作。

我編輯的測試文件發送數組,而不是字符串:

require './lib/lexicon.rb' 
require "test/unit" 

class TestNAME < Test::Unit::TestCase 

    def test_directions() 
    assert_equal(Lexicon.scan(["north"]),[['direction', 'north']]) 
    result = Lexicon.scan(["north", "south", "east"]) 

    assert_equal(result, [['direction', 'north'], 
      ['direction', 'south'], 
      ['direction', 'east']]) 
    end 
end 

仍然失敗rake test上:

F 
=============================================================================== 

> 

    Failure: test_directions(TestNAME) 
/Users/antony1/Documents/Ruby/projects/ex48/tests/test_ex48.rb:7:in 
`test_directions' 
     4: class TestNAME < Test::Unit::TestCase 
     5: 
     6: def test_directions() => 7:  assert_equal(Lexicon.scan(["north"]),[['direction', 'north']]) 
     8:  result = Lexicon.scan(["north", "south", "east"]) 
     9:  
    10:  assert_equal(result, [['direction', 'north'], <nil> expected but was <[["direction", "north"]]> 

diff: ?   nil   ? [["directio ", "north"]] 
=============================================================================== 


Finished in 0.009885 seconds. 

1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 
0 notifications 0% passed 
+0

即使沒有測試文件直接運行代碼,也會出現該錯誤。 –

+0

你的'.scan'方法不會返回任何因此你看到'nil'的東西。再次檢查我的答案。 –

回答

1

.scan方法接受陣列,而不是一個字符串,那麼你應該重寫:

Lexicon.scan("north") 

Lexicon.scan(["north"]) 

Lexicon.scan("north south east") 

Lexicon.scan(["north", "south", "east"]) 

此外,我想你想根據自己的規格在這個方法返回一個@sentence,所以你的方法應該是這樣的:

def self.scan(words) 
    words.each do |word| 
     recognize = false 
     if @direction.include?(word) == true 
     direction = ['direction', word] 
     @sentence.push(direction) 
     recognize = true 
     end 
    end 
    @sentence 
    end 

完整的工作解決方案。

我不明白你是如何設計的,而是爲了讓你的測試通過它應該是:

lexicon.rb

class Lexicon 
    @sentence = [] 
    @direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back'] 

    def self.scan(words) 
    words.each do |word| 
     recognize = false 
     if @direction.include?(word) == true 
     direction = ['direction', word] 
     @sentence.push(direction) 
     recognize = true 
     end 
    end 
    @sentence 
    end 
end 

test.rb

require './lexicon.rb' 
require "test/unit" 

class TestNAME < Test::Unit::TestCase 

    def test_directions() 
    assert_equal([['direction', 'north']], Lexicon.scan(["north"])) 
    assert_equal(
     [['direction', 'north'], ['direction', 'north'], ['direction', 'south'], ['direction', 'east']], 
     Lexicon.scan(["north", "south", "east"]) 
    ) 
    end 
end 

以0.000結束901秒。 1的測試中,2個斷言,0故障,0錯誤,0 pendings,0省略,0通知 100%通過

+0

變量字是一個數組,是一個字符串上的.split操作的結果。 – AntonySerio

+0

我看到測試文件發送的是字符串而不是數組。我嘗試你建議的修正,但它仍然失敗: – AntonySerio

+0

失敗: test_directions(測試名) /Users/antony1/Documents/Ruby/projects/ex48/tests/test_ex48.rb:7:in'test_directions' 4: class TestName 7:assert_equal(Lexicon.scan([「north」]),[['direction','north']]) 8:結果= Lexicon.scan([ 「北」, 「南」, 「東」]) 9: 10:assert_equal(結果,[[ '方向', '北'], 預期但 <[[「direction」,「north」]]> diff: ?無 ? [[「directio」,「north」]] – AntonySerio

0

下面的方法接受術語的兩個陣列,以及與空格分隔術語的字符串。它按照您測試的預期返回列表。它不會引入無意義的局部變量recognize,無論如何,它並未被原始代碼使用。

def self.scan(words) 
    words = words.strip.split(/\s+/) if words.is_a?(String) # accept string 
    @sentence |= words.inject([]) do |memo, word| # accumulate uniqs 
    memo << ['direction', word] if @direction.include?(word) 
    memo 
    end 
end 

使用此方法您的現有測試將通過。

+0

您的代碼與原始版本的工作方式不同,其中'@句子'將以前的所有調用累加到'Lexicon.scan'方法。 –

+0

@ RustamA.Gasanov Thx,已更新。 – mudasobwa

+0

該方法只需要接受數組。變量字是一個數組,是一個字符串上的.split操作的結果。局部變量識別用於其他測試。 – AntonySerio