如何编写测试来验证类或模块中是否存在常量?
示例:
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发布于 2012-11-29 04:16:58
你会考虑使用来实现更自然的工作流程吗?这意味着你可以使用任何在普通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? :VERSION
和镜面反射:check(MyModule).const_defined? :VERSION
没有太大的区别,因此没有什么需要反复学习/记住/回忆的。
https://stackoverflow.com/questions/13613338
复制相似问题