我有一个按预期工作的ruby模块。但是,当我尝试rake测试时,它失败了:
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
我针对不同的阵列和条件进行了五次额外测试,并且所有六项测试都未通过rake测试。但是,代码本身按预期工作。
我编辑了测试文件以发送数组而不是字符串:
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 :(得分: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.000901秒完成。 1个测试,2个断言,0个失败,0个错误,0个挂起,0个省略,0个通知 100%通过
答案 1 :(得分: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
使用此方法,您的现有测试将通过。