ruby – 使用Rspec进行宝石测试
发布时间:2020-12-17 03:01:00 所属栏目:百科 来源:网络整理
导读:我已经编写了一个自定义 Ruby Gem来挂钩我们公司的身份验证和授权系统,并开始为gem开发单元测试. 在我们的rails应用程序中,Gem可以通过environment.rb和包含配置值的自定义初始化程序和yaml文件进行配置. 我需要在rails中转换Gem的配置来测试独立的Gem.如何
|
我已经编写了一个自定义
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
结束 解决方法
这是一个简单的项目,您可以通过它来了解自己的工作方式:
multiplier
首先,如果您自己进行宝石管理,请不要使用jeweler等辅助工具为您完成.安装珠宝商宝石(宝石安装珠宝商),安装完宝石后,创建宝石项目: jeweler --rspec your_gem_name 有了这个,它将设置一个骨架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
所以,这里存在我们宝石的配置,但你甚至可以在每个规格上做,如果你需要的话.但是如果你想为所有规格使用单个配置,那么你就应该放置它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
