我正在开发一个gem(而不是只有Ruby的东西)到目前为止,代码工作得很好,但是当我从gem根文件夹运行“rspec”时出现以下错误:
$ rspec
spec/logfile_spec.rb:3:in `<top (required)>': uninitialized constant LogFile (NameError)
...
这就是我宝石的组织方式:
$ ls -R lib
logmsg logmsg.rb
lib/logmsg:
logfile.rb version.rb yaml
lib/logmsg/yaml:
logmsg.yml
我的规格:
$ ls -R spec/
logfile_spec.rb logmsg_spec.rb spec_helper.rb
以下是一些文件的内容:
require 'logmsg'
RSpec.configure do |config|
...
end
require 'spec_helper'
describe Logmsg do
it 'has a version number' do
expect(Logmsg::VERSION).not_to be nil
end
end
require 'spec_helper'
describe LogFile do
subject { Logmsg::LogFile.new('test') }
its(:name) { should_not be nil }
its(:datetime_format) { should_not be nil }
its(:format) { should_not be nil }
its(:path) { should_not be nil }
context "registered" do
its(:registered) { should be true }
end
context 'not registered' do
its(:registered) { should be false }
end
end
require 'psych'
require 'logmsg/version'
require 'logmsg/logfile'
module Logmsg
class LogMsg
...
end
end
require 'logger'
module Logmsg
class LogFile
...
end
end
非常感谢您的帮助!谢谢!
答案 0 :(得分:3)
当你这样写:
describe LogFile do
Rspec寻找lib / log_file.rb,并在找不到它时引发该错误。您可以将该描述更改为字符串:
describe `LogFile` do
或给它找到可以的位置:
describe Logmsg::LogFile do