ruby - 运行 RSpec 时找不到库类

标签 ruby testing rspec rubygems

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

相关文章:

ruby - Ruby 的 length 方法是一个符号吗?为什么是:length sometimes the same as length?

运行 irb watir 命令列表的 Ruby 脚本

ruby-on-rails - 我需要在 ruby​​ on rails 中遍历 2 个数组。我知道 zip 函数

Ruby:如何在没有 Imagemagick 的情况下将图像叠加在另一个图像之上?

testing - Wicket 口和AtUnit

testing - 尝试验证模式时面对 java.lang.ClassNotFoundException : com. intuit.karate.demo.util.SchemaUtils

ruby-on-rails - 测试 View 编辑表单时 Rspec 失败

unit-testing - 如何有条件地拆除Tasty中的testbed资源?

ruby - 我可以在 Rspec 测试中设置 "it"的值吗?

ruby - 如果另一个测试失败,我可以跳过一个测试吗?