我已经编写了一个自定义Ruby Gem来挂钩我们公司的身份验证和授权系统,并开始为gem开发单元测试.
在我们的rails应用程序中,Gem可以通过environment.rb和包含配置值的自定义初始化程序和yaml文件进行配置.
我需要在rails中转换Gem的配置来测试独立的Gem.如何将其转换为Rspec以执行集成测试?
轨道中的宝石配置
# environment.rb
MyGem.configure do |config|
config.url = MY_CONFIG ['url']
config.application_name = MY_CONFIG ['app_name']
config.application_id = MY_CONFIG ['app_id']
config.logger = Rails.logger
config.log_level = :debug
# Rails config/initalizers/load_config.rb
# Custom config file loading automatically done via initializers
MY_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/my_config.yml")[Rails.env]
# config/my_config.yml
defaults: &defaults
url: http://url/to/service
app_name: my app
app_id: 1
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
Run Code Online (Sandbox Code Playgroud)
结束
这是一个简单的项目,你可以看到你如何去做:乘数
首先,如果您自己进行宝石管理,请不要使用珠宝商等辅助工具为您完成.安装珠宝商宝石(宝石安装珠宝商),安装完宝石后,创建宝石项目:
jeweler --rspec your_gem_name
Run Code Online (Sandbox Code Playgroud)
有了这个,它将设置一个骨架gem,它将有一个主文件(你需要你需要的宝石文件)和spec文件夹.
在spec文件夹中有spec_helper.rb,这就是我们的配置所在,我所做的是:
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'multiplier'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
end
Multiplier.configure do |config| #these are the only lines I added myself
config.multiplier 4
end
Run Code Online (Sandbox Code Playgroud)
所以,这里存在我们宝石的配置,但你甚至可以在每个规格上做,如果你需要的话.但是如果你想为所有规格使用单个配置,那么你就应该放置它.
| 归档时间: |
|
| 查看次数: |
2500 次 |
| 最近记录: |