ruby - 如何在 rspec 测试中包含 lib 目录

标签 ruby testing rspec rspec-rails

我在 JRuby 1.7.4 上测试包含 lib 目录的 gem 时遇到问题。

我想测试位于 lib/vger/resources/account_manager.rb 的文件

我的规范文件在 spec/vger/resources/account_manager_spec.rb

require 'spec_helper'

describe Vger::Resources::AccountManager do     
    .
    .
    end 
end

我正在尝试将要测试的文件包含在 spec_helper.rb

require 'rubygems'
require 'bundler/setup'
require 'vger/resources/account_manager'
require 'vger'

RSpec.configure do |config|
  # some (optional) config here
end

通过命令 rspec spec/vger/resources/account_manager_spec.rb 运行测试时,我收到此错误:

NameError: uninitialized constant Vger::Resources
    const_missing at org/jruby/RubyModule.java:2631

我想测试的文件似乎没有加载。请告诉我我哪里出错了,我应该在哪里改正。

最佳答案

在调用 require 之前手动更新 spec_helper.rb 中的 LOAD PATH 应该可以解决问题。尝试将其设为 spec_helper.rb 的第一行:

$: << '../lib'

$LOAD_PATH << '../lib'

($:$LOAD_PATH 的别名)

关于ruby - 如何在 rspec 测试中包含 lib 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954989/

相关文章:

unit-testing - Dart:如何测试流是否在特定时间发出元素?

angular - 为什么我的测试不使用服务 stub 方法?

ruby - 根据要求使用特定的 VCR 磁带

ruby - nokogiri doc.xpath ('head' ) 返回 nil

python - 如何针对不同的 Python *补丁* 版本测试库?

ruby - 检查文件/文件夹是否在 Ruby 的子目录中

testing - rspec 模拟外部 api

ruby-on-rails - Rails 使用参数测试 Controller 私有(private)方法

ruby-on-rails - 没有路由匹配 [GET] "/simulations"

ruby - 为什么 return 会跳过我的三元运算符中产生的值?