ruby - 我如何在 RSpec 中测试我加载的代码是否有任何 Ruby 警告?

标签 ruby rspec

有什么方法可以访问 RSpec 示例中的 Ruby 警告列表吗?我在正在测试的库中收到以下错误,并想为其编写单元测试。

warning: previous definition of BUILD_FILE was here

最佳答案

我最终启动了一个新流程来评估流程输出。在我的例子中,问题是一个重新定义的常量,所以我通过将类两次加载到新的 Ruby 进程中然后使用 IO 流读取输出来进行测试:

it 'should not redefine BUILD_FILE constant' do
    root_path = "#{File.dirname(__FILE__)}/../../../../"
    cmd = "load \"#{root_path}libraries/build_file.rb\";"
    ruby_path = 'C:/opscode/chefdk/embedded/bin/ruby.exe'

    pipe_cmd_in, pipe_cmd_out = IO.pipe
    pid = Process.spawn("#{ruby_path} -e '#{cmd} #{cmd}'", :out => pipe_cmd_out, :err => pipe_cmd_out)
    Process.wait(pid)
    pipe_cmd_out.close
    output = pipe_cmd_in.read
    puts "OUTPUT = #{output}"

    expect(output).to_not match(/previous\sdefinition\sof\sBUILD_FILE/)
end

关于ruby - 我如何在 RSpec 中测试我加载的代码是否有任何 Ruby 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35562232/

相关文章:

ruby - Eratosthenes 变体筛法

ruby-on-rails - 如何在 Rails 4 中设置自定义字符串外键?

来自数组的数组的 Ruby 哈希

ruby-on-rails - 使用 Guard 和 Spork 重新加载 RSpec 共享示例

ruby-on-rails - rails - rspec - 为什么这个 "validates_inclusion_of :role, :in => %w[one, two, three ] }"不起作用

ruby-on-rails - 从另一个模型调用 Rails 模型

ruby-on-rails - 如何运行单个 RSpec 测试?

c - ruby 1.8 中缺少函数

ruby-on-rails-3 - Rails3 + Rspec2 : undefined method `authenticate!' for nil:NilClass

ruby - 主要 :Object 的未定义方法 `before'