对于我正在处理的宝石,我不断收到未初始化常量的错误。我不知道它是如何找不到这个文件的。我特别难过,因为我在同一个位置使用相同的语法有一个类似的文件,并且它被读得很好。
我不确定要包含哪些文件,因此如果有帮助,请将您希望在评论中看到的文件名。
LIB / gender.rb:
class TextEng::Gender
...
end
规格/ gender_spec.rb:
require_relative 'spec_helper'
describe TextEng::Gender do #this is the line that's throwing an error
...
end
text_eng.gemspec:
$:.push File.expand_path("../lib", __FILE__)
这是错误:
/Users/stevenspiel/text_eng/spec/gender_spec.rb:3:in `<top (required)>': uninitialized constant TextEng::Gender (NameError)
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
from /Users/stevenspiel/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
Matheus指出,我可以在gender_spec.rb中简单地require_relative '../lib/gender'
,但是让我感到困惑的是我在同一目录中有一个文件,基本上做同样的事情,只是用不同的名称,它工作正常。文件结构如下所示。
LIB / name.rb:
class TextEng::Name
...
end
规格/ gender_spec.rb
require_relative 'spec_helper'
describe TextEng::Name do # this line does not throw any errors
...
end
为什么?
答案 0 :(得分:1)
spec/gender_spec.rb
似乎不了解TextEng::Gender
类。在尝试使用它之前,您应该要求定义该类的文件:
require 'gender'
describe TextEng::Gender do
# ...
end
请注意,根据rubygems约定,TextEng::Gender
类应在lib/text_eng/gender.rb
文件中定义,因此require
调用应如下所示:
require 'text_eng/gender'