隐藏未定义常量
隐藏一个已经未定义的常量是一个无操作。这在规范文件可能在隔离环境(例如运行一个规范文件)或加载所有代码库部分的完整环境(例如运行整个套件)中运行时很有用。
隐藏未定义的常量
假设一个名为“hide_const_spec.rb”的文件包含以下内容:
RSpec.describe "hiding UNDEFINED_CONSTANT" do
it "has no effect" do
hide_const("UNDEFINED_CONSTANT")
expect { UNDEFINED_CONSTANT }.to raise_error(NameError)
end
it "is still undefined after the example completes" do
expect { UNDEFINED_CONSTANT }.to raise_error(NameError)
end
end
当我运行 rspec hide_const_spec.rb
则所有示例应该通过。