aef*_*aef 3 ruby rspec namespaces ruby-1.9.3
我喜欢使用RSpec的include配置方法来包含仅用于命名空间的模块,这样我就不必为其内部类和模块使用完全限定名.这适用于Ruby 1.9.2中的RSpec 2.11.0.但现在在Ruby 1.9.3上这已不再适用了.我怎样才能让它再次运作?
这是一个示例foobar_spec.rb:
module Foo
class Bar
end
end
RSpec.configure do |config|
config.include Foo
end
describe Foo::Bar do
it "should work" do
Bar.new
end
end
Run Code Online (Sandbox Code Playgroud)
如果您通过以下命令调用它:
rspec foobar_spec.rb
Run Code Online (Sandbox Code Playgroud)
它可以在Ruby 1.9.2中正常工作.但它会在Ruby 1.9.3中引发以下错误:
Failure/Error: Bar.new
NameError:
uninitialized constant Bar
Run Code Online (Sandbox Code Playgroud)
She*_*ter 13
这个邮件列表条目讨论了1.9.3中如何查找常量的根变化,因此它看起来像是一个故意的变化.
您可以对整个测试进行范围调整,如下所示:
module Foo
describe Bar do
it "should work" do
Bar.new
end
end
end
Run Code Online (Sandbox Code Playgroud)
作为另一种解决方案,你可以提取新对象创建一个before或let或只是定义对象的subject测试.
如果你的目标是只需要一次指定命名空间,那么惯用RSpec的方式是使用described_class。像这样:
module Foo
class Bar
end
end
describe Foo::Bar do
it "should work" do
described_class.new
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6319 次 |
| 最近记录: |