免责声明:
问题描述:
我的问题:为什么rspec会这样做?我错过了什么?
这是我在lib文件夹中的内容:
LIB / one.rb:
module NamespaceOne
class FooClass
def self.method_one(input)
1
end
end
end
LIB / two.rb:
module NamespaceTwo
class FooClass
def self.method_two(input)
2
end
end
end
,这是两个相应的规格:
规格/ one_spec.rb
require "one"
include NamespaceOne
describe FooClass do
describe ".method_one" do
context "given an empty string" do
it "returns 1" do
expect(FooClass.method_one("")).to eql(1)
end
end
end
end
和spec / two_spec.rb
require "two"
include NamespaceTwo
describe FooClass do
describe ".method_two" do
context "given an empty string" do
it "returns 2" do
expect(FooClass.method_two("")).to eql(2)
end
end
end
end
最后,rspec输出:
$ bundle exec rspec --format documentation
NamespaceOne::FooClass
.method_one
given an empty string
returns 1 (FAILED - 1)
NamespaceTwo::FooClass
.method_two
given an empty string
returns 2
Failures:
1) NamespaceOne::FooClass.method_one given an empty string returns 1
Failure/Error: expect(FooClass.method_one("")).to eql(1)
NoMethodError:
undefined method `method_one' for NamespaceTwo::FooClass:Class
Did you mean? method_two
# ./spec/one_spec.rb:10:in `block (4 levels) in <top (required)>'
Finished in 0.00201 seconds (files took 0.08006 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/one_spec.rb:9 # NamespaceOne::FooClass.method_one given an empty string returns 1
注意:在tests / examples中,如果我使用完整命名空间调用该方法,则测试会通过。即各个规范文件中的以下调整使测试通过,
规格/ one_spec.rb
expect(NamespaceOne::FooClass.method_one("")).to eql(1)
spec / two_spec.rb
expect(NamespaceTwo::FooClass.method_two("")).to eql(2)
但我不明白为什么'包括'模块,因为我做的还不够。
答案 0 :(得分:0)
当你做
时require "one"
include NamespaceOne
describe FooClass do
您在NamespaceOne
中加入Object
,但ruby不知道下一个名称空间FooClass
应解析为NamspaceOne
。那是因为它是词法上的(https://stackoverflow.com/a/20848317/3109182)。而是在描述时使用完整的命名空间,这样rspec和ruby知道要解析的常量:
require "one"
describe NamespaceOne::FooClass do
end