使用 Rspec 进行宝石测试

发布于 2024-11-26 10:11:11 字数 909 浏览 2 评论 0原文

我已经编写了一个自定义 Ruby Gem 来连接到我们公司的身份验证和授权系统,并开始为 gem 开发单元测试。

在我们的 Rails 应用程序中,可以通过 environment.rb 以及包含配置值的自定义初始化程序和 yaml 文件来配置 Gem。

我需要翻译 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

导轨末端

I have written a custom Ruby Gem to hook into our company's authentication and authorization system and am starting to develop the unit tests for the gem.

In our rails app, the Gem can be configured via environment.rb and a custom initializer and yaml file containing the configuration values.

I need to translate the configuration of the Gem in rails to test the standalone Gem. How do I translate this over to Rspec to perform integration testing??

Gem configuration in rails

# 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

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-12-03 10:11:11

这是一个简单的项目,您可以在其中了解如何进行操作:乘数

首先,如果您自己进行宝石管理,请不要使用 珠宝商为您做这件事。安装 Jeweler gem (gem install Jeweler),安装完成后,创建您的 gem 项目:

jeweler --rspec your_gem_name

这样,它将设置一个具有单个主文件的骨架 gem(您可以在其中需要必要的 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

所以,这里存放着我们的gem的配置,但你甚至可以在每个规范上执行它,如果你'需要它。但是,如果您想对所有规格使用单个配置,那么您应该将其放置在此处。

Here's a simple project where you can see how you'd go by doing it: multiplier

First and foremost, if you're doing the gem management by yourself, please don't, use helper tools like jeweler to do it for you. Install the jeweler gem (gem install jeweler) and once you have it installed, create your gem projet:

jeweler --rspec your_gem_name

With this, it's going to setup a skeleton gem that's going to have a single main file (where you would require your necessary gem files) and the spec folder.

At the spec folder there's spec_helper.rb, that's where our configuration lives, what I did was:

$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

So, here lives the config for our gem, but you could even do it on each spec, if you'd need it. But if you want to use a single config for all specs this is where you should place it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文