RubyonRails——如何让我的代码通过rspec测试?

rqqzpn5f  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(164)

有人能读一下我的代码,告诉我为什么它总是不能通过rspec测试吗?代码是完美的,但是测试在某些地方失败了。我得到了预期的结果,但测试失败了。我的代码用两个圆盘解算河内塔,也可以按相反顺序移动(起始位置分别为1和2)。
工作代码:

def move(starting, goal)
  # your code here
  n = 2

  if starting == 1
    mid = 2
    toh(n, starting, mid, goal)
  else
    mid = 1
    toh(n, starting, mid, goal)
  end
end

def toh(n, starting, mid, goal)
  return puts "#{starting} -> #{goal}" if n == 1
  toh(n-1, starting, goal, mid)
  puts "#{starting} -> #{goal}"
  toh(n-1, mid, starting, goal)
  nil
end

puts move(1, 3)

# => 1->2 1->3 2->3

puts move(2, 3)

# => 2->1 2->3 1->3

光谱测试:

1 -> 2
1 -> 3
2 -> 3
F1 -> 2
1 -> 2
2 -> 2
F2 -> 1
2 -> 3
1 -> 3
F2 -> 1
2 -> 1
1 -> 1
F

Failures:

  1) UnitTests move_1_3
     Failure/Error: Unable to find (eval) to read failed line

     NoMethodError:
       undefined method `strip' for nil:NilClass
     # (eval):8:in `block (2 levels) in <top (required)>'

  2) UnitTests move_1_2
     Failure/Error: Unable to find (eval) to read failed line

     NoMethodError:
       undefined method `strip' for nil:NilClass
     # (eval):14:in `block (2 levels) in <top (required)>'

  3) UnitTests move_2_3
     Failure/Error: Unable to find (eval) to read failed line

     NoMethodError:
       undefined method `strip' for nil:NilClass
     # (eval):20:in `block (2 levels) in <top (required)>'

  4) UnitTests move_2_1
     Failure/Error: Unable to find (eval) to read failed line

     NoMethodError:
       undefined method `strip' for nil:NilClass
     # (eval):26:in `block (2 levels) in <top (required)>'

Finished in 0.06713 seconds (files took 0.99476 seconds to load)
4 examples, 4 failures

Failed examples:

rspec (eval)[1:1] # UnitTests move_1_3
rspec (eval)[1:2] # UnitTests move_1_2
rspec (eval)[1:3] # UnitTests move_2_3
rspec (eval)[1:4] # UnitTests move_2_1

这是河内的原始问题描述挑战描述塔-在本挑战的第1部分中,您需要将2个磁盘从任何起始挂钩移动到任何目标挂钩。
您将获得两个数字(从1到3)作为输入-起始桩和目标桩。编写一个函数,计算如何将2个磁盘从起始销钉移动到目标销钉。返回包含所有步骤的字符串。
输出格式:
添加要从中移动的桩号、箭头“->”和要移动到的桩号。例如,要从桩号1移动到桩号3,请打印“1->3”。
移动示例(1,3)

=> 1->2 1->3 2->3

移动(2,3)

=> 2->1 2->3 1->3

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题