0

如何编写测试来测试hey定义如下的函数?模块CONSTANT加载到spec_helper中,其变量被冻结且无法更改。

module CONSTANT
  X=1
  Y=2
  self.freeze
end

def hey
  if CONSTANT::X == 1
    puts "OKOK"
  else
    puts "NOT OK"
  end
end

如何编写测试来测试 else 子句?

4

1 回答 1

1

使用const_set类方法。

编辑:我刚刚注意到这个freeze命令。我已经更新了我的示例。

例子:

CONSTANT = CONSTANT.dup
CONSTANT.const_set("X",0) # this will create a warning message when it sets the value
puts CONSTANT::X # returns 0
于 2013-09-11T15:39:26.113 回答