我喜欢使用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

如果通过以下命令调用它:
rspec foobar_spec.rb

它将在Ruby 1.9.2中正常工作。但是它将在Ruby 1.9.3中引发以下错误:
Failure/Error: Bar.new
     NameError:
       uninitialized constant Bar

最佳答案

mailing list entry讨论了1.9.3中关于如何查找常量的根本更改,因此它看起来像是有意更改。

您可以确定整个测试的范围,如下所示:

module Foo
  describe Bar do
    it "should work" do
      Bar.new
    end
  end
end

作为另一种解决方案,您可以将创建的新对象提取到beforelet或仅将对象定义为测试的subject

10-08 04:49