如何为类或模块级常量编写Ruby单元测试?
如何编写测试来验证类或模块中是否存在常量 例如:如何为类或模块级常量编写Ruby单元测试?,ruby,Ruby,如何编写测试来验证类或模块中是否存在常量 例如: module MyModule VERSION = "0.3.1" end 我试过了 require 'test/unit' require 'shoulda' require "my_class" class MyModuleTest < Test::Unit::TestCase should "have a Version constant" do # next two lines crash assert_
module MyModule
VERSION = "0.3.1"
end
我试过了
require 'test/unit'
require 'shoulda'
require "my_class"
class MyModuleTest < Test::Unit::TestCase
should "have a Version constant" do
# next two lines crash
assert_respond_to MyModule, :VERSION
assert_respond_to 'VERSION', MyModule
end
end
要求“测试/单元”
需要“shoulda”
需要“我的课”
类MyModuleTest代码> 你会考虑使用更自然的工作流吗?这意味着你可以使用任何在普通Ruby中工作的方法,这样你就不需要记住很多多余的东西了。
require 'specular'
module MyModule
VERSION = "0.3.1"
end
Spec.new do
check(MyModule).const_defined? :VERSION
end
puts Specular.run
# => check(MyModule).const_defined? :VERSION
# => - passed
# => Specs: 1
# => Tests: 0
# => Assertions: 1
因此,使用纯Ruby,您可以:MyModule.const_defined?:版本
使用镜面反射:检查(MyModule).const_定义?:版本
差别不大,因此无需反复学习/记忆/回忆。谢谢@slivu<代码>断言MyModule.const_已定义?:版本
成功了。我也会让镜面宝石旋转一下。它“感觉”更自然。