我正在尝试使用我用来开发输出中的函数的示例来加速我的测试开发。
我有两个数组:输入和输出。
function(input[0]) == output[0]。
循环正在工作,但它被卡在最后一个索引中。示例:如果input.length = 10,则始终执行function(input10) == output10。
describe "multiple_chords" do
input = ["A7 DMaj79", "E-7 A7", "D-7 G7", "Bb-7b5 Eb7b9" , "Bb-7b5 Eb7", "G7 A7", "D-7b5 G7b9", "D-79 G#7913"]
output = [[{"root"=>"A", "def"=>"7"}, {"root"=>"D", "def"=>"Maj7"}], [{"root"=>"E", "def"=>"-7"}, {"root"=>"A", "def"=>"7"}], [{"root"=>"D", "def"=>"-7"}, {"root"=>"G", "def"=>"7"}], [{"root"=>"Bb", "def"=>"-7b5"}, {"root"=>"Eb", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"Bb", "def"=>"-7b5"}, {"root"=>"Eb", "def"=>"7"}], [{"root"=>"G", "def"=>"7"}, {"root"=>"A", "def"=>"7"}], [{"root"=>"D", "def"=>"-7b5"}, {"root"=>"G", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]]
for i in 0..input.length-1
it "analyzes correctly #{input[i]}" do
expect(IpmChords::multiple_chords(input[i])).to eq(output[i])
end
end
end控制台中的输出为:
6) IpmChords multiple_chords analyzes correctly Bb-7b5 Eb7
Failure/Error: expect(IpmChords::multiple_chords(input[i])).to eq(output[i])
expected: [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]
got: [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>"13", "def"=>""}]
(compared using ==)
Diff:
@@ -1,3 +1,3 @@
[{"root"=>"D", "tensions"=>"9", "def"=>"-7"},
- {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]
+ {"root"=>"G#", "tensions"=>"13", "def"=>""}]
# ./spec/ipm_classes/ipm_chords_ipm_class_spec.rb:28:in `block (4 levels) in <top (required)>'
7) IpmChords multiple_chords analyzes correctly E-7 A7
Failure/Error: expect(IpmChords::multiple_chords(input[i])).to eq(output[i])
expected: [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]
got: [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>"13", "def"=>""}]
(compared using ==)
Diff:
@@ -1,3 +1,3 @@
[{"root"=>"D", "tensions"=>"9", "def"=>"-7"},
- {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]
+ {"root"=>"G#", "tensions"=>"13", "def"=>""}]您可以看到它总是计算相同的数组索引,但是每次循环迭代的测试名称都是不同的:
6) IpmChords multiple_chords analyzes correctly Bb-7b5 Eb7
7) IpmChords multiple_chords analyzes correctly E-7 A7我认为这将是一个伟大的时间节省,它应该工作,不是吗?希望你能帮助我。谢谢
发布于 2013-07-18 08:00:37
您在这里遇到了Ruby的块绑定的一个小问题。发生的事情是,每个块都将绑定到i,它仍然在作用域中,这意味着它们被调用,它们都将使用i最终的值(这将是最后的数组索引)。
如果你使用Ruby的块迭代器,它将会工作:
describe "multiple_chords" do
input = ["A7 DMaj79", "E-7 A7", "D-7 G7", "Bb-7b5 Eb7b9" , "Bb-7b5 Eb7", "G7 A7", "D-7b5 G7b9", "D-79 G#7913"]
output = [[{"root"=>"A", "def"=>"7"}, {"root"=>"D", "def"=>"Maj7"}], [{"root"=>"E", "def"=>"-7"}, {"root"=>"A", "def"=>"7"}], [{"root"=>"D", "def"=>"-7"}, {"root"=>"G", "def"=>"7"}], [{"root"=>"Bb", "def"=>"-7b5"}, {"root"=>"Eb", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"Bb", "def"=>"-7b5"}, {"root"=>"Eb", "def"=>"7"}], [{"root"=>"G", "def"=>"7"}, {"root"=>"A", "def"=>"7"}], [{"root"=>"D", "def"=>"-7b5"}, {"root"=>"G", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"D", "tensions"=>"9", "def"=>"-7"}, {"root"=>"G#", "tensions"=>["9", "13"], "def"=>"7"}]]
input.each_with_index do |chord, index|
it "analyzes correctly #{chord}" do
expect(IpmChords::multiple_chords(chord)).to eq(output[index])
end
end
end这样做的原因是,您最终不会在保留在作用域中的每个块之外创建变量,因此每个块最终都会使用正确的值进行调用。生成到每个循环的值是该循环的局部值,因此您不会得到“出血”。
顺便说一句,惯用的Ruby通常希望使用枚举数,而不是for循环。
关于样式的说明;您可以通过定义并迭代预期输入和输出的散列来稍微清理一下:
describe "multiple_chords" do
CHORD_MAP = {
"A7 DMaj79" => [{"root"=>"A", "def"=>"7"}, {"root"=>"D", "def"=>"Maj7"}],
"E-7 A7" => [{"root"=>"E", "def"=>"-7"}, {"root"=>"A", "def"=>"7"}],
# ...
}
CHORD_MAP.each do |chord, result|
it "analyzes correctly #{chord}" do
expect(IpmChords::multiple_chords(chord)).to eq(result)
end
end
endhttps://stackoverflow.com/questions/17711866
复制相似问题