0

我有这个规格文件:

require 'spec_helper'

for i in 1..3
   describe file ("/var/tmp/efl_test_0#{i}_link" )do
      it { should be_linked_to "/tmp/efl_test_0#{i}" }
   end
end

预期结果是:

   /var/tmp/efl_test01_link should be_linke_to /tmp/efl_test01
   /var/tmp/efl_test02_link should be_linke_to /tmp/efl_test02
   /var/tmp/efl_test03_link should be_linke_to /tmp/efl_test03

实际结果是:

 Failure/Error: it { should be_linked_to "/tmp/efl_test_0#{i}" }
   stat -c %N /var/tmp/efl_test_01_link | egrep -e "-> ./tmp/efl_test_03."
 Failure/Error: it { should be_linked_to "/tmp/efl_test_0#{i}" }
   stat -c %N /var/tmp/efl_test_02_link | egrep -e "-> ./tmp/efl_test_03."
 Failure/Error: it { should be_linked_to "/tmp/efl_test_0#{i}" }
   stat -c %N /var/tmp/efl_test_03_link | egrep -e "-> ./tmp/efl_test_03."

每个链接都与03目标进行比较。我猜这个问题是关于循环的。

我做错了什么?

4

2 回答 2

2

这可能是如何i被捕获为闭包并由describe稍后进行惰性评估的工件。那时它已经增加了。您可能需要故意捕获它:

for i in 1..3
  path = "/tmp/efl_test_0#{i}"
  describe file ("/var/tmp/efl_test_0#{i}_link" )do
    it { should be_linked_to path }
  end
end

通常你会使用3.times do更传统的 Ruby。该for构造几乎从未使用过。

于 2014-12-15T16:12:41.997 回答
1

这有效:

require 'spec_helper'

3.times do |i|
   describe file ("/var/tmp/efl_test_0#{i+1}_link" )do
      it { should be_linked_to "/tmp/efl_test_0#{i+1}" }
   end
end

结果:

    rspec ./spec/localhost/025_efl_test.rb:5 # 
File "/var/tmp/efl_test_01_link" should be linked to "/tmp/efl_test_01"
    rspec ./spec/localhost/025_efl_test.rb:5 # 
File "/var/tmp/efl_test_02_link" should be linked to "/tmp/efl_test_02"
    rspec ./spec/localhost/025_efl_test.rb:5 # 
File "/var/tmp/efl_test_03_link" should be linked to "/tmp/efl_test_03"
于 2014-12-15T17:00:05.717 回答