如何将模块混合到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