加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何将模块混合到rspec上下文中

发布时间:2020-12-16 22:13:39 所属栏目:百科 来源:网络整理
导读:如何将模块混合到rspec上下文中(也称为描述),以使模块的常量可用于规范? module Foo FOO = 1enddescribe 'constants in rspec' do include Foo p const_get(:FOO) # = 1 p FOO # uninitialized constant FOO (NameError)end 当常量的名称不能很有趣时,const
如何将模块混合到rspec上下文中(也称为描述),以使模块的常量可用于规范?
module Foo
  FOO = 1
end

describe 'constants in rspec' do

  include Foo

  p const_get(:FOO)    # => 1
  p FOO                # uninitialized constant FOO (NameError)

end

当常量的名称不能很有趣时,const_get可以检索常量.是什么导致rspec好奇的行为?

我使用的是MRI 1.9.1和rspec 2.8.0.症状与MRI 1.8.7相同.

解决方法

您可以使用RSpec的shared_context:
shared_context 'constants' do
  FOO = 1
end

describe Model do
  include_context 'constants'

  p FOO    # => 1
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读