我正在开发一个 gem(不是 Rails 的东西,只是 Ruby),到目前为止代码运行良好,但是从 gem 根文件夹运行“rspec”时出现以下错误:
$ rspec
spec/logfile_spec.rb:3:in `<top (required)>': uninitialized constant LogFile (NameError)
...
我的 gem 是这样组织的:
$ 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
以下是一些文件的内容:
- 规范/spec_helper.rb
require 'logmsg'
RSpec.configure do |config|
...
end
- spec/logmsg_spec.rb(当我单独测试时,这个工作正常)
require 'spec_helper'
describe Logmsg do
it 'has a version number' do
expect(Logmsg::VERSION).not_to be nil
end
end
- spec/logfile_spec.rb(这个让我有些头疼)
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
- lib/logmsg.rb
require 'psych'
require 'logmsg/version'
require 'logmsg/logfile'
module Logmsg
class LogMsg
...
end
end
- lib/logmsg/logfile.rb(这是它没有找到类的那个)
require 'logger'
module Logmsg
class LogFile
...
end
end
非常感谢您的帮助!谢谢!
最佳答案
当你这样写的时候:
describe LogFile do
Rspec 会寻找 lib/log_file.rb,并在找不到时引发该错误。您可以将该描述更改为字符串:
describe `LogFile` do
或者给它一个可以找到它的位置:
describe Logmsg::LogFile do
关于ruby - 运行 RSpec 时找不到库类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818654/