I like to use RSpec's include configuration method to include modules which are only for namespacing so that I don't have to use fully-qualified names for their inner classes and modules. This worked fine with RSpec 2.11.0 in Ruby 1.9.2. But now on Ruby 1.9.3 this doesn't work anymore. How can I get it working again?
我喜欢使用RSpec的包含配置方法来包含仅用于命名空间的模块,这样我就不必为它们的内部类和模块使用完全限定的名称。这在Ruby 1.9.2中的RSpec 2.11.0中运行良好。但是现在在Ruby 1.9.3上,它已经不能工作了。我怎么才能让它再次工作呢?
Here an example foobar_spec.rb:
这里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
If you call it by the following command:
如果您用以下命令调用它:
rspec foobar_spec.rb
It will work in Ruby 1.9.2 just fine. But it will raise the following error in Ruby 1.9.3:
它将在Ruby 1.9.2中正常工作。但是会在Ruby 1.9.3中出现如下错误:
Failure/Error: Bar.new
NameError:
uninitialized constant Bar
3 个解决方案
#1
12
This mailing list entry discusses the root change in 1.9.3 as to how constants are looked up, so it looks like a deliberate change.
这个邮件列表条目讨论了1.9.3中关于如何查找常量的根更改,因此它看起来像是一个经过深思熟虑的更改。
You could scope the whole test, like this:
您可以对整个测试进行限定,如下所示:
module Foo
describe Bar do
it "should work" do
Bar.new
end
end
end
As another solution, you could extract the new object creation to a before
or let
or just define the object as the subject
of the test.
作为另一种解决方案,您可以将新的对象创建提取到一个之前,或者将对象定义为测试的主题。
#2
4
If your goal is to only have to specify the namespace once, then the idiomatic RSpec way is to use described_class. Like this:
如果您的目标是只指定名称空间一次,那么惯用的RSpec方法是使用descripbed_class。是这样的:
module Foo
class Bar
end
end
describe Foo::Bar do
it "should work" do
described_class.new
end
end
#3
0
You need to use Foo::Bar in the it block as well as in the describe argument.
您需要使用Foo::Bar在it块中以及在描述参数中。
#1
12
This mailing list entry discusses the root change in 1.9.3 as to how constants are looked up, so it looks like a deliberate change.
这个邮件列表条目讨论了1.9.3中关于如何查找常量的根更改,因此它看起来像是一个经过深思熟虑的更改。
You could scope the whole test, like this:
您可以对整个测试进行限定,如下所示:
module Foo
describe Bar do
it "should work" do
Bar.new
end
end
end
As another solution, you could extract the new object creation to a before
or let
or just define the object as the subject
of the test.
作为另一种解决方案,您可以将新的对象创建提取到一个之前,或者将对象定义为测试的主题。
#2
4
If your goal is to only have to specify the namespace once, then the idiomatic RSpec way is to use described_class. Like this:
如果您的目标是只指定名称空间一次,那么惯用的RSpec方法是使用descripbed_class。是这样的:
module Foo
class Bar
end
end
describe Foo::Bar do
it "should work" do
described_class.new
end
end
#3
0
You need to use Foo::Bar in the it block as well as in the describe argument.
您需要使用Foo::Bar在it块中以及在描述参数中。