运行RSpec时找不到库类

时间:2016-01-15 19:43:10

标签: ruby testing rspec rubygems

我正在开发一个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

以下是一些文件的内容:

- spec / 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

非常感谢您的帮助!谢谢!

1 个答案:

答案 0 :(得分:3)

当你这样写:

describe LogFile do

Rspec寻找lib / log_file.rb,并在找不到它时引发该错误。您可以将该描述更改为字符串:

describe `LogFile` do

或给它找到可以的位置:

describe Logmsg::LogFile do