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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16954989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:01:19  来源:igfitidea点击:

How to include lib directory in rspec tests

rubytestingrspecrspec-rails

提问by Akash Khandelwal

I am having a problem in testing my gem which includes a libdirectory, on JRuby 1.7.4.

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

I want to test a file located at lib/vger/resources/account_manager.rb

我想测试一个位于 lib/vger/resources/account_manager.rb

My spec file is in spec/vger/resources/account_manager_spec.rb

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

require 'spec_helper'

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

I am trying to include the file which I want to test in spec_helper.rb

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

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

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

While running the test by the command rspec spec/vger/resources/account_manager_spec.rbI am getting this error:

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

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

I seems that the file which I want to test is not getting loaded. Please tell me where I am going wrong and where should I make corrections.

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

回答by Akash Khandelwal

Manually update your LOAD PATH in spec_helper.rb before calling requireshould do the trick. Try making this the first line of your spec_helper.rb:

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

$: << '../lib'

or

或者

$LOAD_PATH << '../lib'

($:is an alias for $LOAD_PATH)

($:是 的别名$LOAD_PATH)

回答by Slavik Shynkarenko

You can add the following line to your .rspecfile in app's root: -I lib

您可以.rspec将以下行添加到应用程序根目录中的文件中: -I lib

It's also possible to include files: -r lib/api.rb

也可以包含文件: -r lib/api.rb

These options are described as follows:

这些选项描述如下:

-I PATH

Specify PATH to add to $LOAD_PATH (may be used more than once).

-r, --require PATH

Require a file.

-I 路径

指定要添加到 $LOAD_PATH 的 PATH(可以多次使用)。

-r, --require 路径

需要一个文件。

回答by Stephane Paul

I use the following for my specs...depending on which level your lib is....

我将以下内容用于我的规格......取决于你的库是哪个级别......

require_relative '../../lib/module'

require_relative '../../lib/module'

回答by Ivan Shamatov

RSpec loads rails environment, as I remember, so you just need to add to autoload directories in your application.rb file

RSpec 加载 rails 环境,我记得,所以你只需要添加到 application.rb 文件中的自动加载目录

Find this line

找到这一行

# config.autoload_paths += %W(#{config.root}/extras)

uncomment it fix it to be like this:

取消注释它修复它是这样的:

config.autoload_paths += %W(#{config.root}/lib)

it should work

它应该工作