ruby – rails rspec – 如何检查模型常量?
发布时间:2020-12-16 19:28:47 所属栏目:百科 来源:网络整理
导读:我怎么能这样做: it { should have_constant(:FIXED_LIST) } 在我的模型(活动记录)中,我有FIXED_LIST =’A String’ 它不是db属性或方法,我无法使用responds_to或has_attribute来测试它(它们失败).我可以用什么来检查它. – 顺便说一下我安装了shoulda-matc
我怎么能这样做:
it { should have_constant(:FIXED_LIST) } 在我的模型(活动记录)中,我有FIXED_LIST =’A String’ 它不是db属性或方法,我无法使用responds_to或has_attribute来测试它(它们失败).我可以用什么来检查它. – 顺便说一下我安装了shoulda-matchers. 解决方法
根据David Chelimsky的回答,我通过略微修改他的代码来实现这一点.
在文件spec / support / utilities.rb(或spec / support中的其他一些文件)中,您可以放置??: RSpec::Matchers.define :have_constant do |const| match do |owner| owner.const_defined?(const) end end 注意使用“RSpec :: Matchers.define”而不是“matchers” 这允许测试规范中的常量,例如: it "should have a fixed list constant" do YourModel.should have_constant(:FIXED_LIST) end 注意使用“have_constant”而不是“have_const” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |